# The phantom directive does not do what I expect

Typing these two codes on http://latex2png.com/:

\sqrt{\phantom{a^2}b}

and

\sqrt{a^2b}

I get the following results:

You will notice that the two square roots have different shapes and sizes, contrary to what one would expect from the description of \phantom.

Is there a workaround to achieve identical sizes?

What goes wrong is that the argument of the square root is typeset in a cramped style.

If the radical appears in display style, then the material under the square root sign is typeset in cramped display style. However, \phantom only chooses among the uncramped styles, so you get a different height. In the following picture you see the output of a^2 in uncramped and cramped styles

Clearly this explains the difference in placement of the radical sign you get.

Use \cramped from mathtools:

\documentclass{article}
\usepackage{mathtools}

\begin{document}

\begin{align}
&\sqrt{a^2b}\,\sqrt{\phantom{\cramped{a^2}}b} \\
&\sqrt{\phantom{\cramped{a^2}}b}\,\sqrt{a^2b} \\
&\sqrt{\frac{1}{2}}
\end{align}

\end{document}

If you use LuaLaTeX, then there's a slicker way: redefining \mathpalette.

\documentclass{article}
\usepackage{amsmath}

\renewcommand*{\mathpalette}[2]{%
\ifcase\mathstyle\relax
#1\displaystyle{#2}\or
#1\crampeddisplaystyle{#2}\or
#1\textstyle{#2}\or
#1\crampedtextstyle{#2}\or
#1\scriptstyle{#2}\or
#1\crampedscriptstyle{#2}\or
#1\scriptscriptstyle{#2}\or
#1\crampedscriptscriptstyle{#2}\fi
}

\begin{document}

\begin{align}
&\sqrt{a^2b}\,\sqrt{\phantom{a^2}b} \\
&\sqrt{\phantom{a^2}b}\,\sqrt{a^2b} \\
&\sqrt{\displaystyle a^2b}\,\sqrt{\displaystyle\phantom{a^2}b}\\
&\sqrt{\displaystyle\phantom{a^2}b}\,\sqrt{\displaystyle a^2b}
\end{align}

\end{document}

Caveat The above redefinition of \mathpalette doesn't guarantee success in all cases, particularly when a command defined with \mathpalette is used in a fraction. It's essentially a proof of concept.

• I was sure you'd answer this question :):):) – CarLaTeX Dec 3 '17 at 12:02
• why wouldn't an \hphantom work just as well? maybe not in all cases, but in this case, ... – barbara beeton Dec 4 '17 at 17:53
• @barbarabeeton I'd like to have a general solution. – egreg Dec 4 '17 at 18:14

Your example shows output from \displaystyle, i.e. between $$...$$. I'll explain why we can see such difference.

The \phantom macro creates a box like \hbox{$\currentstyle contents$}, empties such box but uses its dimensions.

It means that it does something like this:

$$\sqrt{a^2 b}, \sqrt{\hbox{\displaystyle a^2}b}, \sqrt{\phantom{a^2}b}$$

Note that the exponent position is in another place in \displaystyle than in the argument of \sqrt, where reduced \textstyle is used. The position of exponents grows up in three steps: reduced textstyle, textstyle, displaystyle. The word "reduced" means that something is above such exponent, so exponent will be not high up. But when \hbox{$\displaystyle a^2$} is created then this is done independently of context and TeX does not know that there is something above the exponent.

• +1. At the end of the first sentence, you wrote, "... where reduced \textstyle is used". Is it reduced (aka cramped) \textstyle that's in use here, or is it cramped \displaystyle? The difference between cramped \textstyle and cramped \displaystyle, AFAICT, is not in the vertical offset of the exponent "2" but, rather, in the amount of whitespace that's inserted between "2" and the horizontal bar of the square root symbol. Please advise. – Mico Dec 3 '17 at 9:41
• The explanation is wrong. In the argument to the root, (cramped) display style is used, not text style, which you can see by trying $$\sqrt{1\over2}$$\bye. – egreg Dec 3 '17 at 10:22

I don't use http://latex2png.com/, but, as jakun correctly pointed out, that seems to be so in display math:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Inline:
$$\sqrt{\phantom{a^2}b}$$
$$\sqrt{a^2b}$$

Display style:
$\sqrt{\phantom{a^2}b}$
$\sqrt{a^2b}$

Display style whith \verb|\hphantom|:
$\sqrt{\hphantom{a^2}b}$
$\sqrt{a^2b}$
\end{document}

It can be solved typing it in inline math:

$\sqrt{a^2b}$ and $\sqrt{\phantom{a^2}b}$

or using \hphantom:

\sqrt{a^2b}\sqrt{\hphantom{a^2}b}

this seems to give the correct output also on that site:

What about playing with \hphantom and \vphantom in that case?

I've added a grid to show that the dimensions seem correct:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{eso-pic}

\begin{document}
\AtPageLowerLeft{%
\begin{tikzpicture}[overlay,remember picture]
\draw[step=.1,gray,very thin] (0,0) grid (\paperwidth,\paperheight);
\end{tikzpicture}}}
Inline:

$$\sqrt{a^{2^N}b}$$
$$\sqrt{\hphantom{a^{2^N}}\vphantom{^N}b}$$

$$\sqrt{\hphantom{a^{2^N}}\vphantom{^N}b}$$

Display style:
$\sqrt{a^{2^N}b}$

Display style whith \verb|\hphantom| and \verb|\vphantom|:
$\sqrt{\hphantom{a^{2^N}}\vphantom{^N}b}$
\end{document}

• Type \sqrt{a^2b}\sqrt{\phantom{a^2}b} into latex2png.com – Wynne Dec 3 '17 at 6:37
• This might be an issue of display style vs inline math. When compiling \begin{align} & \sqrt{\phantom{a^2}b} \\ & \sqrt{a^2b} \end{align} with pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian) the root with the phantom is bigger. – jakun Dec 3 '17 at 7:13
• @jakun That can be solved using \hphantom, I'll add it to my answer. – CarLaTeX Dec 3 '17 at 7:20
• @Wynne Look at my renewed answer according to jakun pointing out. – CarLaTeX Dec 3 '17 at 7:32
• I'm afraid this is too simplistic: try \sqrt{a^{2^N}b} and then with \sqrt{\hphantom{a^{2^N}}b} – egreg Dec 3 '17 at 10:25

FWIW, it appears that the output is correct in ConTeXt MkIV.

\starttext
\startformula
\sqrt{\phantom{a^2}b}
\sqrt{a^2b}
\stopformula
\stoptext

gives

However, (as explained by @egreg below) the \sqrt in ConTeXt uses \displaystyle rather than \crampeddisplaystyle. If we switch to crampeddisplaystype, the output of \phantom is still correct:

\starttext
\startformula
\sqrt{\phantom{a^2}b}
\sqrt{a^2b}
\stopformula
\startformula
\sqrt{\crampeddisplaystyle\phantom{a^2}b}
\sqrt{\crampeddisplaystyle a^2b}
\stopformula
\stoptext

which gives

I also tested with LuaLaTeX, but LuaLaTeX gives the same output as PDFLaTeX. Perhaps the ConTeXt definition of \phantom (defined in supp-box.mkiv) could be adapted in the LaTeX kernel or a LaTeX package.

• It seems that ConTeXt is using the uncramped style under the square root, as the superscript is higher than with the cramped style. – egreg Dec 3 '17 at 17:43
• It's possible through the \mathstyle int I imagine. In classic TeX may be just adding a \insidesqrttrue would help. – Manuel Dec 3 '17 at 17:48
• But the OP is using latex2png.com and if you put your MWE over there, it doesn't work – CarLaTeX Dec 3 '17 at 17:53
• @egreg Tracing the code confirms this :) – Joseph Wright Dec 3 '17 at 18:00
• @egreg, @JosephWright: Thanks. I added an example with the \displaycrampedstyle as well. I'll check if it is possible to change ConTeXt sqrt to cramped style. – Aditya Dec 3 '17 at 23:43