4

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

4

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}
2

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

1
\documentclass{scrbook}
  \usepackage{array}
  \usepackage{booktabs}
  \newlength\ColSpacing
% \setlength{\ColSpacing}{7em}

\begin{document}
  \begin{table}[!h]
  \caption{bla.}
  \centering
  \setlength{\ColSpacing}{7em}
  \begin{tabular}{cccc}
    & \hspace{\ColSpacing} & \hspace{\ColSpacing} & \hspace{\ColSpacing} \\
    \toprule
    & \multicolumn{3}{c}{sample number}\\
    \cmidrule{2-4}
    & 1 & 2 & 3\\ \midrule 
    some longer text here & A & B & C\\
    \bottomrule
  \end{tabular}
  \flushleft
  \end{table}

  \vspace{10ex}
  \begin{tabular}{c*{3}{wc{7em}}}
    \toprule
    & \multicolumn{3}{c}{sample number}\\
    \cmidrule{2-4}
    & 1 & 2 & 3\\ \midrule 
    some longer text here & A & B & C\\
    \bottomrule
  \end{tabular}
  \flushleft

\end{document}
2
  • Your solution adds an empty line above the \toprule of the table, thus increasing the vertical space between caption and \toprule. In order to get fixed width centered columns, you could use \begin{tabular}{c*{3}{wc{7em}}} instead. (Needs the array package.)
    – leandriis
    Feb 22 at 18:37
  • Good suggestion Leandris. Looks like the more correct way of doing it. Mar 21 at 13:37

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.