Understanding the Coordinate System in TikZ/LaTeX
The coordinate system in TikZ (LaTeX’s drawing package) provides several ways to position and reference points in your drawings, especially when creating inline shapes. Let me explain the key concepts:
Basic Coordinate System
Cartesian Coordinates
The most fundamental coordinate system in TikZ is the Cartesian coordinate system:
\tikz \draw (0,0) -- (1,2);
- The first coordinate
(0,0)
is the origin - The second coordinate
(1,2)
is 1 unit right and 2 units up from the origin - By default, 1 unit = 1cm (but this can be changed)
Relative Coordinates
You can specify coordinates relative to the previous point using ++
:
\tikz \draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;
This draws a 1×1 square by moving right, then up, then left, then closing the path.
Coordinate Systems for Inline Shapes
When drawing shapes inline with text, understanding these coordinate aspects is crucial:
Baseline Alignment
For inline shapes, the most important parameter is the baseline
option:
\tikz[baseline=(node.base)] \node (node) {A};
This aligns the base of the node "A" with the baseline of the surrounding text.
Origin Placement
When using \tikz
inline, the origin (0,0)
is placed at the current position in the text. This is why a basic shape like:
Text with \tikz \fill[red] (0,0) circle (0.2em); a red dot.
Places the dot exactly at that position in the text flow.
Specifying Coordinates in Different Units
You can use different units for precision:
\tikz \draw (0,0) -- (1cm,1pt); % Mixing units
\tikz \draw (0,0) circle (0.5ex); % Use font-relative units
For inline shapes, font-relative units (em
, ex
) are particularly useful:
1em
= the width of the letter ‘M’ in the current font1ex
= the height of the letter ‘x’ in the current font
Node-Based Coordinate System
Nodes provide named anchors that can be referenced:
\tikz[baseline]{
\node (A) at (0,0) {X};
\draw[red] (A.north) -- (A.south);
\draw[blue] (A.east) -- (A.west);
}
This creates a node with the text "X" and draws lines through its cardinal points.
Practical Example for Inline Shapes
Here’s how all these coordinate concepts apply when creating an inline smoke effect:
\newcommand{\smokepuff}[1][gray]{%
\tikz[baseline=-0.5ex]{%
% Origin at baseline-0.5ex
\draw[#1, fill=#1!30, decoration={random steps, segment length=2pt, amplitude=1pt},
decorate] (0,0) circle (0.3em);
% Small particles around the main puff
\foreach \i in {1,...,5}{%
\fill[#1!40] ({rand*0.4em},{rand*0.4em}) circle (0.05em);
}
}%
}
In this smoke puff example:
baseline=-0.5ex
positions the origin slightly below the text baseline(0,0) circle (0.3em)
draws the main puff at that origin- The
\foreach
loop places random small particles using relative coordinates
Polar Coordinates
TikZ also supports polar coordinates using (angle:radius)
syntax:
\tikz \draw (0:1cm) -- (60:1cm) -- (120:1cm) -- (180:1cm) -- (240:1cm) -- (300:1cm) -- cycle;
This draws a regular hexagon by specifying points at equal angles around a circle.
Understanding these coordinate systems allows you to precisely position elements in your inline TikZ graphics, creating professional-looking mathematical diagrams and visual elements embedded within your text.
[[Latex tikz Drawing Package]]