3

I'm trying to create a table with math that is properly aligned horizontally but can also be centred across multiple rows. I've tried two different methods (one using a solution from this answer) and both result in slight alignment errors, as shown.

Output of MWE

I've previously tried using an aligned environment as in this example but as far as I can tell there is no way to align math across multiple such environments or insert a cmidrule into one.

Is there a way to modify either of these solutions to fix the errors shown? What's the best solution to align math in this way?

\documentclass{report}
\usepackage{amsmath,array,booktabs,multirow}

% From https://tex.stackexchange.com/questions/78788/align-equations-over-multiple-tabular-rows/78839#78839
\newcolumntype{A}{ >{$} r <{$} @{} >{${}} l <{$} } % A for "align"

\begin{document}

%   FIXED COLUMN DIVIDER
\begin{table}[ht]
    \centering
    \caption{Fixed column divider}
    \begin{tabular}{ l r@{ = } l r@{ $\leftarrow$ } l }
        \toprule
        \textbf{Name}
        &   \multicolumn{2}{l}{\textbf{Math}}   
        &   \multicolumn{2}{l}{\textbf{More Math}}      \\
        \midrule
        \multirow{2}{*}{Foo}
        &   $A$
        &   $x^2 + y^3$
        &   $x$
        &   $x(1 - \alpha)$                         \\
        &   $BC$
        &   $x^3 - y^2$         
        &   $y$
        &   $y(1 + \beta)$                          \\
        \cmidrule{4-5}
        \multirow{2}{*}{Bar}
        &   $D$
        &   $xy + w_z$
        &   \multirow{2}{*}{$w_z$}
        &   \multirow{2}{*}{$w_z(1 + q)$}           \\  
        &   \multicolumn{2}{l}{Note: if $A > D \text{ then } D \leftarrow AB + C$}
        &   \multicolumn{2}{l}{}                    \\
        \bottomrule
    \end{tabular}   
\end{table}

%   NEW COLUMN TYPE
\begin{table}[ht]
    \centering
    \caption{New column type}
    \begin{tabular}{ l A A }
        \toprule
        \textbf{Name}
        &   \multicolumn{2}{l}{\textbf{Math}}   
        &   \multicolumn{2}{l}{\textbf{More Math}}      \\
        \midrule
        \multirow{2}{*}{Foo}
        &   A   &= x^2 + y^3
        &   x   &\leftarrow x(1 - \alpha)           \\
        &   BC  &= x^3 - y^2            
        &   y   &\leftarrow y(1 + \beta)            \\
        \cmidrule{4-5}
        \multirow{2}{*}{Bar}
        &   D   &= xy + w_z
        &   \multirow{2}{*}{$w_z$}
        &   \multirow{2}{*}{$\leftarrow w_z(1 + q)$}\\  
        &   \multicolumn{2}{l}{Note: if $A > D \text{ then } D \leftarrow AB + C$}
        &   \multicolumn{2}{l}{}                    \\
        \bottomrule
    \end{tabular}   
\end{table}

\end{document}
  • I take it you want $w_z$ aligned with Bar to the left and $x$ above? – John Kormylo Apr 18 '18 at 20:38
  • Correct! Specifically, the leftarrows in the $w_z$ side of the table should be aligned. – TACD Apr 19 '18 at 9:22
2

You can place the LHS of each equation inside a box that's measurable and then adjust it so that all those in the second [third] column have the same-width LHS. This approach is made easy using eqparbox which uses a \label-\ref-like approach to store the maximum box width (requires two compilation on the first go or any subsequent changes).

\eqmathbox is similar to \eqparbox, retaining the math context. The syntax \eqmathbox[<tag>][<align>]{<stuff>} aligns <stuff> with the same <tag> based on the preferred <align>ment specified. This is ultimately done where <stuff> is placed inside a box of maximum width across all <stuff>s with the same <tag>.

enter image description here

\documentclass{article}

\usepackage{amsmath,eqparbox,booktabs,xparse}

% https://tex.stackexchange.com/a/34412/5764
\makeatletter
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
  \IfValueTF{#1}
    {\def\eqmathbox@##1##2{\eqmakebox[#1][#2]{$##1##2$}}}
    {\def\eqmathbox@##1##2{\eqmakebox{$##1##2$}}}
  \mathpalette\eqmathbox@{#3}
}
\begin{document}

\begin{tabular}{ *{3}{l} }
  \toprule
  \textbf{Name} & \textbf{Math} & \textbf{More Math} \\
  \midrule
  \raisebox{-.5\normalbaselineskip}[0pt][0pt]{Foo} &
    $\eqmathbox[L][r]{A} = x^2 + y^3$ &
    $\eqmathbox[R][r]{x} \leftarrow x (1 - \alpha)$ \\
  &
    $\eqmathbox[L][r]{BC} = x^3 - y^2$ &
    $ \eqmathbox[R][r]{y} \leftarrow y (1 + \beta)$ \\
  \cmidrule{3-3}
  \raisebox{-.5\normalbaselineskip}[0pt][0pt]{Bar} &
    $ \eqmathbox[L][r]{D} = x y + w_z$ &
    \raisebox{-.5\normalbaselineskip}[0pt][0pt]{%
      $\eqmathbox[R][r]{w_z} \leftarrow w_z (1 + q)$%
    } \\
  & Note: if $A > D$ then $D \leftarrow AB + C$ \\
  \bottomrule
\end{tabular}

\end{document}

Above I used \raisebox{<len>}[0pt][0pt]{<stuff>} in lieu of \multirow since it's not really part of the question, nor needed.

  • This works great, thanks! Replacing \raisebox{<len>}[0pt][0pt]{<stuff>} with \multirow{2}{*}{<stuff>} appears to allow me to easily modify the solution to vertically center over n rows. Is there a reason I should be preferring \raisebox? – TACD Apr 19 '18 at 9:32
  • @TACD: No; multirow is just another package dependency that is not always needed. – Werner Apr 19 '18 at 15:01
1

I wasn't sure it would work, but you can put \multirow inside a \multicolumn (just like \raisebox) and let it compute the offsets.

Note the phantom entries (including a phantom \leftarrow) in the last line to set up the alignment. Both cells are the widest for any row, so placement of the \multicolumn version is simple.

\documentclass{report}
\usepackage{amsmath,array,booktabs,multirow}

\begin{document}

%   FIXED COLUMN DIVIDER
\begin{table}[ht]
    \centering
    \caption{Fixed column divider}
    \begin{tabular}{ l r@{ = } l r@{ $\leftarrow$ } l }
        \toprule
        \textbf{Name}
        &   \multicolumn{2}{l}{\textbf{Math}}   
        &   \multicolumn{2}{l}{\textbf{More Math}}      \\
        \midrule
        \multirow{2}{*}{Foo}
        &   $A$
        &   $x^2 + y^3$
        &   $x$
        &   $x(1 - \alpha)$                         \\
        &   $BC$
        &   $x^3 - y^2$         
        &   $y$
        &   $y(1 + \beta)$                          \\
        \cmidrule{4-5}
        \multirow{2}{*}{Bar}
        &   $D$
        &   $xy + w_z$
        &   \multicolumn{2}{c}{\multirow{2}{*}{$w_z \leftarrow w_z(1 + q)$}}\\
        &   \multicolumn{2}{l}{Note: if $A > D \text{ then } D \leftarrow AB + C$}
        &   \multicolumn{1}{r@{ \phantom{$\leftarrow$} }}{\phantom{$w_z$}}% remove \leftarrowy
        &   \phantom{$w_z(1 + q)$}\\

        \bottomrule
    \end{tabular}   
\end{table}

\end{document}

demo

  • This works well, but it's not immediately obvious (to me at least) how to modify this for proper vertical alignment over n rows. – TACD Apr 19 '18 at 9:25
  • Note revised solution. This would have been slightly easier if you had not automated the \leftarrow. – John Kormylo Apr 19 '18 at 22:47
0

enter image description here

It is better to use math environments directly rather than adjust column properties. You may have noticed that in align environments, the spacing between entries is large for a table. In this case you can use \\[-1.5mm] just to reduce the spacing. You can replace 1.5 with whatever number you want, , but you must always be sure not to separate \\ from [-1.5mm].

The >{\hsize=scale\hsize} specification in tabularx environment can be used to specify the column width in terms of another, a very desirable feature. For instance, in the table above, the second row is set to be 70% larger than the third one. Here is the source code for the table shown above:

\documentclass{report}
\usepackage{amsmath,array,booktabs,multirow}
\usepackage{ragged2e}
\usepackage{tabularx}
\usepackage{makecell}

\newcolumntype{C}{ >{ \arraybackslash \Centering } X }
\renewcommand \tabularxcolumn [1] { >{ \centering } m{#1} }
\newcommand {\scalelinespace} [1] {\rule{0pt}{#1\normalbaselineskip}}

\begin{document}


\begin{table*}[h]


    \centering
    \begin{tabularx}{1\textwidth}{>{\hsize=0.5\hsize}C| >{\hsize=1.45\hsize}C| >{\hsize=1.05\hsize}C}

        \toprule

        \textbf{Name} & \textbf{Math} & \textbf{More Math} \\

        \midrule

        Foo
        & 
            $\begin{aligned}
                A &= x^{2} + y^{3} \\[-1.5mm]
                BC &= x^{3} - y^{2}
            \end{aligned}$
        &
            $\begin{aligned}
            x &\leftarrow x \thinspace (1 - \alpha)  \\[-1.5mm]
            y &\leftarrow y \thinspace (1 + \beta) 
            \end{aligned}$
        \\
        \Xcline{3-3}{0.2mm}
        \scalelinespace{1}
        Bar
        & 
        \scalelinespace{1}
            \hspace{4.1mm} $D = x y + w_{z}$
            \text{Note if } $A>D \text{ then } D \leftarrow AB+C$
        &
        \scalelinespace{1}
            $w_{z} \leftarrow w_{z} \thinspace (1 + q)$
        \\ 
        \bottomrule

    \end{tabularx}

\end{table*}




\end{document}
  • I apologise if my question was unclear — I'm not trying to align the math in each column horizontally, I'm trying to keep it left-justified but aligned with the primary math operator (so = for the second column and \leftarrow for the third). – TACD Apr 19 '18 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.