2

It seems that the distance between a node and its label depends on the way the label is inserted:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw [help lines] (0,0) grid (4,4);
        \node[above]  at (1,1) {A};
        \node[label=above:B] at (1,1) {};   
        \node[left]  at (3,2) {C};
        \node[label=left:D] at (3,2) {};    
    \end{tikzpicture}
\end{document}

Which creates this:enter image description here

If I specify the label distance option, it seems only B and D are affected.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[label distance=3mm]
        \draw [help lines] (0,0) grid (4,4);
        \node[above]  at (1,1) {A};
        \node[label=above:B] at (1,1) {};   
        \node[left]  at (3,2) {C};
        \node[label=left:D] at (3,2) {};    
    \end{tikzpicture}
\end{document}

enter image description here

I am aware that, for instance, \node[left] implicity creates a new node to the left of the coordinates specified. How can I control the distance at which this node is created, so that whichever syntax is used, the distance is the same?

  • 1
    Why are you not using \node[above, label=above:B] at (1,1) {A};? – Torbjørn T. Jun 5 '18 at 15:46
  • 1
    you should not mix node distance and label distance. for example, node above some coordinate has distance from this coordinate equal to outer sep, label above some node at the same coordinated is above node border. for illustration compare \node[label=A] at (1,1) {}; and \coordinate[label=A] (A)( at (1,1);. or i miss the point of your question? – Zarko Jun 5 '18 at 18:17
3

The difference comes from the fact that a \node is not a zero-size object. It has an inner sep, which by default is 0.333em, so the width/height of an empty, default node, is 0.666em. This becomes obvious if you add draw to the node options.

If you use a \coordinate[label=.. instead of a \node on the other hand, you get the same result. A coordinate is a special type of node used to give names to coordinates for reuse later, and where the size is zero, and all the anchors point to the center anchor.

The square in the image below is the node to which a label has been added. You can see where the difference comes from. What above does is just set anchor=south. But for the node the south anchor is placed at the specified coordinate, for the label the south anchor (of the label node) is placed at the north anchor of the parent node, which is centered on that same coordinate.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw [help lines] (0,0) grid (4,4);
        \node[draw,label=above:A] (a) at (1,2) {};
        \node[above] at (a) {A};
        \node[draw,label=left:C] (c) at (2,2) {};
        \node[left] at (c) {C};

        \coordinate[label=above:A] (a) at (1,1);
        \node[above] at (a) {A};
        \coordinate[label=left:C] (c) at (2,1);
        \node[left] at (c) {C};
\end{tikzpicture}
\end{document}

It doesn't look like you can globally set the offset for right/above/etc., so if you've set label distance=3mm and want the same offset for a normal node, you need e.g. above=3mm.

Or you can load the positioning library and set above=of <named coordinate>, then you can set the default distance with node distance, like you did with label distance. Example:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
    \begin{tikzpicture}[
        label distance=3mm,
        node distance=3mm
     ]
        \draw [help lines] (0,0) grid (4,4);

        % all three As end up in the same place
        \coordinate[label=above:A] (a) at (1,2);
        \node[above=of a] {A}; % positioning library
        \node[above=3mm] at (a) {A}; % standard above

        % same thing here, all Cs are in the same place
        \coordinate[label=left:C] (c) at (2,2);
        \node[left=of c] {C};
        \node[left=3mm] at (c) {C};

\end{tikzpicture}
\end{document}
  • Sorry, perhaps my example wasn't clear. I'm not trying to have a node labelled "A", then an extra label "B" above. I was just trying to show two different ways that I have seen used to add a label to a coordinate point, say, (1,1); and to show that the two methods do not yield the same result; and trying to understand why, and how to get the same results. If I can reformulate my question, it is: why doesn't \node[above] at (1,1) {A}; yields the same result as \node[label=above:A] at (1,1) {};. And how can we make it do? – Peutch Jun 5 '18 at 17:47
  • @Peutch Right, will rewrite my answer. – Torbjørn T. Jun 5 '18 at 17:48
  • 1
    @Peutch More useful now? – Torbjørn T. Jun 5 '18 at 18:12

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.