[Tex/LaTex] What are the different kinds of vertical spacing and horizontal spacing commands available

spacing

@egreg gave a very nice and thorough answer to What are the different kinds of boxes in (La)TeX?. It seems it would be nice to have a similarly thorough answer to controlling spacing.

I'm not thinking so much about

\vspace
\hspace
\\[<dim>]

and their starred variants because there is a very thorough answer about such spacing commands found at What commands are there for horizontal spacing?
At least in terms of horizontal spacing.

I'm thinking more along the lines of such things as

\llap
\rlap
\clap
\smash
\mathllap
\mathrlap
\mathclap

And the phantoms:

\phantom
\hphantom
\vphantom

And I'm sure I've forgotten a few.

What would be nice in addition is to not only have some kind of an explanation of the uses and differences of these commands but also a pointer to where someone can find documentation for them. For example,

I know I can do texdoc mathtools and find documentation for \mathllap etc. But I don't know where to find documentation for \smash and the various phantoms.

EDIT

Sometime I don't always get the result I expect

For example, \phantom doesn't always behave like I would like such as in this example,

\documentclass{article}
\usepackage{amsmath,amssymb}
\pagestyle{empty}
\begin{document}

    \begin{align*}
        y &= x - z\raisebox{0ex}[0pt][0pt]{\rule[-4ex]{0.1pt}{4ex}} \\
          &= x \mathop{\phantom{-}} z\raisebox{0ex}[0pt][0pt]{\rule{0.1pt}{4ex}} \\
          &= x \phantom{ - } z \\
    \end{align*}
    \hspace*{\fill}Or even worse:\hspace*{\fill}
    \begin{align*}
        y &= x - z\raisebox{0ex}[0pt][0pt]{\rule[-4ex]{0.1pt}{4ex}} \\
          &\phantom{= x -} z\raisebox{0ex}[0pt][0pt]{\rule{0.1pt}{4ex}} \\
    \end{align*}

\end{document}

enter image description here

Best Answer

Calling these spacing commands is a bit misleading they are all commands to set boxes of specified dimensions.

\rlap, \llap \clap are essentially \makebox[0pt][r], \makebox[0pt][l] and \makebox[0pt][c] except they can avoid the complication of looking for optional arguments etc. Also they differ in the way that \hbox differs from \mbox in that following the plain TeX rather than LaTeX tradition they lack a \leavevmode at the start of their definition so they do not start a paragraph if used in vertical mode.

In text mode \smash is in the same way essentially \raisebox{0pt}[0pt][0pt]

In text mode, the phantom commands are all equivalent to using an empty box with dimensions given by another box, so \phantom is essentially

\def\Phantom#1{\savebox{0}{#1}\savebox{2}{}%
    \ht2=\ht0 \dp2=\dp0 \wd2=\wd0
    \usebox{2}}

\vphantom is the same except that the width is forced to 0pt rather than the original width of the text in the first box.

The math mode versions are all essentially the same except for two complications, math mode has to be re-entered inside the box, and a \mathchoice construct has to be used so they work correctly at display and script sizes. \phantom and \smash have built in math mode tests and then switch between the text and math definitions automatically. For \rlap (just for historical reasons) it is text mode only, so for math mode you need to switch batc to math explictly or use \mathrlap from a suitable package.

Note that in \mathmode always makes a mathord atom which gets no special spacing. In the examples in the question you compared the spacing of - with the mathord spacing and the mahtop spacing but - is a mathbin atom so you need \mathbin{\phantom{-}}

\documentclass{article}
\usepackage{amsmath,amssymb}
\pagestyle{empty}
\begin{document}

    \begin{align*}
        y &= x - z\raisebox{0ex}[0pt][0pt]{\rule[-4ex]{0.1pt}{4ex}} \\
          &= x \mathbin{\phantom{-}} z\raisebox{0ex}[0pt][0pt]{\rule{0.1pt}{4ex}} \\
          &= x \mathbin{\phantom{ - }} z \\
    \end{align*}
    \hspace*{\fill}Or even worse:\hspace*{\fill}
    \begin{align*}
        y &= x - z\raisebox{0ex}[0pt][0pt]{\rule[-4ex]{0.1pt}{4ex}} \\
          &\phantom{{}= x -{}} z\raisebox{0ex}[0pt][0pt]{\rule{0.1pt}{4ex}} \\
    \end{align*}

\end{document}

enter image description here

Even then spacing of subscripts may be different

\phantom{P}_x

is like

{P{}}_x

in which the subscript is not the same as

P_x

It's pretty hard to avoid that as the boxing implied by phantom obscures the kerning information in the font metrics, but fortunately it is rare to want a visible subscript on an invisible base.

Related Question