![使用 TikZ 节点内的索引正确居中数学模式](https://linux22.com/image/451780/%E4%BD%BF%E7%94%A8%20TikZ%20%E8%8A%82%E7%82%B9%E5%86%85%E7%9A%84%E7%B4%A2%E5%BC%95%E6%AD%A3%E7%A1%AE%E5%B1%85%E4%B8%AD%E6%95%B0%E5%AD%A6%E6%A8%A1%E5%BC%8F.png)
在 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
: