5

I was drawing a small diagram with tikz, and I noticed there is a small discrepancy on the position of the nodes according to how you define their position.

Here's the MWE:

\documentclass{article}

\usepackage{amssymb}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[node distance=5mm and 5mm]

    %bottom row
    \node (2) {2};
    \path (2) ++(170:8) node (3) {3};

    %second row
    \node[above=of 2] (4) {4}; 
    \path (4) ++(170:6) node (5) {5}%
              ++(170:2) node (a) {6};
    \node[above=of 3] (6) {6};

    %third row
    \node[above=of 4] (7) {7};
    \path (7) ++(170:6) node (b) {$\mathbb{N}^3$}%
              ++(170:2) node (d) {3'};
    \node[above=of 5] (N) {$\mathbb{N}^3$}; 
    \node[above=of 6] (3') {3'}; 

    %top row
    \node[above=of 7] (2'') {2''}; 

    \path (2'') ++(170:2) node (C) {C}%
                ++(170:2) node (D) {D}%
                ++(170:2) node (A) {A}%
                ++(170:2) node (6') {6'};
    \node[above=of N] (A) {A};
    \node[above=of 3'] (c) {6'};
\end{tikzpicture}
\end{document}

Here's the output:

enter image description here

I am basically drawing a 4x5 parallelogram. I am drawing some nodes twice (6',3',6, A, and \mathbb{N}^3), once using the path syntax and once using above=of. I would expect them to be placed in the same position (or very close, as it happens for 6 and 3'). However, the discrepancy is a bit too much in the top row to be due to some numerical approximation of the pixels. It seems to me that there is some sort of shift, can anybody explain to me what causes it?



EDIT: This is a simplified version of the above code, where I keep only 2 of the 4 rows, and compactified the picture. I am keeping the old code and picture because it seems to me that the shift is larger in the top row (see the letter A). But the same phenomenon happens here, so I guess they have the same explanation.

\documentclass{article}

\usepackage{amssymb}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}



\begin{tikzpicture}[node distance=5mm and 5mm]

    \node (4) {4}; 
    \path (4) ++(170:2) node (5) {5}%
                ++(170:2) node (6) {6};
    

    \node[above=of 4] (7) {7};
    \path (7) ++(170:2) node (b) {$\mathbb{N}^3$}%
                ++(170:2) node (d) {3'};
    \node[above=of 5] (N) {$\mathbb{N}^3$}; 
    \node[above=of 6] (3') {3'}; 
\end{tikzpicture}

\end{document}

Here's the output:

enter image description here

2
  • Could you please simplify the example so that we have just the minimum nodes that show the discrepancy? I am, two of them? Like it is now it's quite hard digging it out. I imagine that the problem is that sometimes you use the border anchor and sometimes you use a center anchor, but I have not time to explore now... Thanks!
    – Rmano
    May 24 at 7:49
  • @Rmano done, I hope it's clearer now
    – Manlio
    May 24 at 8:11

1 Answer 1

8

It's caused by the default setting on grid=false. You can read its doc (https://tikz.dev/tikz-shapes#pgf./tikz/on:grid) for more info.

In

    \path (7) ++(170:2) node (b) {$\mathbb{N}^3$}%
                ++(170:2) node (d) {3'};

The distance between centers of nodes 7 and b is 2cm.

But with on grid=false, \node[above=of 5] (N) {$\mathbb{N}^3$}; puts node N above of node 5 so that the "outer" distance (distance between south anchor of N and north anchor of 5) is 5mm.

Therefore when the total height (height + depth) of node text changes, the position differences changes too. In OP's example, the height of $\mathbb{N}^3$ is larger than that of 3', and the height of 3' is larger than 7.

\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\verb|on grid=false|\par
\begin{tikzpicture}[node distance=5mm and 5mm, on grid=false] % default setting
    \node (4) {4}; 
    \path (4) ++(170:2) node (5) {5}
                ++(170:2) node (6) {6};
    
    \node[above=of 4] (7) {7};
    \path (7) ++(170:2) node (b) {$\mathbb{N}^3$}%
                ++(170:2) node (d) {3'};
    
    \node[above=of 5, blue] (N) {$\mathbb{N}^3$}; 
    \node[above=of 6, blue] (3') {3'}; 
\end{tikzpicture}

\verb|on grid=true|\par
\begin{tikzpicture}[node distance=5mm and 5mm, on grid=true]
    \node (4) {4}; 
    \path (4) ++(170:2) node (5) {5}
                ++(170:2) node (6) {6};

    \node[above=of 4] (7) {7};
    \path (7) ++(170:2) node (b) {$\mathbb{N}^3$}%
                ++(170:2) node (d) {3'};
    
    \node[above=of 5, blue] (N) {$\mathbb{N}^3$}; 
    \node[above=of 6, blue] (3') {3'}; 
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.