14

Trying to answer to How to improve this picture? Or have a better idea?, I had a behavior I do not understand.

Please consider the following MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,calc}
\begin{document}
\begin{tikzpicture}[
    thick, node distance=1cm]
    \node (n1) {1};
    \node [below right of=n1] (n2) {2};
    \node [below right of=n2] (n3) {3};
    \node [right of=n3] (n4) {4};
    \node [above of=n4] (n5) {5};
    \node [above right of=n4] (n6) {6?};
    \begin{scope}[->]
        \draw (n1) -- (n2);
        \draw (n2) -- (n3);
        \draw (n3) -- (n4);
        \draw (n4) -- (n5);
        \draw[red] (n2) -- node[above]{???} (n5);
    \end{scope}
\end{tikzpicture}
\end{document}

and it gives as output:

output

I supposed that the node 5 (which is above of 4) would land at the same height than node 6 (which is above right of 4) and effectively at the same height than 2.

Why is this happening? I thought that maybe the positioning package is trying to put the node at the same distance between them... I tried to put the node above $\sqrt{2}$ but it still didn't work. Or am I just confused?

Is there some option to positioning to have the nodes on a grid?

Rmano
  • 40,848
  • 3
  • 64
  • 125

1 Answers1

18

There are two things to consider here:

  • You're using the old positioning syntax below right of = n1. With the positioning library, you should be using below right = of n1.
  • To get things to stay on a grid, you should set node distance=1cm and 1cm instead of just node distance=1cm. This specifies that the nodes should be placed 1cm apart both vertically and horizontally, not 1cm in Euclidean distance

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,calc}
\begin{document}
\begin{tikzpicture}[
    thick, node distance=0.7cm and 0.7cm]
    \node (n1) {1};
    \node [below right = of n1] (n2) {2};
    \node [below right = of n2] (n3) {3};
    \node [right = of n3] (n4) {4};
    \node [above = of n4] (n5) {5};
    \node [above right = of n4] (n6) {6?};
    \begin{scope}[->]
        \draw (n1) -- (n2);
        \draw (n2) -- (n3);
        \draw (n3) -- (n4);
        \draw (n4) -- (n5);
        \draw[red] (n2) -- node[above]{???} (n5);
    \end{scope}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • 1
    Thanks! I found out that also the option on grid has the same effect --- although with subtle differences (http://tex.stackexchange.com/questions/9386/difference-between-right-of-and-right-of-in-pgf-tikz?rq=1#comment443633_9391) – Rmano Apr 02 '16 at 17:46