我正在使用 booktabs 库在 latex 中制作表格。但是,我注意到由于表格的大小,它超出了右边距。我尝试遵循几篇文章中的几条建议,但没有一条对我有用。我的代码如下:
\begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
{\small
\begin{tabular}{*9c}
\toprule
Case & \multicolumn{4}{c}{dv1} & \multicolumn{4}{c}{dv2}\\
& cla& score1& score2& score3& cla& score1& score2& score3\\
\midrule
\textbf{Claas1} & {49.34$\%$} & & & & {44.73$\%$} & & & \\
\textbf{Claas2} & \textbf{52.52$\%$} & \textbf{59.44} & \textbf{2.16} &
\textbf{2.16} & \textbf{47.11$\%$} & \textbf{49.77} & \textbf{2.21}
& \textbf{2.21}\\
\textbf{Claas3} & 49.92$\%$ & 60.13 & 2.01 & 2.01 & 46.23$\%$ & 50.33 &
2.05 & 2.05\\
\textbf{Claas4} & 50.29$\%$ & 60.10 & 2.00 & 2.00 & 46.55$\%$ & 49.95 &
2.01 & 2.01\\
\bottomrule
\end{tabular}
}
\end{table}
如何在模板页面范围内缩小或拉伸表格?
答案1
示例 2 中的代码确保表格在边距内。但是,在示例 1 中,我建议对您的表格进行一些(在我看来)改进。右对齐图表列、标题中的百分号以及增加表格和标题之间的空间:
示例 1
\documentclass{article}
\usepackage{array, booktabs, caption}
\usepackage[skip=1ex]{caption}
\setlength{\tabcolsep}{0pt}
\begin{document}
\begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
{\small
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l*{8}{r}}
\toprule
Case & \multicolumn{4}{c}{dv1} & \multicolumn{4}{c}{dv2}\\
\cmidrule{2-5}\cmidrule{6-9}
& cla (\%)& score1& score2& score3& cla (\%)& score1& score2& score3\\
\midrule
\textbf{Claas1} & {49.34} & & & & {44.73} & & & \\
\textbf{Claas2} & \textbf{52.52} & \textbf{59.44} & \textbf{2.16} &
\textbf{2.16} & \textbf{47.11} & \textbf{49.77} & \textbf{2.21}
& \textbf{2.21}\\
\textbf{Claas3} & 49.92 & 60.13 & 2.01 & 2.01 & 46.23 & 50.33 &
2.05 & 2.05\\
\textbf{Claas4} & 50.29 & 60.10 & 2.00 & 2.00 & 46.55 & 49.95 &
2.01 & 2.01\\
\bottomrule
\end{tabular*}
}
\end{table}
\end{document}
示例 2
\documentclass{article}
\usepackage{array, booktabs, caption}
\setlength{\tabcolsep}{0pt}
\begin{document}
\begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
{\small
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}*{9}{c}}
\toprule
Case & \multicolumn{4}{c}{dv1} & \multicolumn{4}{c}{dv2}\\
& cla& score1& score2& score3& cla& score1& score2& score3\\
\midrule
\textbf{Claas1} & {49.34$\%$} & & & & {44.73$\%$} & & & \\
\textbf{Claas2} & \textbf{52.52$\%$} & \textbf{59.44} & \textbf{2.16} &
\textbf{2.16} & \textbf{47.11$\%$} & \textbf{49.77} & \textbf{2.21}
& \textbf{2.21}\\
\textbf{Claas3} & 49.92$\%$ & 60.13 & 2.01 & 2.01 & 46.23$\%$ & 50.33 &
2.05 & 2.05\\
\textbf{Claas4} & 50.29$\%$ & 60.10 & 2.00 & 2.00 & 46.55$\%$ & 49.95 &
2.01 & 2.01\\
\bottomrule
\end{tabular*}
}
\end{table}
\end{document}
答案2
由于您有许多列,最简单的方法是减少值\tabcolsep
(默认为 6pt)。我借此机会改进了表格的布局,使用三个(修剪过的)\cmidrule
s\multirow
作为第一列的第一个单元格,并为单元格和标题添加了一些垂直填充。还有一个补充空列来分隔两组列:
\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs,array, caption, multirow, makecell}%
\captionsetup{ justification=centering, skip=4pt}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\begin{document}
\begin{table}[!htbp]
\centering
\setlength{\tabcolsep}{4pt}
\setcellgapes{3pt}\makegapedcells
\let\cmidrulewidth\lightrulewidth
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
\begin{tabular}{>{\bfseries}c*9c}
\toprule
\multirowcell{2}[0pt][l]{Case} & \multicolumn{4}{c}{dv1} & & \multicolumn{4}{c}{dv2}\\
& cla& score1& score2& score3& & cla& score1& score2& score3\\
\cmidrule(r){1-1}\cmidrule(lr){2-5}\cmidrule(l){7-10}
Claas1 & 49.34$\%$ & & & & & {44.73$\%$} & & & \\
Claas2 & \textbf{52.52$\%$} & \textbf{59.44} & \textbf{2.16} &
\textbf{2.16} & & \textbf{47.11$\%$} & \textbf{49.77} & \textbf{2.21}
& \textbf{2.21}\\
Claas3 & 49.92$\%$ & 60.13 & 2.01 & 2.01 & & 46.23$\%$ & 50.33 &
2.05 & 2.05\\
Claas4 & 50.29$\%$ & 60.10 & 2.00 & 2.00 & & 46.55$\%$ & 49.95 &
2.01 & 2.01\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案3
为了完整性:使用siunitx
列包S
,etoolbox
并xparse
确定粗体形状中的数字和单元格的缩写\multicolumn
:
编辑:
现在的解决方案是考虑在新的 LaTeX 中处理\bfseries
(参见Ulrike Fischer 的回答)。
\documentclass{article}
\usepackage[skip=1ex, labelfont=bf]{caption}
\usepackage{booktabs, multirow}
\usepackage{siunitx}
\usepackage{etoolbox}
\newrobustcmd\B{\DeclareFontSeriesDefault[rm]{bf}{b}\bfseries}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
{\multicolumn{#1}{c}{#2}}
\begin{document}
\begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
\sisetup{detect-weight,
mode=text,
table-format=2.2}
\setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}
>{\bfseries}l
S[table-space-text-post=\,\%]<{\,\%}
S
*{2}{S[table-format=1.2]}
*{2}{S}
S[table-format=1.2]
S[table-format=1.2,
table-space-text-post=\,\%]<{\,\%}
}
\toprule
\multirow{2.4}{*}{Case}
& \mcc[4]{dv1}
& \mcc[4]{dv2} \cr
\cmidrule(l){2-5}\cmidrule(l){6-9}
& \mcc{cla} & {score 1} & {score 2} & {score 3}
& {cla} & {score 1} & {score 2} & \mcc{score 3} \cr
\midrule
Claas 1 & 49.34 & & &
& 44.73 & & & \mcc{} \cr
Claas 2 &\B 52.52 &\B 59.44 &\B 2.16 &\B 2.16
&\B 47.11 &\B 49.77 &\B 2.21 &\B 2.21 \cr
Claas 3 & 49.92 & 60.13 & 2.01 & 2.01
& 46.23 & 50.33 & 2.05 & 2.05 \cr
Claas 4 & 50.29 & 60.10 & 2.00 & 2.00
& 46.55 & 49.95 & 2.01 & 2.01 \cr
\bottomrule
\end{tabular*}
\end{table}
\end{document}