# [Tex/LaTex] What are the ways one can control spacing between nodes in TikZ

tikz-pgf

If I have 4 nodes, one on top of each other, I would like to know the different ways to control the spacing between the nodes. I already know a few:

• \node[...below=1cm of xyz]
• \tikzstyle[...node distance = 2cm]
• \tikzstyle[...inner sep = 2pt]

Could someone provide me with a more comprehensive list of options.

In this case, specifically, I have a picture like so (work in progress)

I want to be able to control the big blue box so it is closer to the left and right borders than it is to the top and bottom. I also want to control the synapses so they move down such that synapse 3 is closer to the bottom than synapse 0 is to the top. I basically want full control of all four paddings. Here is the TikZ code used to generate the above image.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[remember picture,
simulator engine/.style={fill=black!10,rounded corners,inner sep=20pt},
topology/.style={rounded corners,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
neuron/.style={fill=blue!10,draw=blue,rounded corners,inner sep=15pt},
synapse/.style={draw=red!75,fill=red!20,rounded corners,inner sep=5pt},
empty synapse/.style={draw=blue!10,rounded corners,inner sep=5pt}
]
\node[simulator engine] (simulatorEngine) {
\begin{tikzpicture}
\node[topology] (topology1) {
\begin{tikzpicture}
\node [neuron] (neuron1-1)  {
\begin{tikzpicture}
\node [synapse,draw=blue] (synapse1-1-0) {$\text{Synapse}_{0}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-1-0] (synapse1-1-1) {$\text{Synapse}_{1}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-1-1] (synapse1-1-2) {$\text{Synapse}_{2}$};
\node [empty synapse,below=0.2cm of synapse1-1-2] (synapse1-1-e) {\ldots};
\node [synapse,draw=blue,below=0.1cm of synapse1-1-e] (synapse1-1-3) {$\text{Synapse}_{3}$};
\end{tikzpicture}
};
\node [neuron,draw=blue,right=0.2cm of neuron1-1] (neuron1-2) {
\begin{tikzpicture}
\node [synapse,draw=blue] (synapse1-2-0) {$\text{Synapse}_{0}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-2-0] (synapse1-2-1) {$\text{Synapse}_{1}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-2-1] (synapse1-2-2) {$\text{Synapse}_{2}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-2-2] (synapse1-2-3) {$\text{Synapse}_{3}$};
\end{tikzpicture}
};
\node [neuron,draw=blue,right=0.2cm of neuron1-2] (neuron1-3) {
\begin{tikzpicture}
\node [synapse,draw=blue] (synapse1-3-0) {$\text{Synapse}_{0}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-3-0] (synapse1-3-1) {$\text{Synapse}_{1}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-3-1] (synapse1-3-2) {$\text{Synapse}_{2}$};
\node [synapse,draw=blue,below=0.1cm of synapse1-3-2] (synapse1-3-3) {$\text{Synapse}_{3}$};
\end{tikzpicture}
};

\node [black,below] at (neuron1-1.north)  {$\text{Neuron}_{0}$};
\node [black,below] at (neuron1-2.north)  {$\text{Neuron}_{1}$};
\node [black,below] at (neuron1-3.north)  {$\text{Neuron}_{2}$};
\end{tikzpicture}
};

\end{tikzpicture}
};

\node [black,below] at (simulatorEngine.north) {Simulator Engine};
\end{tikzpicture}
\end{document}


control spacing between nodes ? good question for an exam about tikz !

1. to be or not to be a node ?

I suppose for the next part of the answer that the reasons to use "node" are fine (the first reason is to display some text). I think that you cannot consider that inner sep is a way to control spacing between nodes. For example, when line width grows, the line recovers inner sep. inner sep is a part of the object node. The two important parts of a node are the body (here the text) and the shape. The dimensions of the shape depends of the dimensions of the body, the value of inner sep, the value of line width. The definition of a node gives anchors. It's possible to use them to place the nodes.

2. Same dimensions ?

Before placing nodes, you need to know if all the nodes have the same shape with the same dimensions. In this case, it's possible to place automatically the nodes.

3. Absolute position

In the next example, I use absolute coordinates. It's interesting because, you can apply a scale option in this case. You need to give the exact dimensions of each node. The distance between the two last lists is 1.25 cm (I use a scale option). I get it with ($(\n.east)+(1,0)$). xshift=1cm is another possibility but in this case, the scale option doesn't work.

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[]{fourier} % I need to use €
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}

\begin{tikzpicture}
\foreach \n [count=\i from 0] in {A,B,C,D}
{\node [draw,minimum width =2cm,minimum height=2ex] at (0,\i*1 cm) {Text \n};  }
\end{tikzpicture}
\begin{tikzpicture} [scale=1.25]
\foreach \n [count=\i from 0] in {a,b,c,d}
{\node [draw,minimum width =2cm,minimum height=2ex] (\n) at (0,\i*1 cm) {Text \n};}
\foreach \n [count=\i from 0] in {a,b,c,d}
{\node [draw,minimum width =2cm,
minimum height=2ex,anchor=west] (\n) at at ($(\n.east)+(1,0)$) {Text \n\i};}
\end{tikzpicture}

\end{document}


4. relative position with anchors or with the positioning library

The letter is placed at (0,0) but an anchor is used to place one corner at the origin. Then a stamp is placed relatively at the upper right corner. The second stamp is placed relatively to the first. left=2mm of stamp1 replaces anchor=north east,shift={(-2mm,0mm)} (see the comment line).

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[]{fourier} % I need to use €
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[every node/.style=draw,scale=1.25]
\tikzset{stamp/.style={fill=blue!20,minimum width=1cm,minimum height=1.4cm,align=center}}
\node[fill=lightgray!15,minimum width=10cm,minimum height=6cm,align=left,anchor=south west] (letter) at (0,0)  {Mr AlterMundus\\Paris\\France};
\node [stamp,anchor=north east,shift={(-2mm,-2mm)}] (stamp1) at (letter.north east){2€};
%\node [stamp,anchor=north east,shift={(-2mm,0mm)}] (stamp2) at (stamp1.north west){2€};
\node[stamp,left=2mm of stamp1] {1€};
\end{tikzpicture}
\end{document}


5. relative position with the positioning library with(out) grid option

It's possible to use the positioning library to place nodes relatively to their centers and not to their borders. In the next example, i use the two possibilities

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[]{fourier} % I need to use €
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[every node/.style=draw,scale=1.25]
\tikzset{card/.style={fill=blue!20,minimum width=1cm,minimum height=1.4cm,align=center}}
\draw[fill=green!15!black!20,minimum width=10cm,minimum height=6cm,align=left,anchor=south west]   (0,0) rectangle (8,4) coordinate[pos=.5](O);
\node [above=.5cm,card,xshift=1cm] (card1) at (O){\Huge \textbf{V}};
\node[on grid,card,left=.6cm of card1] (card2){\Huge \textbf{D}};
\node[on grid,card,left=.6cm of card2] (card3){\Huge \textbf{R}};
\node[on grid,card,left=.6cm of card3] (card4){\Huge \textbf{A}};
\node [below=.5cm,,card,xshift=2cm] (card1) at (O){\Huge \textbf{V}};
\node[card,left=.5cm of card1] (card2){\Huge \textbf{D}};
\node[card,left=.5cm of card2] (card3){\Huge \textbf{R}};
\node[card,left=.5cm of card3] (card4){\Huge \textbf{A}};
\end{tikzpicture}
\end{document}


node distance

This is useful only to place nodes (relative method) without to precise the distance but it's a length and not a "way" to place node.

Conclusion If you need to control the spacing between nodes, you can use the positioning library ( with(out) "on grid" option, center to center or border to border) or you can also use the anchors and absolute coordinates.