2

When making a graph in TikZ where the labels of some nodes have indices and the labels of others do not, there is a very ugly effect where TikZ tries to center the entire label instead of the baseline, because each node is assigned its own baseline.

You can see what I mean in the following example:

\documentclass[varwidth]{standalone}

\usepackage{tikz}
\usepackage{amssymb}

\begin{document}
Ti\textit{k}Z gives each node its own baseline for text:
 \begin{center}
   \tikz{
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};

     \node[draw, circle, minimum size=24pt] (1) at (1,0) {\(v\)};
     \node[draw, circle, minimum size=24pt] (2) at (2,0) {\(v_{1}\)};
   }
   \tikz{
     \node (a) at (0,0) {}; \node (b) at (3,0) {};
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};
     \path[draw=red] (a) to (b);
     \path[draw=red] (y1) to (y2);
     \path[draw=red] (y3) to (y4);

     \node[draw, circle, minimum size=24pt] (1) at (1,0) {\(v\)};
     \node[draw, circle, minimum size=24pt] (2) at (2,0) {\(v_{1}\)};
   }
 \end{center}
 The effect is more obvious with squares:
 \begin{center}
   \tikz{
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};

     \node[draw, circle, minimum size=24pt] (1) at (1,0) {\(\square\)};
     \node[draw, circle, minimum size=24pt] (2) at (2,0) {\(\square_{1}\)};
   }
   \tikz{
     \node (a) at (0,0) {}; \node (b) at (3,0) {};
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};
     \path[draw=red] (a) to (b);
     \path[draw=red] (y1) to (y2);
     \path[draw=red] (y3) to (y4);

     \node[draw, circle, minimum size=24pt] (1) at (1,0) {\(\square\)};
     \node[draw, circle, minimum size=24pt] (2) at (2,0) {\(\square_{1}\)};

   }
 \end{center}
 Math mode instead uses the same baseline:
  \begin{center}
   \tikz{
     \node (a) at (0,0) {}; \node (b) at (4,0) {};
     \path[draw=red] (a) to (b);

     \node[draw, circle] (1) at (2,0) {\(\square = \square_{1}\)};
   }
 \end{center}  
\end{document}

This produces:

enter image description here

What I would like instead would be for both vertices to have the "v" at the same position, like they are in math mode, because otherwise it looks quite odd when both vertices appear side by side in a graph.

Is there a way to do this without fudging with \phantom letters and whatnot?

1 Answer 1

4

You can use anchor=mid or anchor=base in the node options. Here is the result with the former:

\documentclass[varwidth,border=1mm]{standalone}
\usepackage{tikz}
\usepackage{amssymb}

\begin{document}
Ti\textit{k}Z gives each node its own baseline for text:
 \begin{center}
   \tikz{
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};

     \node[draw, circle, minimum size=24pt, anchor=mid] (1) at (1,0) {\(v\)};
     \node[draw, circle, minimum size=24pt, anchor=mid] (2) at (2,0) {\(v_{1}\)};
   }
   \tikz{
     \node (a) at (0,0) {}; \node (b) at (3,0) {};
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};
     \path[draw=red] (a) to (b);
     \path[draw=red] (y1) to (y2);
     \path[draw=red] (y3) to (y4);

     \node[draw, circle, minimum size=24pt, anchor=mid] (1) at (1,0) {\(v\)};
     \node[draw, circle, minimum size=24pt, anchor=mid] (2) at (2,0) {\(v_{1}\)};
   }
 \end{center}
 The effect is more obvious with squares:
 \begin{center}
   \tikz{
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};

     \node[draw, circle, minimum size=24pt, anchor=mid] (1) at (1,0) {\(\square\)};
     \node[draw, circle, minimum size=24pt, anchor=mid] (2) at (2,0) {\(\square_{1}\)};
   }
   \tikz{
     \node (a) at (0,0) {}; \node (b) at (3,0) {};
     \node (y1) at (1,.8) {}; \node (y2) at (1,-.8) {};
     \node (y3) at (2,.8) {}; \node (y4) at (2,-.8) {};
     \path[draw=red] (a) to (b);
     \path[draw=red] (y1) to (y2);
     \path[draw=red] (y3) to (y4);

     \node[draw, circle, minimum size=24pt, anchor=mid] (1) at (1,0) {\(\square\)};
     \node[draw, circle, minimum size=24pt, anchor=mid] (2) at (2,0) {\(\square_{1}\)};

   }
 \end{center}
\end{document}

enter image description here

With anchor=base:

enter image description here

3
  • Thank you, that is exactly what I was looking for! Is there a similar trick for the horizontal shift of the content? Jun 26 at 7:46
  • For horizontal placement, I don't see any other option than making TeX believe both “things” have the same width (using \phantom and/or \makebox tricks).
    – frougon
    Jun 26 at 7:48
  • Sadly, the graphdrawing library seems to ignore this option. A MWE does not fit in a comment, but essentially, as soon as I use graphdrawing and pass anchor=mid to the two vertices, they stay as before, i.e. wrong. Jun 26 at 7:59

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.