Tx · LaTeX & TikZ

How to Adjust TikZ Node Distance for Individual Nodes

Published By the 9to5Science editorial team

At a glance

  1. 1. Load positioning

    Add the positioning library to use placements such as below=of and right=of.

  2. 2. Set each gap

    Give a distance in the placement option, such as below=8mm of first.

  3. 3. Use defaults selectively

    Set node distance for placements without an explicit distance.

  4. 4. Check what is measured

    With the positioning library, the distance is the gap between node borders.

TikZ’s node distance option provides a default spacing for nodes placed relative to one another. To change the spacing for just one placement, give its direction key an explicit length, as in below=8mm of A. Other placements can keep the default or use their own lengths.

Set a distance for one placement

Load TikZ’s positioning library, then write the distance in the relative-placement option. For example:

\node[below=8mm of first] (second) {Second};

This positions second below first with an 8 mm gap. You can use the same form with above, left, right, and diagonal directions such as below left. The length applies to that placement; it does not change the spacing setting for the rest of the picture.

With the positioning library, the specified distance is the gap between the node borders, not the distance between their centers. The center-to-center distance therefore also depends on the dimensions of the nodes.

Worked example

This complete example uses three different distances in one diagram. It requires the tikz package and its positioning library.

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

\begin{document}
\begin{tikzpicture}[
  every node/.style={draw, rounded corners, inner sep=3pt}
]
  \node (start) {Start};
  \node[below=6mm of start] (near) {6 mm below};
  \node[below=14mm of near] (far) {14 mm below};
  \node[right=18mm of start] (side) {18 mm right};
\end{tikzpicture}
\end{document}

The near node is 6 mm below start. The far node is 14 mm below near, and side is 18 mm to the right of start. Each length is local to its own placement, so changing one does not alter the others.

Combine explicit distances with a default

You can define a default distance for placements that do not specify one:

\begin{tikzpicture}[
  node distance=10mm,
  every node/.style={draw}
]
  \node (a) {A};
  \node[below=of a] (b) {Default gap};
  \node[below=4mm of b] (c) {Shorter gap};
\end{tikzpicture}

Here, below=of a uses the default 10 mm distance, while below=4mm of b sets a different distance for the next placement. This is often the simplest approach when most nodes share a regular layout but a few need extra or reduced spacing.

Common mistakes

  • Forgetting the library: below=of a and similar positioning syntax require \usetikzlibrary{positioning}. Without it, TikZ may report an unknown key.
  • Putting the length in the wrong place: Use below=8mm of a, not below of=8mm a. The direction key takes the length, followed by of and the reference node.
  • Expecting a center-to-center measurement: The positioning library uses the gap between node borders. Larger text or different node shapes can make center positions differ even when the border gap is the same.
  • Changing the default when only one gap should change: Setting node distance changes the default for placements that use it. For a one-off adjustment, put the distance directly in that placement key.
  • Mixing positioning styles unintentionally: Older syntax such as below of=a is different from the below=of a syntax provided by the positioning library. Use the latter when you want explicit distances such as below=8mm of a.

When to use coordinates instead

Relative positioning is convenient when your diagram is organized around named nodes. If you need an exact coordinate rather than a border-to-border gap, place a node at a coordinate such as \node at (2,1) {Text};. Coordinates are measured in the picture’s coordinate system and do not automatically account for the dimensions of another node.

References

  1. The TikZ & PGF Manual

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