# Centering terms of an equation inside an aligned environment

I have the following equation inside an aligned environment:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\expr}[1]{\delta\Delta\Gamma_{#1}}

\begin{document}

\begin{aligned} X &= \expr{A} + \expr{BC} + \expr{D}\\ &= \expr{A} + \expr{B} + \expr{C} + \expr{D} \end{aligned}

\end{document}


I would like to align the first and second + signs on the first line with the first and last + signs on the second line. I have used a word processor to show you what I want:

In other words, the \expr{BC} is centered. What is the best way to accomplish this?

## 4 Answers

No need to guess a good spacing, because TeX can do the work.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}% first solution
\usepackage{eqparbox}% second solution

%%% for the second solution
\newcommand{\same}[2]{\eqmakebox[#1][c]{$\displaystyle#2$}}
%%%

\newcommand{\expr}[1]{\delta\Delta\Gamma_{#1}}

\begin{document}

$$\renewcommand{\arraystretch}{1.2} \setlength{\arraycolsep}{0pt} \begin{array}{*{9}{ >{{}}c<{{}} }} X &=& \expr{A} &+& \multicolumn{3}{c}{\expr{BC}} &+& \expr{D} \\ &=& \expr{A} &+& \expr{B} &+& \expr{C} &+& \expr{D} \end{array}$$

\begin{aligned} X &= \expr{A} + \same{X}{\expr{BC}} + \expr{D} \\ &= \expr{A} + \same{X}{\expr{B} + \expr{C}} + \expr{D} \end{aligned}

\end{document}


The \same macro for the second solution takes as first argument an arbitrary string of characters that must be unique throughout the document for the particular parts you want to have the same width. In other words, if you use the same construction elsewhere, you need to pass something different from X.

With matrix or with array:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\expr}[1]{\delta\Delta\Gamma_{#1}}

\begin{document}

$$\setlength\arraycolsep{0.5pt} \begin{matrix} X & = \expr{A} + & \expr{BC} & + \expr{D}\\ & = \expr{A} + & \expr{B} + \expr{C} & + \expr{D} \end{matrix}$$

$$\setlength\arraycolsep{0.5pt} \begin{array}{crcl} X &= \expr{A} + & \expr{BC} & + \expr{D}\\ &= \expr{A} + & \expr{B} + \expr{C} & + \expr{D} \end{array}$$

\end{document}


A solution based on eqparbox (requires two compilations):

\documentclass{article}
\usepackage{amsmath}
\newcommand{\expr}[1]{\delta\Delta\Gamma_{#1}}
\usepackage{eqparbox}

\begin{document}

\begin{alignedat}{3} X &= \expr{A} & & + \eqmakebox[E]{\expr{BC}} & & + \expr{D}\\ &= \expr{A} & & + \eqmakebox[E]{\expr{B} + \expr{C}} & & + \expr{D} \end{alignedat}

\end{document}


There are other very fine solutions, I just want to show an alternative, what I'm using mostly for all my equations (but not seeing very often on this site). IEEEeqnarray is an environment from the IEEEtran document class, but you can load only the macros with \usepackage{IEEEtrantools}. The equation array is a bit more complex at first, but can solve many more complicated alignment situations. That's what I like: A solution that's "fits all". Here is your example:

\documentclass{article}
\usepackage{IEEEtrantools}
\newcommand{\expr}[1]{\delta\Delta\Gamma_{#1}}

\begin{document}

\begin{IEEEeqnarray*}{rCcCcCc}
X &=& \expr{A} &+&        \expr{BC}     &+& \expr{D} \\
&=& \expr{A} &+& \expr{B}  + \expr{C} &+& \expr{D}
\end{IEEEeqnarray*}

\end{document}


IEEEeqnarray works by explicitly defining the used columns between alignment points. You can use r, c and l for right, center and left alignment. Their upper case versions additionally apply an operator spacing around the column. In your example every second column is an operator, thats why I'm using the upper case variant there.

IEEEeqnarray is of course a bit more verbose than e.g. align, but IMHO that pays out. You can check out the documentation at https://www.ctan.org/pkg/ieeetrantools.