11

I want to align contents of my equations inside an already aligned environment like align:

Good alignment using the mathcolor command

Below is what I get using the \phantom command as a trick to get the previous spacing together with a minimal working example:

Bad alignment using the phantom command

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{amsmath}

\begin{document}
\begin{align*}
  N &= (-9) - (+5) &
  O &= (+7) - (-8) &
  P &= (+5) + (-6) &
  Q &= (-3) + (+2) \\
  %
  &=\phantom{(} -9 \phantom{)-(} -5 &
  &=\phantom{(+} 7 \phantom{)-(} +8 &
  &=\phantom{(+} 5 \phantom{)+(} -6 &
  &=\phantom{(} -3 \phantom{)+(} +2
\end{align*}
\end{document}

It seems like I'm encountering a similar problem as in question 28075 (Phantom width of binary operator). Reading this answer to question 38984, I have the feeling I'm losing the character's class and hence loosing the "natural" spacing.

Is there a proper way to add phantom space in math mode?

6
  • 1
    Wrapping the phantoms in \mathopen might help (untested)
    – daleif
    Sep 22, 2016 at 16:05
  • As a workaround for now, I use the \mathcolor command defined in this answer to question 85035 to display white text...
    – remjg
    Sep 22, 2016 at 16:05
  • @daleif it works perfectly ! Does it force the caracter to be of class 4 ?
    – remjg
    Sep 22, 2016 at 16:08
  • It leaves the stream in a state where an opening char has just been passed
    – daleif
    Sep 22, 2016 at 16:25
  • @remjg: Would you consider this alternative another possibility or perhaps an answer? code / output
    – Werner
    Sep 23, 2016 at 0:19

5 Answers 5

12

Easy fix: Just surround the unary minus and plus signs in curly brackets { }:

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{amsmath}

\begin{document}
\begin{align*}
  N &= (-9) - (+5) &
  O &= (+7) - (-8) &
  P &= (+5) + (-6) &
  Q &= (-3) + (+2) \\
  %
  &=\hphantom{(} {-}9 \hphantom{)-(} {-}5 &
  &=\hphantom{(+} 7 \hphantom{)-(} {+}8 &
  &=\hphantom{(+} 5 \hphantom{)+(} {-}6 &
  &=\hphantom{(} {-}3 \hphantom{)+(} {+}2
\end{align*}
\end{document}

Result

The rules for binary atoms are a little special to distinguish a binary minus (a + b) from a unary minus (-1). In the binary case, there is additional space around the operator, which is missing in the latter case.

TeX allows to stay a binary operator, if it is surrounded by compatible context: a + b, 4 + \int, ... Other atoms like an open parentheses on the left prevents the binary spacing: (-9) is set without additional spaces. However, the property of the open parentheses does not go outside the \phantom. In this case TeX considers the whole subformula \phantom{(} as acceptable partner for a binary minus and the spacing increases.

\mathord{-} or the shorter {-} forces TeX to set a unary minus. The latter works, because standalone curly braces in math mode make a subformula, whose acts as \mathord.

6

An aligned TABstack:

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{tabstackengine}
\stackMath
\setstackaligngap{0pt}
\begin{document}
\[
%\TABunaryLeft% IS ALREADY THE DEFAULT
\alignstackanchor[8pt]{
  N =& (&-9&) - (&+5&) &\quad
  O =& (&+7&) - (&-8&) &\quad
  P =& (&+5&) + (&-6&) &\quad
  Q =& (&-3&) + (&+2&) 
  }{
    =& & -9&  &-5 &&
    =& &  7&  &+8 &&
    =& &  5&  &-6 &&
    =& & -3&  & +2&
}
\]
\end{document}

enter image description here

5

You can get away with phantoms, declaring the right type, such as \mathopen{\hphantom{(}} or \mathbin{\hphantom{+}}, but perhaps a more customizable version is better.

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{amsmath,array}

\begin{document}
\begin{equation*}
\renewcommand{\arraystretch}{1.5}
\setlength{\arraycolsep}{0pt}
\newcolumntype{E}{
  r % left-hand side
  >{{}}c<{{}} % equals
  c % open parenthesis
  r % number
  c % closed parenthesis
  >{{}}c<{{}} % operator
  c % open parenthesis
  r % number
  c % closed parenthesis
}
\newcolumntype{?}{@{\hspace{.5em}}|@{\hspace{.5em}}}
\begin{array}{E ? E ? E ? E}
  N &= &(& -9 &)& - &(& +5 &)&
  O &= &(& +7 &)& - &(& -8 &)&
  P &= &(& +5 &)& + &(& -6 &)&
  Q &= &(& -3 &)& + &(& +2 &) \\
  %
    &= & & -9 & &   & & -5 & &
    &= & &  7 & &   & & +8 & &
    &= & &  5 & &   & & -6 & &
    &= & & -3 & &   & & +2 &
\end{array}
\end{equation*}
\end{document}

You can redefine the column type ? if you don't want rules which, in this case, may be better in order to clearly separate the various expressions.

enter image description here

2

You can do that with alignedat and no \phantom:

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{amsmath}

\begin{document}

\begin{align*}
  N &=\!\begin{alignedat}[t]{2}%
  ( -& 9 &) - (+ &5)\\
  - & 9 & {} - {} & 5 & %
  \end{alignedat}
  &
  O &=\!\begin{alignedat}[t] {2}%
  ( + &7 &) - (-& 8)\\
  & 7 & {}+{} & 8%
  \end{alignedat}
  & P &=\!\begin{alignedat}[t]{2}%
  ( + &5 &) + (- &6)\\
  & 5 &{} -{}&6%
  \end{alignedat}
  & Q &=\!\begin{alignedat}[t]{2}%
  ( -&3 &) + ( +& 2)\\
  - &3 & {}+{} & 2%
  \end{alignedat}
\end{align*}

\end{document} 

enter image description here

3
  • you're getting binary, rather than unary, pluses and minuses in the second line. wrap them in braces to make them "ordinary" characters. Sep 22, 2016 at 16:41
  • @barbara beeton: I had to fiddle with this problem to obtain it! In my opinion, semantically, they have to be binary.
    – Bernard
    Sep 22, 2016 at 16:46
  • that's a reasonable explanation. i withdraw my suggestion (but leave the comment since someone else may have the same reaction). Sep 22, 2016 at 16:55
2

Rather than using \phantom{+}, which is (roughly speaking) interpreted as \phantom{{+}}, i.e., not a relation, use

\mathrel{\phantom{+}}

which forces \phantom{+} to be treated as a relation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .