9

@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

Moriambar
  • 11,466
A.Ellett
  • 50,533

1 Answers1

14

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.

David Carlisle
  • 757,742
  • Should I change the title then to something like "What are various ways of creating boxes of zero width or height?" – A.Ellett Feb 18 '13 at 20:38
  • Also, I was curious, is there something like \mphantom that would work correctly as a phantom within a math environment? \phantom doesn't always seem to do what I want when in math mode (though right now no particular example comes to mind). – A.Ellett Feb 18 '13 at 20:40
  • \phantom is an ifmmode switch already and then does a text mode and math mode version, depending – David Carlisle Feb 18 '13 at 20:41
  • I'd edited my question to show what I'm talking about with \phantom. – A.Ellett Feb 18 '13 at 20:47
  • You say \phantom{P}_x is like {P{}}_x, why didn't you just say it is like P{}_x? The results look the same to me. – A.Ellett Feb 18 '13 at 21:15
  • well because what I said is more correct:-) P{}_x is a P mathord character atom an empty mathord atom with a subscript node. So in particular the position of the script is unaffected by the size of the P it is just on {} {P{}}_x is a mathord atom with a subscript and base containing P and an empty mathord atom so that is like the phantom case where the subscript is a script of the whole white box – David Carlisle Feb 18 '13 at 21:41
  • of course, \phantom{{} - {}} would give the same result as the \mathbin construct, but your explanation provides more meaning to someone who needs to know why this is needed. – barbara beeton Feb 18 '13 at 21:51
  • 1
    @barbarabeeton in this case but the external mathbin is better as then you get whatever spacing _ would have in the current context which might not be {}-{} if you were start of list or an adjacent mathbin or... – David Carlisle Feb 18 '13 at 22:05
  • Should the second \makebox command in the second paragraph be \makebox[0pt][l]? (i.e. [l] instead of [r]) I don't have rep here to make the edit myself and it's too few characters to propose the edit. – jedwards Jun 24 '16 at 17:45