16

I have a diagram of a few nodes and a few labels. I want the distance of each label from its respective node to be increased (to prevent it from overlapping with lines of the diagram). What is the correct syntax to do this?

\documentclass[tikz, border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw[step=1cm] (0,0) grid (5,1);
    \node[label=above:\rotatebox{-45}{king} ] at (0.5,0.5) {0};
    \node[label=above:\rotatebox{-45}{queen}] at (1.5,0.5) {1};
    \node[label=above:\rotatebox{-45}{man}  ] at (2.5,0.5) {0};
    \node[label=above:\rotatebox{-45}{woman}] at (3.5,0.5) {0};
    \node[label=above:\rotatebox{-45}{child}] at (4.5,0.5) {0};
\end{tikzpicture}
\end{document}

2 Answers 2

16

Setting label distance I think.

\documentclass[tikz, border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[label distance=5mm]
    \draw[step=1cm] (0,0) grid (5,1);
    \node[label=above:\rotatebox{-45}{king} ] at (0.5,0.5) {0};
    \node[label={[label distance=1cm]above:\rotatebox{-45}{queen}}] at (1.5,0.5) {1};
    \node[label=above:\rotatebox{-45}{man}  ] at (2.5,0.5) {0};
    \node[label=above:\rotatebox{-45}{woman}] at (3.5,0.5) {0};
    \node[label=above:\rotatebox{-45}{child}] at (4.5,0.5) {0};
\end{tikzpicture}
\end{document}

enter image description here

4
  • Ah, that's very useful. Do you know how the distance could be set for each label? For example, how could the label for "queen" be raised a bit higher than the others? Mar 2, 2017 at 12:22
  • @BlandCorporation See updated answer. (Note also the every label stuff wasn't necessary. Mar 2, 2017 at 12:25
  • Hey, thank you very much for your solution on that. Mar 2, 2017 at 15:02
  • From TikZ manual: "The distance is additionally inserted between the main node and the label node." So you can provide a negative distance as well (I would do it to make "queen" closer to its node). May 26, 2022 at 8:49
8

You can place the labels with numbers(for example 90 instead of above) and add a distance. With adding label distance=<...> like this:

\node[label={[label distance=2mm]90:\rotatebox{-45}{king}}]

See: Tikz manual http://vesta.informatik.rwth-aachen.de/ftp/pub/mirror/ctan/graphics/pgf/base/doc/pgfmanual.pdf

-> 3.9 Adding Labels Next to Nodes (p.52) it says: number instead of things like below left -> 17.10.2 The Label option label (p.340) : The is additionally inserted between the main node and the label node.

enter image description here

MWE:

\documentclass[tikz, border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw[step=1cm] (0,0) grid (5,1);
    \node[label={[label distance=2mm]90:\rotatebox{-45}{king}}] at (0.5,0.5) {0};
    \node[label={[label distance=2mm]90:\rotatebox{-45}{queen}}] at (1.5,0.5) {1};
    \node[label={[label distance=2mm]90:\rotatebox{-45}{man}}] at (2.5,0.5) {0};
    \node[label={[label distance=2mm]90:\rotatebox{-45}{woman}}] at (3.5,0.5) {0};
    \node[label={[label distance=2mm]90:\rotatebox{-45}{child}}] at (4.5,0.5) {0};
\end{tikzpicture}
\end{document}
1
  • Thank you very much for your solution and for your explanation of the syntax used for the labels. If I could accept two solutions, I would. Mar 2, 2017 at 15:04

You must log in to answer this question.

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