# TikZ: How does global/local [node distance] work?

The expected output of this MWE is two overlaying nodes (i.e. B and C).

However, \node [draw, right = 1cm of A] (B) {B}; successfully overrides the global setting of node distance=2cm, while \node [draw, right = of A, node distance = 1cm] (C) {C}; doesn't.

So, why do both syntaxes result in different outputs?

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}
\begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
\node [draw] (A) {A};
\node [draw, right = 1cm of A] (B) {B};
\node [draw, right = of A, node distance = 1cm] (C) {C};
\end{tikzpicture}
\end{document}


The ordering is important. The TikZ parser parses from left to right. This means that you need to first set the node distance (locally) to 1cm by saying node distance = 1cm, and then let TikZ compute the actual coordinates of the C node by saying right = of A.

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}
\begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
\node [draw] (A) {A};
\node [draw, right = 1cm of A] (B) {B};
\node [draw, node distance = 1cm, right = of A] (C) {C};
\end{tikzpicture}
\end{document}


As you can see, now the B and C nodes are on top of each other.

• I didn't know my question is that naive – Diaa Nov 15 '18 at 16:07
• I really appreciate your answer, but I think it is better to delete this question – Diaa Nov 15 '18 at 16:08
• @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.) – user121799 Nov 15 '18 at 16:09
• I mean it is a stupid question XD that doesn't deserve to be asked :) – Diaa Nov 15 '18 at 16:10
• @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;) – nidhin Nov 15 '18 at 19:34

The operation of the node distance key is particular and far from obvious. It only works if and only if there is an of part but no shift part.

I quote p 231 of 3.0.1a manual:

/tikz/node distance=<shifting part> (no default, initially 1cm and 1cm) The value of this key is used as is used if and only if a <of-part> is present, but no <shifting part>.

Look at this example.

• Nodes colored blue do not have a shifting part in their code, so the node distance key is active.
• The red colored nodes have a shifting part and therefore the node distance key is inactive.

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\foreach \i in {8,13,17}{
\begin{tikzpicture}[noeud/.style={draw,node distance=\i mm},
entre/.style={midway,draw=none,fill=white,inner sep =1pt}]
\draw[fill=green!10] (-1,-.1) rectangle (3,5);
\draw[help lines] (-1,-.1) grid (3,5);
% No shifting part
\begin{scope}[every node/.append style={fill=blue!20,font=\scriptsize}]
\node[noeud,align=center] (a1) at (0,0) {node distance\\ actived};
\node[noeud] (b1) [above=of a1] {1};
\node[noeud] (c1) [above=of b1] {2};
\draw [<->](a1)--(b1)node[entre]{\i mm};
\draw [<->](b1)--(c1)node[entre]{\i mm};
\end{scope}
% Shifting part
\begin{scope}[every node/.append style={fill=red!20,font=\scriptsize}]
\node[noeud,align=center] (a2) at (2,0) {node distance\\ inactived};
\node[noeud] (b2) [above=1cm of a2] {1};
\node[noeud] (c2) [above=1cm of b2] {2};
\draw [<->](a2)--(b2)node[entre]{1 cm};
\draw [<->](b2)--(c2)node[entre]{1 cm};
\end{scope}
\end{tikzpicture}
}
\end{document}


right = 1cm of A


The shifting part is present and equal to 1 cm. So the key distance node=2cm is disabled. Point B is therefore located 1 cm from A as you have specified.

Then for point C, you write this:

 right = of A, node distance = 1cm


As there is no shifting part in this code, the distance node=2cm key is active and therefore the node is placed at 2cm. Then you specify this distance again, but it has already been computed and so pgf does nothing more, as you can see with a 10 cm distance node key.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}
\begin{tikzpicture}[auto, node distance=2cm]
\node [draw] (A) {A};
\node [draw, right = 1cm of A] (B) {B};
\node [draw, right =of A,circle,fill=red!40,opacity=.5,inner sep =10pt] (B) {B};
\node [draw, right = of A, node distance = 10cm,fill=blue!40,opacity=.5,inner sep =10pt] (C) {C};
\node [draw, node distance = 3cm, right = of A,fill=violet,opacity=.5,inner sep =10pt] (D) {D};
\end{tikzpicture}
\end{document}


Translated with www.DeepL.com/Translator