3

I regularly use \phantom to visually align things in my documents, and often have to manually fill in the spaces that are cut off by removing the atoms (which I don't fully understand) between the objects before and after the \phantom---normally this works by adding {}'s inside the \phantom argument, but not always.

With the help of https://tex.stackexchange.com/a/95923/42225, I've cobbled together the following, but I feel that there should be a more elegant solution that doesn't require the preceding and following objects as arguments (which I've made optional).

\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{xparse}

\makeatletter
\NewDocumentCommand{\archphantom}{O{{}} m O{{}}}{%
    \setbox0\hbox{$#1#2#3$}%
    \setbox1\hbox{$#1{#2}#3$}%
    \dimen@\dimexpr\wd0-\wd1%
    #1\phantom{#2}\kern\dimen@#3%
}
\makeatother

\begin{document}

\begin{align*}
    x & = \fbox{\hspace{5cm}\vphantom{b}}+\ldots\\
    &\archphantom{=}[\ldots] + \fbox{\hspace{5cm}\vphantom{b}} && \text{works}\\
    &\phantom{=}\,\:\ldots + \fbox{\hspace{5cm}\vphantom{b}} && \text{trial $\&$ error}\\
    &\phantom{{}={}}\ldots + \fbox{\hspace{5cm}\vphantom{b}} && \text{doesn't work}
\end{align*}

\end{document}

For this case in particular (=+\ldots), I wasn't able to simulate the correct spacing by e.g. \phantom{{}={}}\ldots, which motivated me to finally ask this question. ;-)

As a side remark, I'm concurrently looking for good name suggestions for the resulting command (or feedback to my choice). I've first thought about \phantombridge, since I need to take into account the objects before and after---metaphorically the two visible abutments of an invisible bridge. Now, however, since an arch is basically the same concept, I prefer \archphantom, which also has the added connotation of "a \phantom of higher rank" (compare angel vs. archangel).

Axel
  • 791
  • 2
    unrelated to the question but don't use box1 for local assignments, use box 2 (and don't have a % after \wd1 (or \wd2) – David Carlisle Mar 27 '14 at 11:23
  • Discussion of \phantom and its surrounding material was discussed in the several answers (and their comments) at http://tex.stackexchange.com/questions/166327/position-of-the-slash-in-hatted-slashed/ – Steven B. Segletes Mar 27 '14 at 11:30
  • @DavidCarlisle, thanks for the tip! Could you briefly explain (or point me to a reference) why the box1 shouldn't be used? – Axel Mar 27 '14 at 12:07
  • 1
    for all register types (box,dimen,count,...) 0,2,4,6,8 are scratch registers for local assignment and 1,3,5,7,9 are scratch registers for global assignments. Originally this convention specified in the tex book for plain tex but used consistently in latex too. – David Carlisle Mar 27 '14 at 14:46

1 Answers1

5

The spacing is affected by the math class of the atom and the adjacent atoms. The usual trick of adding {} allows a \mathrel atom to stay as a relation and not turn effectively into a mathord and lose its spacing, but as you note it doesn't always get the correct spacing if the adjacent items are not mathord.

You can use

\mathrel{\phantom{=}}

which hides = and the re-asserts the \mathrel class.

It would be possible to steal some code from bm package to interrogate the math class of the argument and add the \mathrel automatically, but I don't think I'd do that, it would make the code very fragile.

Note that if the argument contains more than a single symbol then in some cases the spacing will be affected anyway as boxing any expression freezes the stretchy glue at its natural width, whereas the the unboxed version may be stretched or shrunk, depending.

David Carlisle
  • 757,742