When one uses a TikZ picture in display math, how can one place the baseline on the math axis instead of the math baseline?
It is straightforward to align TikZ pictures with the baseline, as explained in the answers to this question.
As in the following MWE, I was also able to define a macro \onmathaxis using a trick from this answer answer by Heiko Oberdiek, using the height of a box containing an empty \vcenter{} to obtain the height of the math axis above the baseline and using that in a \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}
But I wonder if I there is a simpler way -- perhaps something built-in to TikZ, or a built-in macro that does something similar to \onmathaxis.
Note that \vcenter itself is not sufficient: the baseline of the TikZ picture may not be vertically centered, as in the MWE above.
(In my case, I am drawing Ferrers diagrams, and I like to have the math axis aligned with a row of the diagram for aesthetic reasons.)


baseline=#1-relevant height? – marsupilam Aug 14 '17 at 09:22tikzpicturewith athickline, the baseline of the thick line is not "on" the math axis; rather, the thick line is "centered about" the math axis. Which of these two behaviors is actually desired? – Steven B. Segletes Aug 14 '17 at 09:46baselinekey should be on the math axis. So in the MWE, the horizontal line should be centred on the math axis. – Senex Aug 14 '17 at 09:57\vcenter, one may use the dimensions of a parenthesis to determine the math axis. That is to say,\sbox0{$\vcenter{}$}\the\ht0provides the same result as\setbox0=\hbox{)}\the\dimexpr.5\ht0-.5\dp0\relax– Steven B. Segletes Oct 17 '17 at 10:57