0

It appears that if the total widths of the columns is insufficient, tabular stretches the last column. In the following image note that the last column is stretched due to the heading being wide.

enter image description here

Question:

Is there a simple way to change this default to stretch the first column instead?

Notes:

  • These tables are automatically generated and can have a maximum of 18 columns. Thus, the l*{17}{c} tabular specification.
  • One way for me to resolve this problem would be to specify the width of the first column, but seems to me that there must be a better way. I would prefer not to have to make the first column wider than it needs to be.

Code:

\documentclass{article}
\usepackage{booktabs}

\def\NumberOfColumns{4}%

\newcommand*{\BuildTable}[1]{%
    %% #1 = Title
    \begin{tabular}{l*{17}{c}}
        \toprule
        \multicolumn{\NumberOfColumns}{c}{{#1}} \\
        \cmidrule(lr){1-4}
        Heading & 0 & 1 & 2 \\
        \cmidrule(lr){1-1}
        \cmidrule(lr){2-2}
        \cmidrule(lr){3-3}
        \cmidrule(lr){4-4}
        Label & a & b & c \\
        Label & a & b & c \\
        Label & a & b & c \\
        \bottomrule
    \end{tabular}%
}%

\begin{document}
\BuildTable{Narrow Title}
\quad
\BuildTable{Some Title That is Wide}
\end{document}

1 Answer 1

1

If you are willing to use the tblr environment from tabularray package, you can specify the option hspan=even to evenly stretch all columns.

output

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\def\NumberOfColumns{4}%

\begin{document}

    normal \verb|tblr| \smallskip
    
    \begin{tblr}{colspec={l*{3}{c}}}
        \toprule
        \SetCell[c=\NumberOfColumns]{c} Some Title That is Wide \\
        \cmidrule[lr]{1-4}
        Heading & 0 & 1 & 2 \\
        \cmidrule[lr]{1-1}
        \cmidrule[lr]{2-2}
        \cmidrule[lr]{3-3}
        \cmidrule[lr]{4-4}
        Label & a & b & c \\
        Label & a & b & c \\
        Label & a & b & c \\
        \bottomrule
    \end{tblr}
    \bigskip
    
    with \verb|hspan=even| \smallskip
    
    \begin{tblr}{colspec={l*{3}{c}},hspan=even}
        \toprule
        \SetCell[c=\NumberOfColumns]{c} Some Title That is Wide \\
        \cmidrule[lr]{1-4}
        Heading & 0 & 1 & 2 \\
        \cmidrule[lr]{1-1}
        \cmidrule[lr]{2-2}
        \cmidrule[lr]{3-3}
        \cmidrule[lr]{4-4}
        Label & a & b & c \\
        Label & a & b & c \\
        Label & a & b & c \\
        \bottomrule
    \end{tblr}

    \bigskip
    
    with \verb|booktabs| spacing \smallskip
    
    \begin{booktabs}{colspec={l*{3}{c}},hspan=even}
        \toprule
        \SetCell[c=\NumberOfColumns]{c} Some Title That is Wide \\
        \cmidrule[lr]{1-4}
        Heading & 0 & 1 & 2 \\
        \cmidrule[lr]{1-1}
        \cmidrule[lr]{2-2}
        \cmidrule[lr]{3-3}
        \cmidrule[lr]{4-4}
        Label & a & b & c \\
        Label & a & b & c \\
        Label & a & b & c \\
        \bottomrule
    \end{booktabs}
\end{document}

The tblr environment has more vertical spacing by default. If you would like to keep the booktabs spacing, use the booktabs environment provided by tabularray package, which behaves the same as a regular tabular regarding the spacing.

2
  • Looks like this requires an update to the TeXLive 2021 packages so can't test it right now, but does look promising. Feb 3 at 19:56
  • 1
    @PeterGrill with tabularray package, you also don't need to worry about column numbers, since you can write columns={c}, column{1}={l}.
    – L.J.R.
    Feb 4 at 2:33

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.