[Tex/LaTex] How to add phantom space in math mode without losing “natural” spacing

math-modespacing

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?

Best Answer

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.