79

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?

4
  • 6
    From the TeXbook: Plain TEX also provides \smash{<subformula>}, a macro that yields the same result as {<subformula>} but makes the height and depth zero. Commented Mar 31, 2018 at 17:40
  • Both of the existing answers leap into technical explanations of how the command is implemented, but neither explains what the result actually looks like on the page or why the command is useful. Based on the answers, I would expect that \smash{aaa}bbb would be typeset as bbb overstriking aaa, but in fact the output is exactly the same as if I just use aaabbb in the source code. Commented Jul 10, 2021 at 13:59
  • 1
    @BenCrowell egregs answer starts with " ... prints it as if its height and depth were zero", height and depth are not the width. But it into a \fbox to see the effect. Commented Jul 10, 2021 at 16:28
  • @BenCrowell Where did you get that idea? The width is not modified. In order to print text pretending it has zero width, there is \makebox[0pt][l]{aaa} (or \rlap{aaa}). Commented Jul 10, 2021 at 21:10

5 Answers 5

54

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}
1
18

\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

0
16
+25

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}

enter image description here

1
  • Indeed. Outside of math mode, \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. Commented Mar 24, 2023 at 15:23
1

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:

enter image description here

With it:

\sqrt{x} + \sqrt{\smash[b]{y}} + \sqrt{z}

enter image description here

The second radicand has reduced vertical limits for its bounding box.

2
  • 1
    Thanks for the example using [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. Commented Feb 9, 2025 at 12:25
  • @WillRobertson: Thanks for reporting, fixed. Commented Feb 9, 2025 at 13:03
0

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.

5
  • Are you the developer of novel? Why was it removed from GitHub? Commented Feb 9, 2025 at 17:52
  • 1
    @Gaussler Yes. But I not longer maintain it, hence removed from GitHub. Still works in TeXlive 2024, AFAIK. There was once (maybe still is) a GitHub fork by CTAN user 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. Commented Feb 9, 2025 at 18:42
  • Yeah, I remember you saying that you were working on a modern reimagination of it, novelette or something. Commented Feb 9, 2025 at 19:09
  • 1
    @Gaussler Yes, was going to release 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. Commented Feb 9, 2025 at 19:59
  • 1
    No, I get that, hence the word “reimagined”, but maybe that's not the right term, either? In any case, really looking forward to playing with it once it arrives! 😊 Commented Feb 9, 2025 at 20:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.