当在显示数学中使用 TikZ 图片时,如何将基线放在数学轴上而不是数学基线上?
将 TikZ 图片与基线对齐非常简单,如这个问题。
就像下面的 MWE 一样,我也可以定义一个宏\onmathaxis
来自这个答案Heiko Oberdiek 的回答是,使用包含空的框的高度\vcenter{}
来获取基线上方数学轴的高度,并将其用于\raisebox
:
\documentclass{article}
\usepackage{tikz}
\makeatletter
% Derived from https://tex.stackexchange.com/a/129840/44122
\DeclareRobustCommand*{\onmathaxis}[1]{%
\ensuremath{{% allow invoking in text mode
% * \vcenter centers stuff vertically around the math axis. By putting
% an empty `\vcenter{}` in a box, we get the height of the math axis
% as height of the box.
% * Box register 0 is a local scratch register.
\sbox0{$\vcenter{}$}%
% * \ht0: height of box 0, thus height of math axis.
% * \m@th resets \mathsurround to 0pt (avoids additional
% surrounding space if \mathsurround is set).
\raisebox{\ht0}{$\m@th #1$}%
}}%
}
\makeatother
\begin{document}
Aligned with baseline:
\bigskip
Inline math: $\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm);\end{tikzpicture} - x$.
\bigskip
\[
\mbox{Display math: }\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm); \end{tikzpicture} - x.
\]
\bigskip
\bigskip
Using \verb|\onmathaxis|:
\bigskip
Inline math: $\onmathaxis{\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm);\end{tikzpicture}} - x$.
\bigskip
\[
\mbox{Display math: }\onmathaxis{\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm); \end{tikzpicture}} - x.
\]
\end{document}
但我想知道是否有更简单的方法 - 也许是 TikZ 内置的东西,或者是执行类似操作的内置宏\onmathaxis
。
请注意,这\vcenter
本身是不够的:TikZ 图片的基线可能不是垂直居中,就像上面的 MWE 一样。
(就我而言,我正在画费雷尔图,并且出于美观原因,我喜欢将数学轴与图表的一行对齐。)
答案1
这样做无需设置方框,而是使用从括号中得出的维度来确定数学轴。我称之为\def
ed 维度\axisht
,当原始尺寸发生变化时,它会自行修正,如下面的 MWE 所示。
与约翰的建议一起使用[yshift=\axisht]
。
\documentclass{article}
\usepackage{tikz}
\def\axisht{\dimexpr.5\fontcharht\font`)-.5\fontchardp\font`)\relax}
\begin{document}
Aligned with baseline:
\bigskip
Inline math: $\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm);\end{tikzpicture} - x$.
\bigskip
\[
\mbox{Display math: }\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm); \end{tikzpicture} - x.
\]
\bigskip
\bigskip
Using \verb|\axisht|:
\bigskip
Inline math: $\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm,yshift=\axisht] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm);\end{tikzpicture} - x$.
\bigskip
\[
\mbox{Display math: }\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm,yshift=\axisht] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm); \end{tikzpicture} - x.
\]
\bigskip
\bigskip
\LARGE
Using \verb|\axisht|:
\bigskip
Inline math: $\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm,yshift=\axisht] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm);\end{tikzpicture} - x$.
\bigskip
\[
\mbox{Display math: }\begin{tikzpicture}[baseline=1.5mm,x=3mm,y=3mm,yshift=\axisht] \filldraw[lightgray] (-3mm,-3mm) -- (3mm,-3mm) -- (3mm,3mm) -- (-3mm,3mm) -- cycle; \draw (-3mm,1.5mm)--(3mm,1.5mm); \end{tikzpicture} - x.
\]
\end{document}