我有以下 MWE,但是最后一列(在多列下)的对齐存在问题
\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{calc}
\usepackage{array}
\usepackage{color}
%
\begin{document}
%
% table 1
% evenly alined
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component sizes}\\
%\cmidrule(r){3-5}
\cmidrule{3-5}
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Information on component level degrees of freedom}
\label{tab:information-overview-academic-model}
\end{table}
%
% table 2
% not evenly alined
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component matrix sizes \textcolor{red}{test text added}}\\
%\cmidrule(r){3-5}
\cmidrule{3-5}
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Information on component level degrees of freedom}
\label{tab:information-overview-academic-model}
\end{table}
%
\end{document}
我得到的结果是多列下的列不是均匀分布的。当我增加多列命令的文本参数的宽度时,情况变得更糟,第一个例子似乎没问题,但第二个例子肯定有问题。以下是上述代码的输出:
任何能解决此问题的想法都值得赞赏。提前致谢。
答案1
我建议两种解决方案。
第一个方法\makecell
将多列标题放在多行上;第二个方法将tabularx
空间均匀分布在最后 3 列中,而将标题保留在一行。
\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{calc}
\usepackage{array}
\usepackage{color}
\usepackage{makecell}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
%
% table 1
% evenly alined
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component sizes}\\
%\cmidrule(r){3-5}
\cmidrule{3-5}
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Information on component level degrees of freedom}
\label{tab:information-overview-academic-model}
\end{table}
%
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{\makecell{Reduced component\\ matrix sizes\\ \textcolor{red}{test text added}}}\\
%\cmidrule(r){3-5}
\cmidrule{3-5}
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{With \textbackslash\texttt{makecell}}
\label{tab:information-overview-academic-modelbis}
\end{table}
\begin{table}[htbp]
\centering
\begin{tabularx}{\linewidth}{ccCCC}
\toprule
& & \multicolumn{3}{c}{Reduced component matrix sizes \textcolor{red}{test text added}}\\
%\cmidrule(r){3-5}
\cmidrule{3-5}
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabularx}
\caption{With \texttt{tabularx}}
\label{tab:information-overview-academic-modelter}
\end{table}
\end{document}
答案2
只是为了多样性,这里有一个解决方案,(a)计算第 3、4 和 5 列的自然宽度,(b)使用三列的组合宽度(加上列间空白量)以允许自动换行的方式排版长标题字符串。
这种方法可以使表格材料保持美观和紧凑;这通常被认为是一件好事。
请注意,我会将标题“组件”和“全尺寸”放在标题材料的顶部而不是底部。
\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor,booktabs,array,ragged2e}
%% Create a new column type:
\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1}}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component sizes}\\
\cmidrule{3-5}
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{OP's original layout}
\label{tab:a}
\end{table}
%% perform a few length calculations
\newlength\mylen
\newlength\mylena
\newlength\mylenb
\settowidth\mylena{$m=4$}
\settowidth\mylenb{$m=12$}
\setlength{\mylen}{\dimexpr2\mylena+\mylenb+4\tabcolsep\relax}
\begin{table}[htbp]
\centering
\begin{tabular}{@{}ccccc@{}}
\toprule
Component & Full size &
\multicolumn{3}{C{\mylen}@{}}{Reduced component matrix sizes \textcolor{red}{test text added}}\\
\cmidrule(l){3-5}
& & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Automatic line-breaking in cols 3 to 5}
\label{tab:b}
\end{table}
\end{document}