# TikZ: What affects the space between nodes placed with below of= syntax?

How does TikZ decide how much space to put between these two nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (a) {A};
\node [below of=a] (b) {B};
\draw (a.north west) rectangle (a.south east);
\draw (b.north west) rectangle (b.south east);
\end{tikzpicture}
\end{document}


And how can I change it? I've added some rectangles to illustrate the extent of the nodes.

I can, of course, have more control as follows:

\begin{tikzpicture}
\node (a) {A};
\node at (a.south) [anchor=north] (b) {B};
\draw (a.north west) rectangle (a.south east);
\draw (b.north west) rectangle (b.south east);
\end{tikzpicture}


And then just add whatever space I want to (a.south) but this is less semantic and much more fiddly.

There must be a way to globally change the space between nodes placed with the positioning library...

To clarify, I'd ideally like to specify the separation between node rather than the distance between node centres. This is because I have nodes of varying y-height and I'd like to have them have a uniform distance between them.

-

You are loading the positioning library but then use below of=..., however the library provides the syntax below=<optional length> of ..., while below of is already provided by the TikZ core.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (a) {A};
\node [below=1cm of a] (b) {B};
\draw (a.north west) rectangle (a.south east);
\draw (b.north west) rectangle (b.south east);
\end{tikzpicture}
\end{document}

-
Aha. And this specifies distance between nodes rather than distance between node centres. Excellent! – Seamus Aug 14 '11 at 14:18

Here is something you may use: the \tikzset command and the node distance option. Note that I drew the rectangles by specifying the node shape and telling tikz to draw the shape.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{my node/.style={node distance=1cm,shape=rectangle}}

\begin{document}

\begin{tikzpicture}
\node[draw=red,my node] (a) {A};
\node[draw=blue,my node,below=of a] (b) {B};
\end{tikzpicture}

\end{document}


You may also specify the node distance at the beginning of a picture or a particular node : \tikzpicture[node distance=1cm] or \node[node distance=1cm]

-
I knew there had to be a better way to draw round a node! node distance is the distance between the centres of the nodes. Is there a way to specify the separation between nodes? That is, the distance between, say a.south and b.north? – Seamus Aug 14 '11 at 13:50
You can adjust this with inner sep and outer sep (the space added within and outside the border, respectively) or by setting a minimum width or minimum height, depending on the effect you're looking for. – Ant Aug 14 '11 at 21:35

The answer is in the manual : 16.5.3 Advanced Placement Options (CVS version of pgfmanual but I think you can find this paragraph in the classic manual).

The example gives in the manual is : ((see Martin's answer)

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{%
arrows,
positioning
}

\begin{document}

\begin{tikzpicture}[every node/.style=draw,scale=2]
\draw[help lines] (0,0) grid (2,2);
\node (some node) at (1,1) {some node};
\node (other node) [above=1cm of some node] {\tiny above=1cm of some node};
\draw [<->] (some node.north) -- (other node.south)
node [midway,right,draw=none] {1cm};
\end{tikzpicture}

\end{document}


Important :

When this library is loaded, the options like above or above left behave differently. With the positioning library loaded, the above option does not take a simple ⟨dimension⟩ as its parameter. Rather, it can (also) take a more elaborate ⟨specification⟩ as parameter. This ⟨specification⟩ has the following general form: It starts with an optional ⟨shifting part⟩ and is followed by an optional ⟨of-part⟩.

Remark : You can instead use the distance between the nodes, use the distances between the centers : You need to do this use the option on grid. The manual gives

\begin{tikzpicture}[every node/.style=draw]
\draw[help lines] (0,0) grid (2,3);
%  Not gridded
\node (a1) at (0,0) {not gridded};
\node (b1) [above=1cm of a1] {fooy};
\node (c1) [above=1cm of b1] {a};
%  gridded
\node (a2) at (2,0) {gridded};
\node (b2) [on grid,above=1cm of a2] {fooy};
\node (c2) [on grid,above=1cm of b2] {a};
\end{tikzpicture}


But another possibility is to use node distance (see Frederic's answer). The syntax is different above=of ...

\begin{tikzpicture}[every node/.style=draw,node distance=5mm]
\draw[help lines] (0,0) grid (2,3);
%  Not gridded
\node (a1) at (0,0) {not gridded};
\node (b1) [above=of a1] {fooy};
\node (c1) [above=of b1] {a};
%  gridded
\begin{scope}[on grid]
\node (a2) at (2,0) {gridded};
\node (b2) [above=of a2] {fooy};
\node (c2) [above=of b2] {a};
\end{scope}
\end{tikzpicture}


Last remark: I don't like positioning the first argument : I use Tikz from the first version and positioning redefines some options. The second argument is : it's not easy to scale a picture that uses positioning and node distance.

-