希望这是一个非常简单的问题。为什么下面示例中的多列会搞乱下面各列之间的间距?显然,我希望 A、B 和 C 列间距均匀(如果我注释多列行,它们就会是这样的)。如何实现这一点?
\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}
结果是
答案1
指定列宽前“额外”的跳过会被拉伸,并且如果跨越单元格比它跨越的列宽,则所有空间都会进入最后一个跨越的列。
因此,您要么需要隐藏跨度的宽度(如下所示),要么增加自然宽度(例如更改为),\fill
以便1cm plus 1fill
跨越单元格不会强制列变宽。
您不需要重复,\extracolsep
它适用于所有后续列,直到重置为止。
\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
如果tabular*
不是必须的话,使用tabularx
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}
答案3
\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}