2

A hopefully very simple question. Why does the multicolumn in my example below screw up the spacing between the columns below? Obviously I would want columns A, B, and C to be spaced evenly (as they are if I comment the multicolumn-row). How to achieve this?

\documentclass{scrbook}
\usepackage{booktabs}
\usepackage{multirow}

\newcommand{\crule}[1]{\multispan{#1}{\hspace*{\tabcolsep}\hrulefill\hspace*{\tabcolsep}}}

\begin{document}

\begin{table}[!t]
    \caption{bla.} 
    \begin{tabular*}{\textwidth}{@{}l@{\extracolsep\fill}c@{\extracolsep\fill}c@{\extracolsep\fill}c@{}}
    \toprule
        & \multicolumn{3}{c}{sample number}\\
        & \crule{3}\\ % definition should still include some neg. vspace after that
        & 1 & 2 & 3\\
    \midrule
    some longer text here  & A    & B   & C\\
    \bottomrule
    \end{tabular*}
    \end{table}
\end{document}

results in

the resulting table

3

Column widths are assigned before the "extra" skips are stretched, and if a spanning cell is wider than the columns it spans, all space goes into the last spanned column.

So you either need to hide the width of the span as below, or increase the natural width eg change \fill to 1cm plus 1fill so that the spanning cell doesn't force the columns wider.

Not you don't need to repeat \extracolsep it applies to all further columns until reset.

\documentclass{scrbook}
\usepackage{booktabs}
\usepackage{multirow}

\newcommand{\crule}[1]{\multispan{#1}{\hspace*{\tabcolsep}\hrulefill\hspace*{\tabcolsep}}}

\begin{document}

\begin{table}[tp]%[!t]
    \caption{bla.} 
    \begin{tabular*}{\textwidth}{@{}l@{\extracolsep\fill}c@{\extracolsep\fill}c@{\extracolsep\fill}c@{}}
    \toprule
        & \multicolumn{3}{c}{\makebox[0pt]{sample number}}\\
        & \crule{3}\\ % definition should still include some neg. vspace after that
        & 1 & 2 & 3\\
    \midrule
    some longer text here  & A    & B   & C\\
    \bottomrule
    \end{tabular*}
    \label{tab:Mg2Si_Variation_SD}
    \end{table}
\end{document}
1

If tabular* is not a must, may be is simpler use tabularx of the same package of David Carlisle.

\documentclass{scrbook}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{multirow}
\begin{document}

\begin{tabularx}{\textwidth}{
l
>{\centering}X
>{\centering}X
>{\centering\arraybackslash}X
}
\toprule
 & \multicolumn{3}{c}{sample number}\\
\cmidrule{2-4}
 & 1 & 2 & 3\\
\midrule
some longer text here  & A    & B   & C   \\
\bottomrule
\end{tabularx}

\end{document}

MWE

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.