# Is there a simple or built-in way to place the baseline of a TikZ picture on the math axis?

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.)

• Well, for what it's worth, your solution looks quite good to me... Maybe you can define a tikz style with argument to wrap around baseline=#1-relevant height ? Commented Aug 14, 2017 at 9:22
• When you say "on the math axis" do you mean "centered about the math axis"? For example, if I make the tikzpicture with a thick line, 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? Commented Aug 14, 2017 at 9:46
• @StevenB.Segletes I mean that the vertical position specified by the baseline key should be on the math axis. So in the MWE, the horizontal line should be centred on the math axis. Commented Aug 14, 2017 at 9:57
• You can use [yshift=\ht0] but you still need the \sbox0{$\vcenter{}$} before tikzpicture. Commented Aug 14, 2017 at 13:17
• Not sure it simplifies anything, but know that instead of \vcenter, one may use the dimensions of a parenthesis to determine the math axis. That is to say, \sbox0{$\vcenter{}$}\the\ht0 provides the same result as \setbox0=\hbox{)}\the\dimexpr.5\ht0-.5\dp0\relax Commented Oct 17, 2017 at 10:57

This does it without setting boxes, but rather uses a dimension derived from a parenthesis to determine the math axis. I call that \defed dimension \axisht, and it corrects itself when the size of the original changes, as shown in the MWE below.

Used in conjunction with John's suggestion of [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}