# Align Equations Over Multiple Tabular Rows

I'm trying to set up a table with some equations in it, however I would like the equations on multiple rows to be aligned properly. So far I have

\begin{tabular}{cc}
\hline $a = \sum_j b(c)$ & $c = d$ \\
\hline \begin{aligned} e&=fghij(y) \\ klm(x)&=nop \end{aligned} &
\begin{aligned} qr&=vwxyzstu \\ abcde&=fghij \end{aligned}
\end{tabular}


which looks like

Note how the equals signs in the top row are not aligned with the corresponding equals signs in the bottom two rows.

So my question is; is there a way to align across different rows or am I going about this completely the wrong way?

• You could introduce columns that are separated at the equal sign … – Qrrbrbirlbel Oct 23 '12 at 13:47
• @Qrrbrbirlbel I don't know why that didn't occur to me. It leaves a big gap between the first term and the equals but this can be removed with \hskip. -0.3cm seems to look okay, see here – Dan Oct 23 '12 at 13:55
• The use of arrays > and < combined with the standard @ syntax might come in handy here. (See my answer.) – Qrrbrbirlbel Oct 23 '12 at 14:00

# tabular

## Balancing the horizontal space

The equal signs aren't aligned because the columns are being centered.

The horizontal center of the a = \sum_j b(c) is not the at the equal sign but somewhere around the \sum. Just as the equal sign of your second aligned environment is probably somewhere around the v.

I introduced two macros that—with a little help—take care of that:

• \leftlap[<opt>]{<widest part>}{<actual output>}:
• <widest part> will be \phantomed and is to set to the widest input that will be used at this (equation) column.
• <opt>: There is some horizontal space before the aligned environment. Empirically I could find that that space equals the amount of \,, therefore it is automatically inserted. If this shouldn't happen set the optional argument to nothing (i.e. type []).
• \rightlap{<widest part>}{<actual output>}

\newcommand*\leftlap[3][\,]{#1\hphantom{#2}\mathllap{#3}}
\newcommand*\rightlap[2]{\mathrlap{#2}\hphantom{#1}}


### When the longest part appears in the sans-aligned math

Then you use the macros in the aligned environment:

\begin{tabular}{c}
$vwxyzstu^{long} = x$ \\
\begin{aligned} \leftlap[\!]{vwxyzstu^{long}}{a} & = \rightlap{x}{b} \\ c & = d \end{aligned}
\end{tabular}


Note the \! in the optional argument. This removes the aforementioned horizontal space that is introduced by the aligned environment. (\! expands to the same horizontal space as \, just negative.)

Output

One can easily see that the space between two tabular lines is smaller than between two aligned lines. This can be one of the reasons that the following solution (titled „Extra columns”) should be preferred and is a lot easier to maintain.

### Code

\documentclass{article}
\usepackage{mathtools}
\usepackage{xcolor} % used only to show the phantomed stuff
\renewcommand\hphantom[1]{{\color[gray]{.6}#1}} % comment out!
\setlength\fboxsep{0pt} \setlength\fboxrule{.2pt} % for the \fboxes

\newcommand*\leftlap[3][\,]{#1\hphantom{#2}\mathllap{#3}}
\newcommand*\rightlap[2]{\mathrlap{#2}\hphantom{#1}}

\begin{document}
\begin{tabular}{cc} \hline
\fbox{$\leftlap{klm(x)}{a} = \rightlap{fghij(y)}{\sum_j b(c)}$}  & $\leftlap{abcde}{c} = \rightlap{vwxyzstu}{d}$ \\ \hline
\fbox{\begin{aligned} e & = fghij(y) \\ klm(x) & = nop \end{aligned}} & \begin{aligned} qr & = vwxyzstu \\ abcde & = fghij \end{aligned}
\end{tabular}
\end{document}


## Extra columns

If you do not want the equal sign to be the default, you can use

\newcolumntype{M}{R@{$\;$}L}


instead. This introduces the right space between the left side of the equation and the = sign. The right part of the equation can then started as always, e.g. & = \sum_j.

### Code

\documentclass{article}
\usepackage{amsmath,array}
\newcolumntype{R}{>{$}r<{$}}
\newcolumntype{L}{>{$}l<{$}}
\newcolumntype{M}{R@{${}={}$}L}
\begin{document}
\begin{tabular}{MM}\hline
a      & \sum_j b(c) & c     & d        \\ \hline
e      & fghij(y)    & qr    & vwxyzstu \\
klm(x) & nop         & abcde & fghij    \\
\end{tabular}
\end{document}


# alignat

Another approach would be the direct use of one of amsmaths *align* environments.

The next step (if the \hlines are needed) would be a TikZ \matrix solution but I guess this would go a little bit too far.

## Code

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat*}{2}
\rlap{Header 1} \hphantom{klm(x)} &                          &                          &            \\
a & = \textstyle \sum_j b(c) &                        c & = d        \\
\rlap{Header 2} \hphantom{klm(x)} &                          &                          &            \\
e & = fghij(y)               &                       qr & = vwxyzstu \\
klm(x) & = nop                    & \hspace{\columnsep}abcde & = fghij    \\
\end{alignat*}
\end{document}


## Output

This is similar to the "Extra columns" proposal by Qrrbrbirlbel, but easier to manage.

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

\newcolumntype{A}{ >{$} r <{$} @{} >{${}} l <{$} } % A for "align"
%% (1) "r" column in math mode:          >{$} r <{$}
%% (2) no space:                         @{}
%% (3) "l" column in math mode, with
%%     an empty subformula at the start: >{${}} l <{$}

\begin{document}
\begin{tabular}{AA}
\toprule
\midrule
a      &= \sum_j b(c) & c     &= d \\
\midrule
\midrule
e      &= fghij(y)    & qr    &= vwxyzstu \\
klm(x) &= nop         & abcde &< fghij \\
\bottomrule
\end{tabular}

\end{document}


The A column type emulates a pair of align columns, so for each of them the syntax is

<left> &= <right>


Using this is better as it doesn't hardwire the equals sign in the column type, as it can be seen in the last bit where an inequality is typeset just as an example.

This can also be adapted to tabular*, with some more work.

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

\newcolumntype{A}{
>{$}r<{$}
@{\extracolsep{0pt}}
>{${}} l <{$}
@{\extracolsep{\fill}}
} % A for "align"
%% (1) "r" column in math mode:          >{$} r <{$}
%% (2) no space:                         @{}
%% (3) "l" column in math mode, with
%%     an empty subformula at the start: >{${}} l <{$}

\begin{document}

\noindent
\begin{tabular*}{\textwidth}{
@{\hspace{\tabcolsep}\extracolsep{\fill}}
AAA
@{\hspace{\tabcolsep}}
}
\toprule
\midrule
a      &= \sum_j b(c) & c     &= d        & aaa &= bbb\\
\midrule
\midrule
e      &= fghij(y)    & qr    &= vwxyzstu & a   &= b\\
klm(x) &= nop         & abcde &< fghij    & x   &= 0\\
\bottomrule
\end{tabular*}

\end{document}


• Isn't this nearly the same as my \newcolumntype{M}{R@{$\;$}L} proposal, only that it uses a {} sub-formula instead of a hard-coded space? – Qrrbrbirlbel Oct 23 '12 at 21:12
• It's similar, yes; all solutions are equal, but some are more equal than others. :) More seriously, hardwiring = in the column specifier is surely wrong; and the \; spacer is not that good either. :) – egreg Oct 23 '12 at 21:33
• Yes, your solution is surely much cleaner. – Qrrbrbirlbel Oct 23 '12 at 21:39
• @egreg: is it possible to use tabularx or something else to set the width of the table to \linewidth and distribute whitespace evenly between cells? – Hotschke Mar 20 '17 at 15:56
• @Hotschke Yes. I added the code (with three pairs of columns, or it would make little sense). – egreg Mar 20 '17 at 16:21