我想将文本置于 tikz 节点的中心,而不考虑下标/上标。当文本是一行句子时,我可以使用text depth=0
和text height=1.5ex
。但是,当节点包含多行文本时,文本不再居中。
您是否知道任何通用的解决方案,可以在不考虑下标/上标的情况下将任何文本居中?如果没有,至少您是否有办法text depth/height
在一行文本上使用当前解决方案,并text depth/text height
在多行节点上使用相同的样式?
例子:
梅威瑟:
\documentclass{report}
\usepackage{tikz}
\begin{document}
What i have:\\
\begin{tikzpicture}
[
mynode/.style={
circle,
draw=blue,
fill=blue!30,
minimum width=2cm,
% anchor=mid,
text depth=0ex,
text height=1.5ex,
% align=center,
% text width=1.5cm
}
]
\foreach \x [count=\i] in {$ABC_\gamma$,$ABC^\delta$,$ABC$,Two lines text}{
\node[mynode] at (0,-3*\i) (c-\i) {\x};
\node[circle,fill=red,inner sep=1pt] at (c-\i){};
}
\end{tikzpicture}
\\
What I want:\\
\begin{tikzpicture}
\node[
circle,
draw=blue,
fill=blue!30,
minimum width=2cm,
anchor=mid,
align=center,
text width=1.5cm
]{Two lines text};
\end{tikzpicture}
\\
What I do not want:\\
\begin{tikzpicture}
\node[
circle,
draw=blue,
fill=blue!30,
minimum width=2cm,
anchor=mid,
align=center,
text width=1.5cm
]{$ABC_\gamma$};
\node[circle,fill=red,inner sep=1pt] at (0,0){};
\end{tikzpicture}
\end{document}
答案1
评论太长了。我认为你对 @percusse 的回复具有误导性。根据我的发现,\smash
确实有效。这里我展示了三个示例,第一个没有下标,然后有下标和粉碎,最后没有粉碎。与上面的图片的不同是由于你的设置,但不是因为下标。
\documentclass{report}
\usepackage{tikz,tikzpagenodes}
\begin{document}
What i have:\\
\begin{tikzpicture}
[
mynode/.style={
circle,
draw=blue,
fill=blue!30,
minimum width=2cm,
% anchor=mid,
text depth=0ex,
text height=1.5ex,
% align=center,
% text width=1.5cm
}
]
\foreach \x [count=\i] in {$ABC_\gamma$,$ABC^\delta$,$ABC$,Two lines text}{
\node[mynode] at (3*\i,0) (c-\i) {\x};
\node[circle,fill=red,inner sep=1pt] at (c-\i){};
}
\end{tikzpicture}
\\
% What I want:\\
% \begin{tikzpicture}
% \node[
% circle,
% draw=blue,
% fill=blue!30,
% minimum width=2cm,
% anchor=mid,
% align=center,
% text width=1.5cm
% ]{Two lines text};
% \end{tikzpicture}
% \\
What I do not want:\\
\begin{tikzpicture}[remember picture]
\node[
circle,
draw=blue,
fill=blue!30,
minimum width=2cm,
anchor=mid,
align=center,
text width=1.5cm
]{$ABC$};
\node[circle,fill=red,inner sep=1pt](c5) at (0,0){};
\end{tikzpicture}
\begin{tikzpicture}[remember picture]
\node[
circle,
draw=blue,
fill=blue!30,
minimum width=2cm,
anchor=mid,
align=center,
text width=1.5cm
]{$AB\smash{C_{\gamma}}$};
\node[circle,fill=red,inner sep=1pt](c6) at (0,0){};
\end{tikzpicture}
\begin{tikzpicture}[remember picture]
\node[
circle,
draw=blue,
fill=blue!30,
minimum width=2cm,
anchor=mid,
align=center,
text width=1.5cm
]{$ABC_{\gamma}$};
\node[circle,fill=red,inner sep=1pt](c7) at (0,0){};
\end{tikzpicture}
\begin{tikzpicture}[remember picture,overlay]
\draw[blue] (c5-| current page text area.west) --
(c6-| current page text area.east);
\end{tikzpicture}
\end{document}