Coordinates in TikZ
Master the different coordinate systems and positioning techniques in TikZ.
Cartesian Coordinates
The default coordinate system uses (x,y) pairs. The default unit is centimeters:
\begin{tikzpicture}
% Basic Cartesian coordinates
\draw (0,0) -- (3,0) -- (3,2) -- (0,2) -- cycle;
% With explicit units
\draw (0cm,0cm) circle (1cm);
\draw (4mm, 4mm) -- (20mm, 15mm);
% Mixed units
\draw (0pt, 3cm) -- (100pt, 3cm);
\end{tikzpicture}Available Units
| Unit | Name | Size |
|---|---|---|
cm | Centimeters | 1 cm (default) |
mm | Millimeters | 0.1 cm |
in | Inches | 2.54 cm |
pt | Points | 1/72.27 in |
em | Em (font-relative) | Width of 'M' |
ex | Ex (font-relative) | Height of 'x' |
Polar Coordinates
Specify points using angle and distance: (angle:radius)
\begin{tikzpicture}
% Point at 45 degrees, 2cm from origin
\draw (0,0) -- (45:2cm);
% Points around a circle
\draw (0:1cm) -- (60:1cm) -- (120:1cm) -- (180:1cm) --
(240:1cm) -- (300:1cm) -- cycle;
% Hexagon using polar coordinates
\foreach \angle in {0, 60, 120, 180, 240, 300} {
\draw (\angle:1.5cm) -- (\angle+60:1.5cm);
}
\end{tikzpicture}Relative Coordinates
Use ++ and + for relative positioning:
\begin{tikzpicture}
% ++ moves the current point (updates reference)
\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;
% Draws a square: (0,0) -> (1,0) -> (1,1) -> (0,1) -> (0,0)
% + keeps the current point (doesn't update reference)
\draw[red] (3,0) -- +(1,0) -- +(1,1) -- +(0,1);
% All relative to (3,0): goes to (4,0), (4,1), (3,1)
% Combine absolute and relative
\draw[blue] (0,2) -- ++(2,0) -- (4,3) -- ++(-1,0);
\end{tikzpicture}++ updates the reference point after each move.+ keeps the original reference point.Named Coordinates
Save coordinates for reuse with names:
\begin{tikzpicture}
% Define named coordinates
\coordinate (A) at (0,0);
\coordinate (B) at (3,0);
\coordinate (C) at (1.5,2);
% Use named coordinates
\draw (A) -- (B) -- (C) -- cycle;
% Label the points
\node[below left] at (A) {A};
\node[below right] at (B) {B};
\node[above] at (C) {C};
% Node coordinates (automatically named)
\node (D) at (5,1) {D};
\draw (D.east) -- ++(1,0);
\end{tikzpicture}Coordinate Calculations
Use the calc library for mathematical coordinate operations:
\usetikzlibrary{calc}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\coordinate (C) at (4,3);
% Midpoint between two coordinates
\coordinate (M) at ($(A)!0.5!(B)$);
\fill[red] (M) circle (2pt);
% Point 25% of the way from A to C
\coordinate (P) at ($(A)!0.25!(C)$);
\fill[blue] (P) circle (2pt);
% Add coordinates
\coordinate (D) at ($(A) + (1,1)$);
% Scale a coordinate
\coordinate (E) at ($2*(B)$);
% Perpendicular projection
\coordinate (F) at ($(A)!(C)!(B)$); % C projected onto line AB
\draw (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}Intersection Points
Find where lines intersect using the intersections library:
\usetikzlibrary{intersections}
\begin{tikzpicture}
% Name the paths
\draw[name path=line1] (0,0) -- (3,2);
\draw[name path=line2] (0,2) -- (3,0);
% Find intersection and mark it
\fill[red, name intersections={of=line1 and line2}]
(intersection-1) circle (3pt);
% Circle and line intersection
\draw[name path=circle] (5,1) circle (1cm);
\draw[name path=line3] (4,0) -- (6,2);
\fill[blue, name intersections={of=circle and line3}]
(intersection-1) circle (2pt)
(intersection-2) circle (2pt);
\end{tikzpicture}Node Anchors
Nodes have predefined anchor points for precise connections:
\begin{tikzpicture}
\node[draw, rectangle, minimum width=2cm, minimum height=1cm] (box) at (0,0) {Box};
% Cardinal anchors
\fill[red] (box.north) circle (2pt);
\fill[red] (box.south) circle (2pt);
\fill[red] (box.east) circle (2pt);
\fill[red] (box.west) circle (2pt);
% Corner anchors
\fill[blue] (box.north east) circle (2pt);
\fill[blue] (box.north west) circle (2pt);
\fill[blue] (box.south east) circle (2pt);
\fill[blue] (box.south west) circle (2pt);
% Center
\fill[green] (box.center) circle (2pt);
% Connect from anchors
\draw[->] (box.east) -- ++(1,0);
\end{tikzpicture}Transformations
Apply transformations to coordinates:
\begin{tikzpicture}
% Original shape
\draw (0,0) rectangle (1,0.5);
% Shifted
\draw[xshift=2cm] (0,0) rectangle (1,0.5);
% Shifted in y
\draw[yshift=1cm] (0,0) rectangle (1,0.5);
% Rotated
\draw[rotate=45] (4,0) rectangle (5,0.5);
% Scaled
\draw[scale=1.5] (6,0) rectangle (6.5,0.25);
% Combined transformations
\draw[xshift=8cm, rotate=30, scale=1.2] (0,0) rectangle (1,0.5);
\end{tikzpicture}Canvas Transformations
Change the default coordinate system:
\begin{tikzpicture}[
x=1.5cm, % 1 unit in x = 1.5cm
y=1cm % 1 unit in y = 1cm
]
\draw[help lines] (0,0) grid (4,3);
\draw[thick] (0,0) -- (4,0) -- (4,3) -- (0,3) -- cycle;
\end{tikzpicture}
% Flipped y-axis (computer graphics style)
\begin{tikzpicture}[y=-1cm]
\draw (0,0) -- (0,2);
\node at (0,0) {Origin};
\node at (0,2) {Below};
\end{tikzpicture}Next Steps
Continue learning TikZ:
- Nodes and Labels - Position text and shapes
- Flowcharts - Use coordinates in diagrams
- Graphs - Network diagrams with coordinates
Coordinates in TikZ
Master the different coordinate systems and positioning techniques in TikZ.
Cartesian Coordinates
The default coordinate system uses (x,y) pairs. The default unit is centimeters:
\begin{tikzpicture}
% Basic Cartesian coordinates
\draw (0,0) -- (3,0) -- (3,2) -- (0,2) -- cycle;
% With explicit units
\draw (0cm,0cm) circle (1cm);
\draw (4mm, 4mm) -- (20mm, 15mm);
% Mixed units
\draw (0pt, 3cm) -- (100pt, 3cm);
\end{tikzpicture}Available Units
| Unit | Name | Size |
|---|---|---|
cm | Centimeters | 1 cm (default) |
mm | Millimeters | 0.1 cm |
in | Inches | 2.54 cm |
pt | Points | 1/72.27 in |
em | Em (font-relative) | Width of 'M' |
ex | Ex (font-relative) | Height of 'x' |
Polar Coordinates
Specify points using angle and distance: (angle:radius)
\begin{tikzpicture}
% Point at 45 degrees, 2cm from origin
\draw (0,0) -- (45:2cm);
% Points around a circle
\draw (0:1cm) -- (60:1cm) -- (120:1cm) -- (180:1cm) --
(240:1cm) -- (300:1cm) -- cycle;
% Hexagon using polar coordinates
\foreach \angle in {0, 60, 120, 180, 240, 300} {
\draw (\angle:1.5cm) -- (\angle+60:1.5cm);
}
\end{tikzpicture}Relative Coordinates
Use ++ and + for relative positioning:
\begin{tikzpicture}
% ++ moves the current point (updates reference)
\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;
% Draws a square: (0,0) -> (1,0) -> (1,1) -> (0,1) -> (0,0)
% + keeps the current point (doesn't update reference)
\draw[red] (3,0) -- +(1,0) -- +(1,1) -- +(0,1);
% All relative to (3,0): goes to (4,0), (4,1), (3,1)
% Combine absolute and relative
\draw[blue] (0,2) -- ++(2,0) -- (4,3) -- ++(-1,0);
\end{tikzpicture}++ updates the reference point after each move.+ keeps the original reference point.Named Coordinates
Save coordinates for reuse with names:
\begin{tikzpicture}
% Define named coordinates
\coordinate (A) at (0,0);
\coordinate (B) at (3,0);
\coordinate (C) at (1.5,2);
% Use named coordinates
\draw (A) -- (B) -- (C) -- cycle;
% Label the points
\node[below left] at (A) {A};
\node[below right] at (B) {B};
\node[above] at (C) {C};
% Node coordinates (automatically named)
\node (D) at (5,1) {D};
\draw (D.east) -- ++(1,0);
\end{tikzpicture}Coordinate Calculations
Use the calc library for mathematical coordinate operations:
\usetikzlibrary{calc}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\coordinate (C) at (4,3);
% Midpoint between two coordinates
\coordinate (M) at ($(A)!0.5!(B)$);
\fill[red] (M) circle (2pt);
% Point 25% of the way from A to C
\coordinate (P) at ($(A)!0.25!(C)$);
\fill[blue] (P) circle (2pt);
% Add coordinates
\coordinate (D) at ($(A) + (1,1)$);
% Scale a coordinate
\coordinate (E) at ($2*(B)$);
% Perpendicular projection
\coordinate (F) at ($(A)!(C)!(B)$); % C projected onto line AB
\draw (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}Intersection Points
Find where lines intersect using the intersections library:
\usetikzlibrary{intersections}
\begin{tikzpicture}
% Name the paths
\draw[name path=line1] (0,0) -- (3,2);
\draw[name path=line2] (0,2) -- (3,0);
% Find intersection and mark it
\fill[red, name intersections={of=line1 and line2}]
(intersection-1) circle (3pt);
% Circle and line intersection
\draw[name path=circle] (5,1) circle (1cm);
\draw[name path=line3] (4,0) -- (6,2);
\fill[blue, name intersections={of=circle and line3}]
(intersection-1) circle (2pt)
(intersection-2) circle (2pt);
\end{tikzpicture}Node Anchors
Nodes have predefined anchor points for precise connections:
\begin{tikzpicture}
\node[draw, rectangle, minimum width=2cm, minimum height=1cm] (box) at (0,0) {Box};
% Cardinal anchors
\fill[red] (box.north) circle (2pt);
\fill[red] (box.south) circle (2pt);
\fill[red] (box.east) circle (2pt);
\fill[red] (box.west) circle (2pt);
% Corner anchors
\fill[blue] (box.north east) circle (2pt);
\fill[blue] (box.north west) circle (2pt);
\fill[blue] (box.south east) circle (2pt);
\fill[blue] (box.south west) circle (2pt);
% Center
\fill[green] (box.center) circle (2pt);
% Connect from anchors
\draw[->] (box.east) -- ++(1,0);
\end{tikzpicture}Transformations
Apply transformations to coordinates:
\begin{tikzpicture}
% Original shape
\draw (0,0) rectangle (1,0.5);
% Shifted
\draw[xshift=2cm] (0,0) rectangle (1,0.5);
% Shifted in y
\draw[yshift=1cm] (0,0) rectangle (1,0.5);
% Rotated
\draw[rotate=45] (4,0) rectangle (5,0.5);
% Scaled
\draw[scale=1.5] (6,0) rectangle (6.5,0.25);
% Combined transformations
\draw[xshift=8cm, rotate=30, scale=1.2] (0,0) rectangle (1,0.5);
\end{tikzpicture}Canvas Transformations
Change the default coordinate system:
\begin{tikzpicture}[
x=1.5cm, % 1 unit in x = 1.5cm
y=1cm % 1 unit in y = 1cm
]
\draw[help lines] (0,0) grid (4,3);
\draw[thick] (0,0) -- (4,0) -- (4,3) -- (0,3) -- cycle;
\end{tikzpicture}
% Flipped y-axis (computer graphics style)
\begin{tikzpicture}[y=-1cm]
\draw (0,0) -- (0,2);
\node at (0,0) {Origin};
\node at (0,2) {Below};
\end{tikzpicture}Next Steps
Continue learning TikZ:
- Nodes and Labels - Position text and shapes
- Flowcharts - Use coordinates in diagrams
- Graphs - Network diagrams with coordinates