# spacing between tikz nodes

The following script

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}
\begin{tikzpicture}
\node(a)[minimum size = 3 cm, draw]
{top left};
\node(b)[minimum size = 3 cm, draw] at (a.south)[anchor=north]
{bottom left};
\node(c)[minimum size = 3 cm, draw] at (a.east)[anchor=west]
{top right};
\end{tikzpicture}
\end{document}


gives the output

How can I add automatic horizontal and vertical spacing between all nodes in a tikz picture?

• you can use positioning library – percusse May 4 '18 at 10:29
• @percusse Oops, sorry, didn't see your comment before I answered. I can always delete if you want to write up an answer yourself. – Torbjørn T. May 4 '18 at 10:34
• @TorbjørnT. No problem at all. It was just a passing comment – percusse May 4 '18 at 10:36

The most common method would be the positioning library. By using its <position> of=<other node> (see section 17.5.3 Advanced Placement Options in the manual for version 3.0.1a) for placing nodes, you can set the default distance with the node distance key.

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

\begin{document}
\begin{tikzpicture}[
every node/.append style={minimum size = 3 cm, draw},
node distance=2mm
]
\node(a) {top left};
\node(b) [below=of a] {bottom left};
\node(c) [right=of a] {top right};
\begin{tikzpicture}[
every node/.append style={minimum size = 3 cm, draw},
node distance=-\pgflinewidth
]
\node(a) {top left};
\node(b) [below=of a] {bottom left};
\node(c) [right=of a] {top right};
\end{tikzpicture}

\end{document}

• Is it possible to specify horizontal and vertical distance? – Viesturs May 4 '18 at 11:00
• @Viesturs Yes, node distance=2mm and 5mm will make the vertical distance 2mm and the horizontal distance 5mm. I do recommend carefully reading the section in the manual I referred to. – Torbjørn T. May 4 '18 at 11:05
• Can I use another node distance for a particular node locally? – Viesturs May 7 '18 at 14:50
• @Viesturs Use e.g. left=2cm of foo. If you have multiple nodes that should have a node distance, you can use a scope environment, \begin{scope}[node distance=2cm] <nodes go here> \end{scope}. – Torbjørn T. May 7 '18 at 16:37