扩展表格以覆盖文本宽度

扩展表格以覆盖文本宽度

下面包含代码的表格适合页边距,这使得文本“ABCXYZabc”和“PQR-ABCXYZabc”溢出其指定列。

在此处输入图片描述

\documentclass[oneside,a4paper]{article}

\usepackage[left=6cm,right=0cm,top=5cm,bottom=2cm,headsep=0.1cm]{geometry}

\usepackage{array}
\usepackage{graphicx}
\usepackage{hhline}

\begin{document}
\begin{table}
\centering
\caption{Average Accuracy values (in \%) of baseline methods on different datasets.}
\label{table:accuracy}
\resizebox{\linewidth}{!}{%
\begin{tabular}{|>{\centering\hspace{0pt}}m{0.092\linewidth}|>{\centering\hspace{0pt}}m{0.065\linewidth}>{\centering\hspace{0pt}}m{0.065\linewidth}>{\centering\hspace{0pt}}m{0.065\linewidth}>{\centering\hspace{0pt}}m{0.069\linewidth}|>{\centering\hspace{0pt}}m{0.065\linewidth}>{\centering\hspace{0pt}}m{0.071\linewidth}>{\centering\hspace{0pt}}m{0.071\linewidth}>{\centering\hspace{0pt}}m{0.075\linewidth}|>{\centering\hspace{0pt}}m{0.065\linewidth}>{\centering\hspace{0pt}}m{0.071\linewidth}>{\centering\hspace{0pt}}m{0.071\linewidth}>{\centering\arraybackslash\hspace{0pt}}m{0.075\linewidth}|} 
\hline
~ & \multicolumn{4}{>{\centering\hspace{0pt}}m{0.264\linewidth}|}{Amazon electronics ~ ~ ~} & \multicolumn{4}{>{\centering\hspace{0pt}}m{0.282\linewidth}|}{DBLP ~ ~ ~} & \multicolumn{4}{>{\centering\arraybackslash\hspace{0pt}}m{0.282\linewidth}|}{Amazon clothing ~ ~} \\ 
\cline{2-13}
~ & 1-ret 1-ere & 3-ret 5-ere & 7-ret 1-ere & 11-ret 2-ere & 1-ret 1-ere & 3-ret~5-ere & 7-ret~1-ere & 11-ret~2-ere & 1-ret 1-ere & 3-ret~5-ere & 7-ret~1-ere & 11-ret~2-ere \\ 
\hhline{|=============|}
ABC & 35.2 & 314 & 236 & 224 & 343 & 334.8 & 434.3 & 1349 & 315.8 & 334.6 & 431 & 241 \\
ABCXYZabc & 3429 & 342 & 242 & 423 & ~24.2 & 344 & ~33.2 & ~2929 & ~3421 & ~4415 & ~2412 & 419 \\
XYZ & 3428 & 342.5 & 21.42 & 2444 & 442.8 & 442 & 342 & 3242~ & 442 & 442.3 & 3248 & 2442 \\ 
\hhline{|=============|}
PQR-ABC & 67.8 & 642.1 & 61.2 & 62.9 & 75.2 & 76.4 & 68.5 & 73.4 & 71.5 & 73.4 & 67.5 & 742 \\
PQR-ABCXYZabc & 61.6 & 63.2 & 421.4 & 55.4 & 7423~ & 77.8~ & 724.1~ & 422.9~ & 73.1~ & 78.1 & 642.1~ & 7421 \\
PQR-XYZ & 42.9 & 42.8 & 542 & 59.9 & 75.42 & 742.6 & 42.8 & 7346 & 72 & 832.6 & 324 & 421 \\
\hline
\end{tabular}
}
\end{table}



\end{document}

我尝试过\begin{adjustwidth}{-1in}{-1in}将表格拉伸到边距,但表格仍然太宽,无法容纳整个页面宽度。因此,我也需要缩小字体。

那么,我怎样才能将表格拉伸到超过\textwidth页面的大小,均匀地分布在页面的左右边距(即,保持表格位于中心),同时缩小/缩放字体大小以使其适合页面?

答案1

溢出的第一列与表格宽度或缩放无关,您使用了m比文本窄的固定宽度列。

在此处输入图片描述

避免缩放表格,它会产生不一致的字体大小和规则宽度。

\documentclass[oneside,a4paper]{article}

% right=0 , really??
\usepackage[left=6cm,right=0cm,top=5cm,bottom=2cm,headsep=0.1cm]{geometry}

\usepackage{array}
\usepackage{graphicx}
\usepackage{hhline}

\begin{document}
\begin{table}

\caption{Average Accuracy values (in \%) of baseline methods on different datasets.}
\label{table:accuracy}
\small
\setlength\tabcolsep{3pt}
\setlength\extrarowheight{5pt}

\hspace*{-10pt}% bleed into left margin
\begin{tabular}{@{}|c|cccc|cccc|cccc|@{}}
\hline
 & \multicolumn{4}{c|}{Amazon electronics} & \multicolumn{4}{c|}{DBLP} & \multicolumn{4}{c|}{Amazon clothing} \\ 
\cline{2-13}
 & 1-ret & 3-ret & 7-ret & 11-ret & 1-ret  & 3-ret & 7-ret & 11-ret & 1-ret & 3-ret & 7-ret & 11-ret \\ 
 & 1-ere & 5-ere & 1-ere & 2-ere & 1-ere & 5-ere & 1-ere & 2-ere & 1-ere & 5-ere & 1-ere & 2-ere \\ 
\hhline{|=============|}
ABC & 35.2 & 314 & 236 & 224 & 343 & 334.8 & 434.3 & 1349 & 315.8 & 334.6 & 431 & 241 \\
ABCXYZabc & 3429 & 342 & 242 & 423 & ~24.2 & 344 & ~33.2 & ~2929 & ~3421 & ~4415 & ~2412 & 419 \\
XYZ & 3428 & 342.5 & 21.42 & 2444 & 442.8 & 442 & 342 & 3242~ & 442 & 442.3 & 3248 & 2442 \\ 
\hhline{|=============|}
PQR-ABC & 67.8 & 642.1 & 61.2 & 62.9 & 75.2 & 76.4 & 68.5 & 73.4 & 71.5 & 73.4 & 67.5 & 742 \\
PQR-ABCXYZabc & 61.6 & 63.2 & 421.4 & 55.4 & 7423~ & 77.8~ & 724.1~ & 422.9~ & 73.1~ & 78.1 & 642.1~ & 7421 \\
PQR-XYZ & 42.9 & 42.8 & 542 & 59.9 & 75.42 & 742.6 & 42.8 & 7346 & 72 & 832.6 & 324 & 421 \\
\hline
\end{tabular}%


\end{table}



\end{document}

答案2

这是一个解决方案,将 12 个数据列中的数字与其(显式或隐式)小数标记对齐。

在此处输入图片描述

\documentclass[oneside, % that's the default for the 'article' class
               a4paper]{article}

\usepackage[left=3cm,right=3cm,top=5cm,bottom=2cm,
            headsep=0.1cm,showframe]{geometry}

\usepackage{tabularx,ragged2e,booktabs}
\newcolumntype{C}{>{\Centering\hspace{0pt}}X}
\newcommand\mC[1]{\multicolumn{1}{C}{#1}}
\newlength\mylen
\newcolumntype{M}{>{\hspace{0pt}}m{\mylen}}
% defer setting value of \mylen until we're in the table

\usepackage{siunitx}
\newcolumntype{T}[1]{S[table-format=#1]} % col. type for aliging numbers on decimal markers

\begin{document}

\begin{table}
\small % 10% linear reduction in font size suffices
\settowidth\mylen{ABCXYZabc} % target width of first column
\setlength\tabcolsep{2.5pt} % default: 6pt

\caption{Average Accuracy values (in \%) of baseline 
         methods on different datasets.\strut}
\label{table:accuracy}

\begin{tabularx}{\textwidth}{@{} M 
      T{4.1} T{3.1} T{3.2} T{4.1} 
      *{3}{T{3.1}} T{4.1}
      *{3}{T{4.1}} T{4.0}
      @{}} 
\toprule
 & \multicolumn{4}{c}{Amazon electronics} 
 & \multicolumn{4}{c}{DBLP} 
 & \multicolumn{4}{c@{}}{Amazon clothing} \\ 
\cmidrule(lr){2-5} \cmidrule(lr){6-9} \cmidrule(l){10-13}
 & \mC{1-ret 1-ere} & \mC{3-ret 5-ere} & \mC{7-ret 1-ere} & \mC{11-ret 2-ere} 
 & \mC{1-ret 1-ere} & \mC{3-ret 5-ere} & \mC{7-ret 1-ere} & \mC{11-ret 2-ere} 
 & \mC{1-ret 1-ere} & \mC{3-ret 5-ere} & \mC{7-ret 1-ere} & \mC{11-ret 2-ere} \\ 
\midrule
ABC       &   35.2 & 314   & 236    &  224   &  343    & 334.8 & 434.3 & 1349   &  315.8 &  334.6 &  431   &  241 \\
ABCXYZabc & 3429   & 342   & 242    &  423   &   24.2  & 344   &  33.2 & 2929   & 3421   & 4415   & 2412   &  419 \\
XYZ       & 3428   & 342.5 &  21.42 & 2444   &  442.8  & 442   & 342   & 3242   &  442   &  442.3 & 3248   & 2442 \\ 
\addlinespace
PQR-ABC   &   67.8 & 642.1 &  61.2  &   62.9 &   75.2  &  76.4 &  68.5 &   73.4 &   71.5 &   73.4 &   67.5 &  742 \\
PQR-ABCXYZabc&61.6 &  63.2 & 421.4  &   55.4 & 7423    &  77.8 & 724.1 &  422.9 &   73.1 &   78.1 &  642.1 & 7421 \\
PQR-XYZ   &   42.9 &  42.8 & 542    &   59.9 &   75.42 & 742.6 &  42.8 &  7346  &   72   &  832.6 &  324   &  421 \\
\bottomrule
\end{tabularx}

\end{table}

\end{document}

答案3

不要重新缩放。改用tabular*。通过设置\tabcolsep0pt,我们将计算留给 TeX。您可能需要\footnotesize而不是\small,具体取决于第一列中的实际数据(应左对齐)。

由于表格中的数字并不均匀,我认为没有必要将它们在小数点处对齐。

列标题设置为两行,因此不需要猜测宽度。

我不确定right=0cm,所以我设置了相等的左边距和右边距。

还要注意的是,标准类是为底部标题设置的。通过加载包,我们可以在顶部标题和内部caption之间获得合适的空间。tabulartable

\documentclass[oneside,a4paper]{article}
\usepackage[left=3cm,right=3cm,top=5cm,bottom=2cm,headsep=0.1cm]{geometry}

\usepackage{array}
\usepackage{booktabs}
\usepackage{caption}

\begin{document}

\begin{table}[htp]
\small

\caption{Average Accuracy values (in \%) of baseline methods on different datasets.}
\label{table:accuracy}

\setlength{\tabcolsep}{0pt} % local setting

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{12}{c}}
\toprule
& \multicolumn{4}{c}{Amazon electronics}
& \multicolumn{4}{c}{DBLP}
& \multicolumn{4}{c}{Amazon clothing} \\ 
\cmidrule{2-5} \cmidrule{6-9} \cmidrule{10-13}
& 1-ret & 3-ret & 7-ret & 11-ret & 1-ret & 3-ret & 7-ret & 11-ret & 1-ret & 3-ret & 7-ret & 11-ret \\ 
& 1-ere & 5-ere & 1-ere & 2-ere & 1-ere & 5-ere & 1-ere & 2-ere & 1-ere & 5-ere & 1-ere & 2-ere \\ 
\midrule
ABC & 35.2 & 314 & 236 & 224 & 343 & 334.8 & 434.3 & 1349 & 315.8 & 334.6 & 431 & 241 \\
ABCXYZabc & 3429 & 342 & 242 & 423 & 24.2 & 344 & 33.2 & 2929 & 3421 & 4415 & 2412 & 419 \\
XYZ & 3428 & 342.5 & 21.42 & 2444 & 442.8 & 442 & 342 & 3242 & 442 & 442.3 & 3248 & 2442 \\ 
\midrule
PQR-ABC & 67.8 & 642.1 & 61.2 & 62.9 & 75.2 & 76.4 & 68.5 & 73.4 & 71.5 & 73.4 & 67.5 & 742 \\
PQR-ABCXYZabc & 61.6 & 63.2 & 421.4 & 55.4 & 7423 & 77.8 & 724.1 & 422.9 & 73.1 & 78.1 & 642.1 & 7421 \\
PQR-XYZ & 42.9 & 42.8 & 542 & 59.9 & 75.42 & 742.6 & 42.8 & 7346 & 72 & 832.6 & 324 & 421 \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}

避免双线。

在此处输入图片描述

相关内容