Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

@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

share|improve this question
    
Partial duplicate: What commands are there for horizontal spacing? –  Werner Feb 18 '13 at 18:45
    
@Werner, I would say that link falls into the first category about \vspace and \hspace and while it does mention \hphantom it doesn't mention \llap and the rest. I'd say my question falls somewhere between the link about boxes and your suggested duplicate. –  A.Ellett Feb 18 '13 at 18:48
    
Perhaps I should change my title to something better? Not sure what. –  A.Ellett Feb 18 '13 at 18:50
    
@A.Ellett \smash is defined in tex/plain/base/plain.tex and does get redefined by amsmath (to accept an optional argument). The phantom family, \llap and \rlap are also defined in plain.tex. \clap was born in a TUG article and is also defined by mathtools. –  Qrrbrbirlbel Feb 18 '13 at 19:17

1 Answer 1

up vote 6 down vote accepted

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][r] 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.

share|improve this answer
    
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
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.