[Tex/LaTex] Difference between “right of=” and “right=of” in PGF/TikZ

nodespositioningtikz-pgf

In the PGF/TikZ manual, sometimes I see the option right of=somenode instead of right=of somenode. They look very similar, but the effects are different. The distance between nodes positioned with the latter option is boundary-to-boundary, as stated in the manual. However, with the first option, it seems that the distance is shorter. I couldn't find any explanation of the first option in the manual.

My question is: what is the difference between the two options? Is there any explanation in the manual that I missed?

Best Answer

The right of key isn't described in the manual at all (at least I couldn't find it). In fact, those keys are deprecated. The file pgf/frontendlayer/tikz/tikz.code.tex contains the following code:

% The following are deprecated:
\tikzoption{above of}{\tikz@of{#1}{90}}%
\tikzoption{below of}{\tikz@of{#1}{-90}}%
\tikzoption{left of}{\tikz@of{#1}{180}}%
\tikzoption{right of}{\tikz@of{#1}{0}}%
\tikzoption{above left of}{\tikz@of{#1}{135}}%
\tikzoption{below left of}{\tikz@of{#1}{-135}}%
\tikzoption{above right of}{\tikz@of{#1}{45}}%
\tikzoption{below right of}{\tikz@of{#1}{-45}}%
\def\tikz@of#1#2{%
  \def\tikz@anchor{center}%
  \let\tikz@do@auto@anchor=\relax%
  \tikz@addtransform{%
    \expandafter\tikz@extract@node@dist\tikz@node@distance and\pgf@stop%
    \pgftransformshift{\pgfpointpolar{#2}{\tikz@extracted@node@distance}}}%
  \def\tikz@node@at{\pgfpointanchor{#1}{center}}}
\def\tikz@extract@node@dist#1and#2\pgf@stop{%
  \def\tikz@extracted@node@distance{#1}}

That is, the center of the new node is placed node distance away from the center anchor of the old node (where only the first number in node distance is used). You can see why this option is deprecated if you try a wide node:

\begin{tikzpicture}
  \node (a) {loooooooooooooooooooooooooooooong};
  \node[right of=a,font=\bfseries,blue] (b) {node b};
\end{tikzpicture}

example of incorrect positioning

On the other hand right=of would measure the node distance (defaulting to 1cm) from the east anchor of node a to the west anchor of node b:

example of correct positioning

Note that in order to use the right=of ⟨node⟩ syntax, you need to include the TikZ library positioning via \usetikzlibrary{positioning}.