我想绘制一个+
中心在圆形的 TikZ 节点,但font=\ttfamily
将其向上移动。为什么?如何解决?
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
or/.style = {circle,
minimum size=4mm,
very thick,
draw,
font=\ttfamily,
},
]
\node[or,font=\normalfont] (a) {+};
\draw[very thin,red] (a.west)--(a.east);
\node[or] (b) at (1,0) {+};
\draw[very thin,red] (b.west)--(b.east);
\end{tikzpicture}
\end{document}
答案1
按照设计,+
Computer Modern 中的水平条略高于我们所认为的视觉中心;这“欺骗”了 TiKZ 将对象置于节点坐标中心的算法。
\documentclass[border=1]{standalone}
\newcommand{\ttplus}{%
\raisebox{-.15ex}[\dimexpr\height-.15ex\relax][0pt]{\ttfamily+}}
\usepackage{tikz}
\begin{document}
\hbox{\fboxsep=0pt \fboxrule=.2pt % just to have no sep and lighter rules
\fbox{+}\,a\,\fbox{\ttfamily+}\,\fbox{\ttplus}}
\hbox{\begin{tikzpicture}[
or/.style = {circle,
minimum size=4mm,
very thick,
draw,
},
]
\node[or,font=\normalfont] (a) {+};
\draw[very thin,red] (a.west)--(a.east);
\node[or] (b) at (1,0) {\ttplus};
\draw[very thin,red] (b.west)--(b.east);
\end{tikzpicture}}
\end{document}
答案2
只是想补充一下 egreg 的精彩回答。为什么它会向上移动?TikZ 将使封闭框居中,如下图所示:
TeX 在文本模式下将字母符号对齐到基线,在数学模式下则对齐到中心线。字距调整是部分解决方案,如 egreg 所示。+
在数学模式下使用 是一个更好的解决方案。这不仅仅是字体问题,也是 TikZ 对齐方式更接近数学模式的问题。更好的方法是使用 TikZ 设计一个小十字,并将其放置在精确的 中心。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\def\boxes{\fbox{\fbox{y}\fbox{x}\fbox{+}\fbox{f}}}
\begin{document}
\begin{tikzpicture}[
or/.style = {circle,
minimum size=6mm,
very thick,
draw,
font=\ttfamily,
},
]
\node[or,font=\normalfont] (a) {yx\textsuperscript{2}+};
\draw[very thin,red] (a.west)--(a.east);
\node[or] (b) at (1.3,0) {$x^2_3+$};
\draw[very thin,red] (b.west)--(b.east);
\fboxsep0pt\fboxrule0.1pt
\node[or] (c) at (2.5,0) {\fbox{\fbox{y}\fbox{x}\fbox{+}}};
\draw[very thin,red] (c.west)--(c.east);
\end{tikzpicture}
\texttt{\boxes}
\end{document}