[Tex/LaTex] What does \smash do, and where is it documented

math-modevertical alignment

I see several users in this forum talk about \smash. It is apparently defined in base TeX.

What does \smash do?

For future reference, is there online documentation where I could find this?

Best Answer

As the name implies, \smash takes its contents and prints it as if its height and depth were zero.

The definition in LaTeX is carried over from the one in plain TeX, with a slight difference; I'll use the LaTeX one for ease of reference. The following refers to LaTeX 2018-12-01.

% latex.ltx, line 4494:
  \relax % \relax, in case this comes first in \halign

The macro distinguish if it is called in math mode or not. Note that, for efficiency, it doesn't read its argument at the outset. I'll assume the call is \smash{abq}. In math mode we get


and we now need to look at \mathsm@sh:

% latex.ltx, line 4503:

According to the working of \mathpalette, we get the equivalent of

\setbox\z@\hbox{$\m@th<current style>{abq}$}\fin@smash

If the call is in text mode, we have \makesm@sh{abq}. Now

% latex.ltx, line 4501:

so we obtain

\setbox\z@\hbox{\color@begingroup abq\color@endgroup}\finsm@sh

In both cases TeX has set the contents of box 0; now \finsm@sh does its job

% latex.ltx, line 4505:
\def\finsm@sh{\ht\z@\z@ \dp\z@\z@ \leavevmode@ifvmode\box\z@}

This sets the height and depth of box 0 to 0pt and typesets the box after initiating horizontal mode.

When you load amsmath the definition is basically the same, with the difference that an optional argument is allowed, which can be either t or b. With t the last operation becomes only

\ht\z@\z@ \box\z@

so the depth is preserved; conversely, with b LaTeX only does

\dp\z@\z@ \box\z@

and the height is preserved. This is accomplished by the redefinition (referring to amsmath.sty release 2018-12-01)

% amsmath.sty, line 903:
  \edef\finsm@sh{\csname mb@#1\endcsname\z@\z@\box\z@}%
  \ifmmode \@xp\mathpalette\@xp\mathsm@sh
  \else \@xp\makesm@sh
  \edef\finsm@sh{\csname mb@#1\endcsname\z@\z@ \leavevmode@ifvmode\box\z@}%
  \ifmmode \@xp\mathpalette\@xp\mathsm@sh
  \else \@xp\makesm@sh

You see that \finsm@sh is redefined each time to consist of the necessary bits.

The conditional definition of \smash in amsmath.sty is due to the fact that \leavevmode@ifvmode has been added to LaTeX starting from version 2018-12-01 to initiate horizontal mode if found in vertical mode. In prior version of LaTeX this didn't happen, reflecting the same behavior as in plain TeX, where the definition is the same as in the LaTeX kernel, but without \leavevmode@ifvmode, which is defined in the LaTeX kernel by

% latex.ltx, line 1633: