Tx · LaTeX & TikZ

How to Use Multiple Relative Positions in TikZ

Published By the 9to5Science editorial team

At a glance

  1. 1. Load positioning

    Add the positioning library before using keys such as right=of or below right=of.

  2. 2. Name reference nodes

    Give each node a name so later nodes can refer to it.

  3. 3. Chain placements

    Place each new node relative to a previously defined node.

  4. 4. Combine directions

    Use below right=of for diagonal placement, or calc for relationships involving two nodes.

Advertisement

TikZ relative positioning lets you describe a diagram in terms of named nodes instead of calculating coordinates by hand. The basic syntax is a positioning option such as right=of A, where A is the name of an earlier node.

For ordinary diagrams, you can create several relative positions by chaining these instructions. For diagonal positions or positions based on two references, use TikZ’s combined direction keys or the calc library.

Load the required libraries

The right=of, below=of, and similar keys are provided by the positioning library. If you need expressions such as a midpoint or an offset from a coordinate, also load calc.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture}[
  every node/.style={draw, rounded corners, minimum width=12mm, minimum height=8mm},
  node distance=12mm
]
  \node (a) {A};
  \node[right=of a] (b) {B};
  \node[below=of b] (c) {C};
  \node[left=of c] (d) {D};

  \draw[->] (a) -- (b);
  \draw[->] (b) -- (c);
  \draw[->] (c) -- (d);
  \draw[->] (d) -- (a);
\end{tikzpicture}

\end{document}

The node name is declared in parentheses, as in (a). A later node can then refer to that name with right=of a or another positioning key.

Chain several relative positions

A placement can refer to any node that has already been defined, not only the immediately preceding node. This makes it possible to build rows, columns, trees, and flowcharts step by step:

\begin{tikzpicture}[
  every node/.style={draw, minimum width=15mm},
  node distance=8mm
]
  \node (start) {Start};
  \node[right=of start] (input) {Input};
  \node[right=of input] (test) {Test};
  \node[below=of test] (stop) {Stop};
  \node[left=of stop] (retry) {Retry};

  \draw[->] (start) -- (input);
  \draw[->] (input) -- (test);
  \draw[->] (test) -- (stop);
  \draw[->] (test) -- (retry);
\end{tikzpicture}

The default distance comes from node distance. You can override it for one placement:

\node[right=18mm of start] (input) {Input};
\node[below=6mm of input] (next) {Next};

The distance is measured between the relevant borders of the nodes, rather than between their centers. This usually gives better-looking diagrams when node sizes differ.

Use a combined direction for diagonal placement

If a node should be both below and to the right of another node, use one combined key:

\node (a) {A};
\node[below right=of a] (b) {B};

Other useful combined directions are above right, above left, and below left.

You can also set a specific diagonal distance:

\node[below right=10mm and 15mm of a] (b) {B};

The two distances specify the separations in the two directions. If exact horizontal and vertical spacing matters, verify the result with your document’s TikZ version and node dimensions; node borders and anchors affect the visible gap.

Do not stack unrelated placement keys

A tempting construction is:

% Avoid relying on this as a two-node constraint:
\node[right=of a, below=of b] (c) {C};

This gives two placement instructions for one node, but it does not generally express a robust geometric constraint involving both a and b. The keys are processed as placement operations, and their interaction can be confusing or depend on the order in which they are applied.

If the intended position is diagonal from one node, write the intention directly:

\node[below right=of a] (c) {C};

If the position genuinely depends on two nodes, use a coordinate calculation instead.

Position a node using two references

The calc library provides interpolation syntax. For example, this places m halfway between a and b:

\begin{tikzpicture}[
  every node/.style={draw, circle, inner sep=2pt}
]
  \node (a) at (0,0) {A};
  \node (b) at (4,1) {B};
  \node (m) at ($(a)!0.5!(b)$) {M};

  \draw (a) -- (b);
\end{tikzpicture}

The expression ($(a)!0.5!(b)$) means the point 50 percent of the way from a to b. More generally, !0.25! places a point one quarter of the way from the first reference to the second.

You can add an offset to a calculated position:

\node (c) at ($(a)!0.5!(b)+(0,-8mm)$) {C};

Here C is below the midpoint of A and B. For precise edge-to-edge placement, use anchors such as a.east, b.west, or a.north in the expression:

\node (c) at ($(a.east)!0.5!(b.west)$) {C};

Common mistakes

Forgetting the library

Without

\usetikzlibrary{positioning}

TikZ does not know the right=of and below=of keys. It may report an unknown key or interpret the option incorrectly.

Referring to a node too early

The reference node must already exist when TikZ processes the placement. Define a before writing right=of a.

Confusing node names and displayed text

In \node (input) {Input};, input is the internal name and Input is the text shown in the diagram. Later references use (input), not the displayed word.

Using right of= with modern syntax

The older syntax right of=a is associated with the older positioning conventions and is less flexible. Prefer the current form:

\node[right=of a] (b) {B};

For multiple relative positions, use chained named nodes for one-reference relationships, combined direction keys for diagonals, and calc expressions when two or more references determine the position.

References

  1. PGF/TikZ Manual: Positioning Nodes
  2. PGF/TikZ Manual: Coordinate Calculations

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.