[Tex/LaTex] \phantom considering cramped style

macrosmath-mode

In LaTeX2e, \phantom is defined as

\def\vphantom{\v@true\h@false\ph@nt}
\def\hphantom{\v@false\h@true\ph@nt}
\def\phantom{\v@true\h@true\ph@nt}
\def\ph@nt{%
  \ifmmode
    \expandafter\mathpalette\expandafter\mathph@nt
  \else
    \expandafter\makeph@nt
  \fi}
\def\makeph@nt#1{%
  \setbox\z@\hbox{\color@begingroup#1\color@endgroup}\finph@nt}
\def\mathph@nt#1#2{%
  \setbox\z@\hbox{$\m@th#1{#2}$}\finph@nt}
\def\finph@nt{%
  \setbox\tw@\null
  \ifv@ \ht\tw@\ht\z@ \dp\tw@\dp\z@\fi
  \ifh@ \wd\tw@\wd\z@\fi \box\tw@}

It means that, in math mode,

$\sqrt{\phantom{x^2}}$

has the same size of

$\sqrt{\hbox{$x^2$}}$

but differs from

$\sqrt{x^2}$

since x^2 in \sqrt is in cramped mode.

I know that mathtools package provides a \cramped macro to solve the problem, but is there an automatical method?


Test code:

\documentclass{minimal}
\usepackage{mathtools}
\begin{document}
\fontsize{60}{72}\selectfont
$\sqrt{x^A} \sqrt{\hbox{$x^A$}}$

$\sqrt{\phantom{\cramped{x^A}}} \sqrt{\phantom{x^A}}$ 
\end{document}

enter image description here

Best Answer

I think the answer is "no" even with e-tex primitives you can not detect this. Perhaps luatex can jump to the rescue?

With thanks to the commenters, this should be possible in luatex

\ifnum\mathstyle=\crampedtextstyle 

or for lualatex

\ifnum\luatexmathstyle=\luatexcrampedtextstyle 

Note that as usual \over may cause some complications stopping the math style being detected, see \Ustack (\luatexUstack) in the luatex manual.

Related Question