Text Positioning in TikZ
Advanced techniques for positioning text and labels in TikZ diagrams.
Basic Position Keywords
TikZ provides intuitive keywords for common positions:
\begin{tikzpicture}
\fill (0,0) circle (3pt);
% Cardinal positions
\node[above] at (0,0) {above};
\node[below] at (0,0) {below};
\node[left] at (0,0) {left};
\node[right] at (0,0) {right};
% Diagonal positions
\fill (4,0) circle (3pt);
\node[above left] at (4,0) {above left};
\node[above right] at (4,0) {above right};
\node[below left] at (4,0) {below left};
\node[below right] at (4,0) {below right};
\end{tikzpicture}Distance Control
Control the distance between text and reference point:
\begin{tikzpicture}
\fill (0,0) circle (3pt);
% Default distance
\node[above] at (0,0) {default};
% Custom distance
\node[above=5mm] at (2,0) {5mm above};
\node[above=1cm] at (4,0) {1cm above};
% Diagonal with distance
\node[above right=5mm and 8mm] at (6,0) {offset};
\end{tikzpicture}Anchor-Based Positioning
Use anchors for precise control over where text is placed:
\begin{tikzpicture}
\draw[help lines] (0,0) grid (6,3);
% anchor determines which part of text is at the coordinate
\node[anchor=south west] at (0,0) {SW anchor at origin};
\node[anchor=center] at (3,1.5) {Centered};
\node[anchor=north east] at (6,3) {NE anchor at corner};
% Same as using position keywords:
\node[anchor=south] at (3,0) {= above}; % south anchor = above position
\end{tikzpicture}Labels on Paths
Position text along lines and curves:
\begin{tikzpicture}
% At the end
\draw (0,0) -- (4,0) node[right] {end};
% At the start
\draw (0,-1) node[left] {start} -- (4,-1);
% In the middle
\draw (0,-2) -- node[above] {midway} (4,-2);
% At specific position (0 = start, 1 = end)
\draw (0,-3) -- (4,-3);
\node[above] at (1,-3) {pos=0.25};
\node[below] at (3,-3) {pos=0.75};
% Using pos option
\draw (0,-4) -- node[pos=0.2, above] {20\%}
node[pos=0.8, below] {80\%} (4,-4);
\end{tikzpicture}Sloped Text
Make text follow the angle of a path:
\begin{tikzpicture}
% Sloped text follows the line angle
\draw (0,0) -- node[sloped, above] {Sloped Text} (4,2);
% Sloped text on curved path
\draw (0,3) to[bend left] node[sloped, above] {Curved Sloped} (4,3);
% Allow upside down (for steep angles)
\draw (5,0) -- node[sloped, allow upside down, above] {Steep} (6,3);
% Midway sloped
\draw (0,-1) -- node[midway, sloped, above] {midway sloped} (4,1);
\end{tikzpicture}Rotated Text
Rotate text to any angle:
\begin{tikzpicture}
% Rotate by degrees
\node[rotate=45] at (0,0) {45 degrees};
\node[rotate=90] at (2,0) {90 degrees};
\node[rotate=-30] at (4,0) {-30 degrees};
% Rotate around specific anchor
\node[rotate=45, anchor=west] at (6,0) {Rotated at west anchor};
% Combined with other styles
\node[draw, rotate=30, fill=yellow!20] at (8,0) {Styled \& Rotated};
\end{tikzpicture}Text Alignment
Control multi-line text alignment:
\begin{tikzpicture}
% Left aligned
\node[draw, text width=3cm, align=left] at (0,0) {
Left aligned text that spans multiple lines
};
% Center aligned
\node[draw, text width=3cm, align=center] at (4,0) {
Center aligned text that spans multiple lines
};
% Right aligned
\node[draw, text width=3cm, align=right] at (8,0) {
Right aligned text that spans multiple lines
};
% Justified
\node[draw, text width=4cm, align=justify] at (4,-3) {
Justified text will stretch to fill the full width on each line except the last.
};
\end{tikzpicture}Labels with Arrows
Position labels on arrows and annotate diagrams:
\begin{tikzpicture}
\node[draw, circle] (A) at (0,0) {A};
\node[draw, circle] (B) at (4,0) {B};
\node[draw, circle] (C) at (2,2) {C};
% Label on arrow
\draw[->] (A) -- node[above] {5} (B);
% Label with background
\draw[->] (A) -- node[fill=white] {10} (C);
% Multiple labels
\draw[->] (B) -- node[above right] {cost}
node[below right] {3} (C);
% Auto positioning
\draw[->] (A) to[bend left] node[auto] {auto} (C);
\draw[->] (C) to[bend left] node[auto, swap] {swap} (A);
\end{tikzpicture}Pin and Label
Add labels that point to nodes:
\begin{tikzpicture}
% Pin: label with a line pointing to the node
\node[draw, circle, pin=60:Label] at (0,0) {A};
% Pin with distance
\node[draw, circle, pin={[pin distance=1cm]90:Far Label}] at (3,0) {B};
% Multiple pins
\node[draw, circle,
pin=45:Top Right,
pin=-45:Bottom Right
] at (6,0) {C};
% Styled pin
\node[draw, circle,
pin={[pin edge={thick, red, ->}]180:Styled}
] at (9,0) {D};
\end{tikzpicture}Fit Library for Grouping
Create labels for groups of nodes:
\usetikzlibrary{fit, backgrounds}
\begin{tikzpicture}
\node[draw, circle] (a) at (0,0) {A};
\node[draw, circle] (b) at (1,0) {B};
\node[draw, circle] (c) at (0.5,1) {C};
% Fit a node around others
\begin{scope}[on background layer]
\node[draw, dashed, rounded corners, fit=(a)(b)(c),
label=above:Group] {};
\end{scope}
\end{tikzpicture}Next Steps
Continue learning TikZ:
- Annotations - Callouts and explanatory text
- Flowcharts - Apply positioning to diagrams
- Graphs - Label network diagrams
Text Positioning in TikZ
Advanced techniques for positioning text and labels in TikZ diagrams.
Basic Position Keywords
TikZ provides intuitive keywords for common positions:
\begin{tikzpicture}
\fill (0,0) circle (3pt);
% Cardinal positions
\node[above] at (0,0) {above};
\node[below] at (0,0) {below};
\node[left] at (0,0) {left};
\node[right] at (0,0) {right};
% Diagonal positions
\fill (4,0) circle (3pt);
\node[above left] at (4,0) {above left};
\node[above right] at (4,0) {above right};
\node[below left] at (4,0) {below left};
\node[below right] at (4,0) {below right};
\end{tikzpicture}Distance Control
Control the distance between text and reference point:
\begin{tikzpicture}
\fill (0,0) circle (3pt);
% Default distance
\node[above] at (0,0) {default};
% Custom distance
\node[above=5mm] at (2,0) {5mm above};
\node[above=1cm] at (4,0) {1cm above};
% Diagonal with distance
\node[above right=5mm and 8mm] at (6,0) {offset};
\end{tikzpicture}Anchor-Based Positioning
Use anchors for precise control over where text is placed:
\begin{tikzpicture}
\draw[help lines] (0,0) grid (6,3);
% anchor determines which part of text is at the coordinate
\node[anchor=south west] at (0,0) {SW anchor at origin};
\node[anchor=center] at (3,1.5) {Centered};
\node[anchor=north east] at (6,3) {NE anchor at corner};
% Same as using position keywords:
\node[anchor=south] at (3,0) {= above}; % south anchor = above position
\end{tikzpicture}Labels on Paths
Position text along lines and curves:
\begin{tikzpicture}
% At the end
\draw (0,0) -- (4,0) node[right] {end};
% At the start
\draw (0,-1) node[left] {start} -- (4,-1);
% In the middle
\draw (0,-2) -- node[above] {midway} (4,-2);
% At specific position (0 = start, 1 = end)
\draw (0,-3) -- (4,-3);
\node[above] at (1,-3) {pos=0.25};
\node[below] at (3,-3) {pos=0.75};
% Using pos option
\draw (0,-4) -- node[pos=0.2, above] {20\%}
node[pos=0.8, below] {80\%} (4,-4);
\end{tikzpicture}Sloped Text
Make text follow the angle of a path:
\begin{tikzpicture}
% Sloped text follows the line angle
\draw (0,0) -- node[sloped, above] {Sloped Text} (4,2);
% Sloped text on curved path
\draw (0,3) to[bend left] node[sloped, above] {Curved Sloped} (4,3);
% Allow upside down (for steep angles)
\draw (5,0) -- node[sloped, allow upside down, above] {Steep} (6,3);
% Midway sloped
\draw (0,-1) -- node[midway, sloped, above] {midway sloped} (4,1);
\end{tikzpicture}Rotated Text
Rotate text to any angle:
\begin{tikzpicture}
% Rotate by degrees
\node[rotate=45] at (0,0) {45 degrees};
\node[rotate=90] at (2,0) {90 degrees};
\node[rotate=-30] at (4,0) {-30 degrees};
% Rotate around specific anchor
\node[rotate=45, anchor=west] at (6,0) {Rotated at west anchor};
% Combined with other styles
\node[draw, rotate=30, fill=yellow!20] at (8,0) {Styled \& Rotated};
\end{tikzpicture}Text Alignment
Control multi-line text alignment:
\begin{tikzpicture}
% Left aligned
\node[draw, text width=3cm, align=left] at (0,0) {
Left aligned text that spans multiple lines
};
% Center aligned
\node[draw, text width=3cm, align=center] at (4,0) {
Center aligned text that spans multiple lines
};
% Right aligned
\node[draw, text width=3cm, align=right] at (8,0) {
Right aligned text that spans multiple lines
};
% Justified
\node[draw, text width=4cm, align=justify] at (4,-3) {
Justified text will stretch to fill the full width on each line except the last.
};
\end{tikzpicture}Labels with Arrows
Position labels on arrows and annotate diagrams:
\begin{tikzpicture}
\node[draw, circle] (A) at (0,0) {A};
\node[draw, circle] (B) at (4,0) {B};
\node[draw, circle] (C) at (2,2) {C};
% Label on arrow
\draw[->] (A) -- node[above] {5} (B);
% Label with background
\draw[->] (A) -- node[fill=white] {10} (C);
% Multiple labels
\draw[->] (B) -- node[above right] {cost}
node[below right] {3} (C);
% Auto positioning
\draw[->] (A) to[bend left] node[auto] {auto} (C);
\draw[->] (C) to[bend left] node[auto, swap] {swap} (A);
\end{tikzpicture}Pin and Label
Add labels that point to nodes:
\begin{tikzpicture}
% Pin: label with a line pointing to the node
\node[draw, circle, pin=60:Label] at (0,0) {A};
% Pin with distance
\node[draw, circle, pin={[pin distance=1cm]90:Far Label}] at (3,0) {B};
% Multiple pins
\node[draw, circle,
pin=45:Top Right,
pin=-45:Bottom Right
] at (6,0) {C};
% Styled pin
\node[draw, circle,
pin={[pin edge={thick, red, ->}]180:Styled}
] at (9,0) {D};
\end{tikzpicture}Fit Library for Grouping
Create labels for groups of nodes:
\usetikzlibrary{fit, backgrounds}
\begin{tikzpicture}
\node[draw, circle] (a) at (0,0) {A};
\node[draw, circle] (b) at (1,0) {B};
\node[draw, circle] (c) at (0.5,1) {C};
% Fit a node around others
\begin{scope}[on background layer]
\node[draw, dashed, rounded corners, fit=(a)(b)(c),
label=above:Group] {};
\end{scope}
\end{tikzpicture}Next Steps
Continue learning TikZ:
- Annotations - Callouts and explanatory text
- Flowcharts - Apply positioning to diagrams
- Graphs - Label network diagrams