2

This is the suggested fresh question by the comment by @egreg in the answer to the question Align Equations Over Multiple Tabular Rows

So the starting point is following code:

\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
\multicolumn{4}{l}{Header 1} \\
\midrule
a      &= \sum_j b(c) & c     &= d        & aaa &= bbb\\
\midrule
\multicolumn{4}{l}{Header 2} \\
\midrule
e      &= fghij(y)    & qr    &= vwxyzstu & a   &= b\\
klm(x) &= nop         & abcde &< fghij    & x   &= 0\\
\bottomrule
\end{tabular*}
\end{document}

Current features are:

  • width of the table is given by the user
  • columns in math mode which are aligned

How can I add columns which are not in math mode but are handled by tabular* or tabularx to distribute remaining whitespace between the columns evenly?

Update the columntypes are: AccAAl
With the help of the comment of David Carlisle I could get a running version:

\documentclass{article}
\usepackage{booktabs,array}
\newcolumntype{A}{ >{$}r<{$} @{\extracolsep{0pt}} >{${}} l <{$} @{\extracolsep{\fill}} }
\usepackage{blindtext}

\begin{document}
\blindtext
\begin{tabular*}{\textwidth}{
    @{\hspace{\tabcolsep}\extracolsep{\fill}}
    A
    @{\hspace{\tabcolsep}}
    @{\extracolsep{\fill}}c
    @{\extracolsep{\fill}}c
    @{\hspace{\tabcolsep}\extracolsep{\fill}}
    AA
    @{\hspace{\tabcolsep}}
    @{\extracolsep{\fill}}l
  }
  \toprule
  \multicolumn{2}{l}{title 1} & title 2 (c) & title 3 (c) & \multicolumn{2}{c}{title 4} & \multicolumn{2}{c}{title 5} & title 6 (l) \\
  \midrule
  a &= b       & cell 2     &   cell 3     &  c&=d          & e&=f & cell6 \\
  a+2 &= b       & cell 2     &   cell 3     &  c+3&=d          & e+4&=f & cell6 \\
  \bottomrule
\end{tabular*}
\blindtext
\end{document}

QUESTION How to properly align column headers which are obviously text and not equations. I tried this naively. However, it is most obvious in column 1, that it is not correct!

Hotschke
  • 5,300
  • 5
  • 33
  • 63

2 Answers2

3

I think you intend something like this (include @{} in the second argument of the \multicolumn):

\documentclass{article}
\usepackage{booktabs,array}
\newcolumntype{A}{
    >{$}r<{$}
    @{\extracolsep{0pt}}
    >{${}} l <{$}
    @{\extracolsep{\fill}}
}
\usepackage{blindtext}

\begin{document}
    \blindtext
    \vspace{\baselineskip}

    \noindent
    \begin{tabular*}{\textwidth}{ @{\hspace{\tabcolsep}\extracolsep{\fill}}
            AccAAl
            @{\hspace{\tabcolsep}}}
        \toprule
        \multicolumn{2}{r@{}}{title 1} & title 2 (c) & title 3 (c) & \multicolumn{2}{r@{}}{title 4} & \multicolumn{2}{r@{}}{title 5} & title 6 (l) \\
        \midrule
        a &= b       & cell 2     &   cell 3     &  c&=d          & e&=f & cell6 \\
        a+2 &= b       & cell 2     &   cell 3     &  c+3&=d          & e+4&=f & cell6 \\
        \bottomrule
    \end{tabular*}
    \vspace{\baselineskip}

    \blindtext
\end{document}

enter image description here

CarLaTeX
  • 62,716
  • Thanks for your answer. However, I noticed that changing \multiocolumn{2}{l} to \multiocolumn{2}{c} seems also to be an improvement. I guess I do not understand the way latex tables place intercolumn space. For instance, I've noticed that you have cleaned up the column specificiation, which I do not understand. Anyhow, the format of tex.SX.com is not suitable to improve understanding of how latex tables work. I am afraid that my question was poorly asked and shouldn't have gotten an answer anyway. – Hotschke Mar 26 '17 at 14:32
  • 1
    @Hotschke It's better if you edit your question and add an image of what you want. Also a photo of an hand drawn picture is OK. – CarLaTeX Mar 26 '17 at 14:38
2

A (very) small variation of CarLaTeX answer with two ways of aligning of the column headers (I was not able to figured out, how you like to have aligned):

\documentclass{article}
\usepackage{array, booktabs}
\newcommand\mcc[1]{\multicolumn{2}{c}{#1}}
 \newcommand\mc[1]{\multicolumn{1}{c}{#1}}
 \newcommand\mr[1]{\multicolumn{2}{r}{#1}}
\newcolumntype{A}{>{$}r<{$} @{} >{$}l<{$}}
\usepackage{lipsum}

\begin{document}
\lipsum[2]
    \begin{center}
    \begin{tabular*}{\textwidth}{%
        @{\hspace{\tabcolsep}\extracolsep{\fill}}
        A  
        cc 
        AA
        l 
        @{\hspace{\tabcolsep}}  }
        \toprule
\mcc{title 1} & title 2 (c) & title 3 (c) & \mcc{title 4} & \mcc{title 5} & \mc{title 6 (l)}    \\
        \midrule
a   & = b    & cell 2      &   cell 3    &  c   & = d   & e     & = f  & cell 66666666        \\
a+2 & = b    & cell 2      &   cell 3    &  c+3 & = d   & e+4   & = f  & cell 6         \\
        \bottomrule
    \end{tabular*}
    \end{center}

    \begin{center}
    \begin{tabular*}{\textwidth}{%
        @{\hspace{\tabcolsep}\extracolsep{\fill}}
        A
        cc
        AA
        l
        @{\hspace{\tabcolsep}}  }
        \toprule
\mr{title 1} & title 2 (c) & title 3 (c) & \mr{title 4} & \mr{title 5} & title 6 (l)    \\
        \midrule
a   & = b    & cell 2      &   cell 3    &  c   & = d   & e     & = f  & cell 6 66666666\\
a+2 & = b    & cell 2      &   cell 3    &  c+3 & = d   & e+4   & = f  & cell 6         \\
        \bottomrule
    \end{tabular*}
    \end{center}

    \lipsum[2]
\end{document}

enter image description here

Zarko
  • 296,517