# Align two rows only using \phantom command

I want to achieve the following picture: What I have tried so far:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\begin{matrix} \phantom{(n+{}}2^3-1^3\phantom{{}-1)^3}&=&3\times1^2&+&3\times1&+&1\\ \phantom{(n+{}}3^3-2^3\phantom{{}-1)^3}&=&3\times2^2&+&3\times2&+&1\\ \phantom{(n+{}}4^3-3^3\phantom{{}-1)^3}&=&3\times3^2&+&3\times3&+&1\\ \phantom{(n+{}}\vdots\phantom{{}-{}}\vdots\phantom{{}-1)^3}&&\vdots&&\vdots&&\vdots\\ \phantom{(n+{}}n^3-(n-1)^3&=&3(n-1)^2&+&3(n-1)&+&1\\ (n+1)^3-n^3\phantom{{{}^3}-1)^3}&=&3n^2&+&3n&+&1\\\hline (n+1)^3-1^3\phantom{{{}^3}-1)^3}&=&3(\sum_{i=1}^ni^2)&+&3(\sum_{i=1}^ni)&+&n\\ &=&3(\sum_{i=1}^ni^2)&+&\dfrac{3n(n+1)}{2}&+&n\\ \end{matrix}$
\end{document} I think it's pretty well, however if we zoom on n^3-(n-1)^3 and (n+1)^3-n^3 we can see they are not perfectly aligned: I can't imagine another expr in \phantom{expr} than the one I put in the code:

• Before - sign: Since the longest expression is (n+1)^3 and I have n^3, the space that compensates I think it should be (n+{} so \phantom{(n+{}}.
• After - sign: Since the longest expression is (n-1)^3 and I have n^3, the space that compensates I think it should be {{}^3}-1)^3 so \phantom{{{}^3}-1)^3}.

What should be the 2 expressions that must go in \phantom?

P.S. I know I could go for adding & before and after - sign, but I think it is a better approach using \phantom. What do you think?

• I don't think that phantoms are a better approach. Jan 11 at 18:04
• you are already using an alignment so certainly using & is more natural than faking alignment with \phantom Jan 11 at 18:09
• I've taken the liberty of adding a few instructions to make your code snippet minimally compilable. Feel free to revert.
– Mico
Jan 11 at 18:56
• You can also use \mathmakebox and \mathclap (mathtools package) to align expresstions. Jan 11 at 21:31
• It is not so matter of correct as easy. In fact, \mathclap together with \phantom is proabably the easiest to figure out (but not to type). Jan 12 at 16:15

Not the best approach. Add alignment points and let TeX do the spacing.

In the second realization I compressed the spaces and made a few cosmetic changes.

\documentclass{article}
\usepackage{amsmath,array,booktabs}

\begin{document}

$\begin{array}{ @{} r @{}>{{}}c<{{}}@{} l c c c c c c @{} } 2^3 &-& 1^3 &=& 3\times1^2 &+& 3\times1 &+& 1 \\ 3^3 &-& 2^3 &=& 3\times2^2 &+& 3\times2 &+& 1 \\ 4^3 &-& 3^3 &=& 3\times3^2 &+& 3\times3 &+& 1 \\[-0.6ex] \vdots & & \vdots & & \vdots & & \vdots & & \vdots \\[0.2ex] n^3 &-& (n-1)^3 &=& 3(n-1)^2 &+& 3(n-1) &+& 1 \\ (n+1)^3 &-& n^3 &=& 3n^2 &+& 3n &+& 1 \\ \midrule[1pt] (n+1)^3 &-& 1^3 &=& 3(\sum_{i=1}^ni^2) &+& 3(\sum_{i=1}^ni) &+& n \\ \addlinespace & & &=& 3(\sum_{i=1}^ni^2) &+& \dfrac{3n(n+1)}{2} &+& n \end{array}$

$\setlength{\arraycolsep}{0pt} \begin{array}{ r >{{}}c<{{}} l >{{}}c<{{}} c >{{}}c<{{}} c >{{}}c<{{}} c } 2^3 &-& 1^3 &=& 3\times1^2 &+& 3\times1 &+& 1 \\ 3^3 &-& 2^3 &=& 3\times2^2 &+& 3\times2 &+& 1 \\ 4^3 &-& 3^3 &=& 3\times3^2 &+& 3\times3 &+& 1 \\[-0.6ex] \vdots & & \vdots & & \vdots & & \vdots & & \vdots \\[0.2ex] n^3 &-& (n-1)^3 &=& 3(n-1)^2 &+& 3(n-1) &+& 1 \\ (n+1)^3 &-& n^3 &=& 3n^2 &+& 3n &+& 1 \\ \midrule[1pt] (n+1)^3 &-& 1^3 &=& \displaystyle 3\biggl(\,\sum_{i=1}^ni^2\biggr) &+& \displaystyle 3\biggl(\,\sum_{i=1}^ni\biggr) &+& n \\ \addlinespace && &=& \displaystyle 3\biggl(\,\sum_{i=1}^ni^2\biggr) &+& \dfrac{3n(n+1)}{2} &+& n \end{array}$

\end{document} • @Mico No, I just forgot to remove them. Jan 11 at 20:11