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 \multicolumn{1}{l}{Header 1} \\
\hline $a = \sum_j b(c)$ & $c = d$ \\
\hline \multicolumn{1}{l}{Header 2} \\
\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?
Best Answer
tabular
Balancing the horizontal space
The equal signs aren't aligned because the columns are being
c
entered.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 secondaligned
environment is probably somewhere around thev
.I introduced two macros that—with a little help—take care of that:
\leftlap[<opt>]{<widest part>}{<actual output>}
:<widest part>
will be\phantom
ed and is to set to the widest input that will be used at this (equation) column.<opt>
: There is some horizontal space before thealigned
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>}
When the longest part appears in the sans-
aligned
mathThen you use the macros in the
aligned
environment:Note the
\!
in the optional argument. This removes the aforementioned horizontal space that is introduced by thealigned
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 twoaligned
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
Output
Extra columns
If you do not want the equal sign to be the default, you can use
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
Output
alignat
Another approach would be the direct use of one of
amsmath
s*align*
environments.The next step (if the
\hline
s are needed) would be a TikZ\matrix
solution but I guess this would go a little bit too far.Code
Output