3

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

enter image description here

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

3
  • 2
    you can use positioning library
    – percusse
    Commented May 4, 2018 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. Commented May 4, 2018 at 10:34
  • @TorbjørnT. No problem at all. It was just a passing comment
    – percusse
    Commented May 4, 2018 at 10:36

1 Answer 1

5

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.

enter image description here

\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};
\end{tikzpicture}\quad%
\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}
4
  • Is it possible to specify horizontal and vertical distance?
    – Viesturs
    Commented May 4, 2018 at 11:00
  • 1
    @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. Commented May 4, 2018 at 11:05
  • Can I use another node distance for a particular node locally?
    – Viesturs
    Commented May 7, 2018 at 14:50
  • 1
    @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}. Commented May 7, 2018 at 16:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .