在 TikZ 中制作图表时,如果某些节点的标签有索引,而其他节点的标签没有索引,则会出现非常丑陋的效果,即 TikZ 试图将全部的标签而不是基线,因为每个节点都分配有自己的基线。
您可以在以下示例中看到我的意思:
\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}
得出的结果为:
我希望的是两个顶点的“v”位于相同的位置,就像它们处于数学模式一样,因为否则当两个顶点在图中并排出现时看起来会很奇怪。
有没有办法做到这一点而不弄虚作假\phantom
字母等等?
答案1
您可以在节点选项中使用anchor=mid
或anchor=base
。以下是使用前者的结果:
\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}
和anchor=base
: