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 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 \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 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
\multicolumn{2}{l}{Header 1} \\ \hline
\fbox{$\leftlap{klm(x)}{a} = \rightlap{fghij(y)}{\sum_j b(c)} $} & $\leftlap{abcde}{c} = \rightlap{vwxyzstu}{d} $ \\ \hline
\multicolumn{2}{l}{Header 2} \\ \hline
\fbox{$\begin{aligned} e & = fghij(y) \\
klm(x) & = nop \end{aligned}$} & $ \begin{aligned} qr & = vwxyzstu \\
abcde & = fghij \end{aligned}$
\end{tabular}
\end{document}
Output
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
\multicolumn{4}{l}{Header 1} \\ \hline
a & \sum_j b(c) & c & d \\ \hline
\multicolumn{4}{l}{Header 2} \\ \hline
e & fghij(y) & qr & vwxyzstu \\
klm(x) & nop & abcde & fghij \\
\end{tabular}
\end{document}
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
\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
One way is to shift up the skip prior to the mid-height \tabucline
with a \\[-1pt]
. See below for 2nd approach.
%Dokumentclass
\documentclass[landscape]{letter}
%Packages
\usepackage[landscape]{geometry}
\usepackage[utf8] {inputenc}
\usepackage {multirow}
\usepackage {tabu}
%Formatting
\pagestyle{empty} %Remove numbering
\begin{document}
\begin{tabu}{ |[1pt] l | l l | l | l |[1pt] }
\tabucline[1 pt]{1-5}
\multicolumn{2}{ |[1pt] l }{ } & \multicolumn{3}{ |[1pt] l |[1pt]}{ } \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & \multicolumn{2}{ l |[1pt] }{ } \\ \hline
y & & \multicolumn{1}{ |[1pt] l | }{ } & \multicolumn{2}{ l |[1pt] }{ } \\[-1pt] \tabucline[1 pt]{3-5}
\hline
x & & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \tabucline[1 pt]{1-5}
\end{tabu}
\end{document}
Alternately, if you want the thin line to bisect the thick line, then the approach: \\[-1pt] \tabucline[1 pt]{3-5}\\[-12.5pt]\hline
works.
%Dokumentclass
\documentclass[landscape]{letter}
%Packages
\usepackage[landscape]{geometry}
\usepackage[utf8] {inputenc}
\usepackage {multirow}
\usepackage {tabu}
%Formatting
\pagestyle{empty} %Remove numbering
\begin{document}
\begin{tabu}{ |[1pt] l | l l | l | l |[1pt] }
\tabucline[1 pt]{1-5}
\multicolumn{2}{ |[1pt] l }{ } & \multicolumn{3}{ |[1pt] l |[1pt]}{ } \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & \multicolumn{2}{ l |[1pt] }{ } \\ \hline
y & & \multicolumn{1}{ |[1pt] l | }{ } & \multicolumn{2}{ l |[1pt] }{ } \\[-1pt] \tabucline[1 pt]{3-5}\\[-12.5pt]
\hline
x & & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \hline
& & \multicolumn{1}{ |[1pt] l | }{ } & & \\ \tabucline[1 pt]{1-5}
\end{tabu}
\end{document}
Best Answer
I propose a solution based on
ltablex
, which combines the functionalities oflongtable
andtabularx
. I supposed the widths for yourp(somelength)
were calculated by some software. This can be replaced with the>{\hsize= xx\hsize}
directive if you need to adjust the widths of theX
columns. The remainingp{…}
column in the following code was replaced withm{…}
to ensure vertical centring of the contents of the cells.