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?
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:
\def\smash{%
\relax % \relax, in case this comes first in \halign
\ifmmode
\expandafter\mathpalette\expandafter\mathsm@sh
\else
\expandafter\makesm@sh
\fi}
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
\mathpalette\mathsm@sh{abq}
and we now need to look at \mathsm@sh:
% latex.ltx, line 4503:
\def\mathsm@sh#1#2{%
\setbox\z@\hbox{$\m@th#1{#2}$}\finsm@sh}
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:
\def\makesm@sh#1{%
\setbox\z@\hbox{\color@begingroup#1\color@endgroup}\finsm@sh}
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:
\ifx\leavevmode@ifvmode\@undefined
\renewcommand{\smash}[1][tb]{%
\def\mb@t{\ht}\def\mb@b{\dp}\def\mb@tb{\ht\z@\z@\dp}%
\edef\finsm@sh{\csname mb@#1\endcsname\z@\z@\box\z@}%
\ifmmode \@xp\mathpalette\@xp\mathsm@sh
\else \@xp\makesm@sh
\fi
}
\else
\renewcommand{\smash}[1][tb]{%
\def\mb@t{\ht}\def\mb@b{\dp}\def\mb@tb{\ht\z@\z@\dp}%
\edef\finsm@sh{\csname mb@#1\endcsname\z@\z@ \leavevmode@ifvmode\box\z@}%
\ifmmode \@xp\mathpalette\@xp\mathsm@sh
\else \@xp\makesm@sh
\fi
}
\fi
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:
\protected\def\leavevmode@ifvmode{\ifvmode\expandafter\indent\fi}
depth is not the same as width so \smash doesn't do anything to horizontal spacing. See tex.stackexchange.com/q/40977/128658 for the depth vs. height explanation and tex.stackexchange.com/q/545310/128658 for how to get smash-like behavior for horizontal spacing.
\smash is a macro defined as
\relax \ifmmode \def \next {\mathpalette \mathsm@sh }\else \let \next \makesm@sh \fi \next
and \mathsm@sh is defined as:
\setbox \z@ \hbox {$\m@th #1{#2}$}\finsm@sh
and finsm@sh is defined as
ht \z@ \z@ \dp \z@ \z@ \box \z@
A simpler version of the command which makes what it does a bit more obvious is this one from TeX by Topic:
\def\smash#1{{\setbox0=\hbox{#1}\dp0=0pt \ht0=0pt \box0\relax}}
which basically takes some content and puts it into a box of 0 height and 0 depth.
It's documented in the TeXbook and also in TeX by Topic, which is included as part of the TeX Live documentation (texdoc texbytopic should bring it up.)
The actual macros in the LaTeX kernel can be found in the document source2e.pdf (also available through texdoc.)
Some packages such as amsmath redefine it. See also
This answer attempts to address @BenCrowell's desire for examples of how smash can affect the output. As comments have indicated, it is height and depth, not width, that is affected by \smash. What this affects, therefore, is the vertical space allotted for the text, which could affect vertical placement of pre- and suc-ceding lines, the positions of super and supscripts (associated with the smashed material), as well as material stacked in conjunction with \smashed text.
\documentclass{article}
\usepackage{stackengine}
\begin{document}
\noindent Affects placement of pre-\\
$\displaystyle\frac{Q}{Z}$ (vertical space widened to account)\\
and suc-ceeding material\\
$\displaystyle\smash{\frac{Q}{Z}}$ (vertical space not widened)\\
See what I mean?
\bigskip
Now let's look at super/subscript placement.
\[\left(\frac{a}{b}\right)^a_b\]
Compare if fraction is smashed:
\[\smash{\left(\frac{a}{b}\right)}^a_b\]
Now for certain types of stacks:
\Shortstack{a b fg d e} versus \Shortstack{a b \smash{fg} d e}.
\end{document}
\smash is great for tweaking the size of text in a paragraph (using fontspec Scale) without spreading lines. The user is responsible for ensuring that the smashed text does not overlap something, unless the overlap is intentional.
For what it's worth, from User’s Guide for the amsmath Package:
The command \smash is used to typeset a subformula with an effective height and depth of zero, which is sometimes useful in adjusting the subformula’s position with respect to adjacent symbols.
With the amsmath package \smash has optional arguments [t] and [b] because occasionally it is advantageous to be able to “smash” only the top or only the bottom of something while retaining the natural depth or height.
For example, when adjacent radical symbols are unevenly sized or positioned because of differences in the height and depth of their contents, \smash can be employed to make them more consistent.
And they provide an example using \smash[b] under the square root symbol:
Without this option:
With it:
\sqrt{x} + \sqrt{\smash[b]{y}} + \sqrt{z}
The second radicand has reduced vertical limits for its bounding box.
[b], just what I was looking for. I think your images might be the wrong order? To me the second one looks to have the inconsistent sqrt baselines.
This years-old question is still useful. Some comments, presented as an answer:
\smash is not restricted to math mode. It works in ordinary text, and AFAIK in all instances of printed text, even page headers and footnotes.
For example, in an artistic page layout, page headers could smash an image into the gap between header and main text. This has been done.
As already noted, \smash continues to occupy its normal horizontal space. If you smash some text to a distant part of a page (this is possible), then a gap will be left, where the text would ordinarily appear. You could absorb this
horizontal space by following the smash with a suitable negative \hspace or something similar. Might make a difference if that happens to occur at a line break. I believe that this is the strategy used by the textpos package.
If you are familiar with image layers in a graphics program, that is similar to what \smash does. By analogy, the smashed text is moved "in the z direction" to a new layer. In HTML, the analogy is absolute positioning with a changed z layer.
I believe that ordinary text, when smashed, does not create a "PDF layer" that would be forbidden in some versions of PDF/X. But I am not certain. In any case, a smashed image would create such a possibly-forbidden layer.
novel? Why was it removed from GitHub?
djazz, but I cannot easily find it. Anyone who wishes can duplicate (and change) novel at GitHub, CTAN, or wherever. The license allows that. Understand that the novel code is very cluttered with tricks to deal with situations going back 10 years, now no longer necessary, and confusing. Also, novel permits a few tricks that work on paper, but cannot easily be ported to Ebook. Nowadays, most printers require Ebook also.
novelette or something.
novelette last year. But in the intervening time, so much changed in the print-on-demand workflow, that I decided it must be re-done. Currently re-doing it. Hope to post it at GitHub maybe next month. If not problems, might later appear at CTAN. Deliberately waiting until TL 2025 released, in case any changes needed. But novelette is not a revised novel.
\smash{<subformula>}, a macro that yields the same result as{<subformula>}but makes the height and depth zero.\smash{aaa}bbbwould be typeset as bbb overstriking aaa, but in fact the output is exactly the same as if I just useaaabbbin the source code.\makebox[0pt][l]{aaa}(or\rlap{aaa}).