139

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?

1
  • A bookkeeping comment: in the v2.10 manual the deprecated above right of= appears on page 192 in the last example of /tikz/swap.
    – equaeghe
    Commented Oct 14, 2012 at 9:26

1 Answer 1

141

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}.

3
  • 7
    By default, positioning measures the distance between the borders of the nodes. But, with the on grid option, it measures the distance between the centers of the nodes. Commented Jul 19, 2014 at 7:33
  • 6
    WARNING: Using the positioning library will render your images unscalable (if no explicitly nodes are present). This is a huge drawback if tikz is used with tikzscale and should therefore be mentioned...
    – Dux
    Commented Feb 22, 2016 at 9:57
  • @Dux I've not had that problem, though I don't use tikzscale extensively.
    – cfr
    Commented Jun 26, 2017 at 17:31

You must log in to answer this question.

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