我有一个调整大小表格,并希望最后一行的文本环绕表格的整个宽度。我该如何设置宽度才能实现这一点p{?}
?multicolumn
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{table}
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{llllllllll}
Column A & Column B & Column C & Column D & Column E & Column F & Column G & Column H & Column I & Column J \\
\multicolumn{10}{p{\dimexpr\textwidth-2\tabcolsep\relax}}{
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu luctus tellus, a faucibus est. Donec id facilisis ipsum. Morbi ac velit magna. Praesent tincidunt egestas ex, ac cursus neque convallis sed. Aenean enim est, feugiat eu velit quis, sodales.
}
\end{tabular}%
}
\end{table}
\end{document}
答案1
您不应调整表格大小,因为这会导致字体大小不一致。请改用tabularx
。
由于您的表格有很多列,您可能需要考虑加载几何图形:除非您使用边距注释,否则您将拥有更合适的水平边距,这使得此类表格更容易在边距之间适合。这是一个简单的代码:
\documentclass[latin]{article}
\usepackage[showframe]{geometry}
\usepackage{array, makecell, tabularx}
\usepackage{graphicx}
\usepackage{babel}
\begin{document}
\begin{table}[!htb]
\centering
\renewcommand{\theadfont}{\normalsize}
\setlength{\tabcolsep}{4pt}
\begin{tabularx}{\linewidth}{@{}*{10}{>{\arraybackslash}X}@{}}
\thead{Column\\ A} & \thead{Column\\ B} & \thead{Column\\ C} & \thead{Column\\ D} & \thead{Column\\ E} & \thead{Column\\ F} & \thead{Column\\ G} & \thead{Column\\ H} & \thead{Column\\ I} & \thead{Column\\ J}\\
\multicolumn{10}{@{}>{\arraybackslash}p{\textwidth}@{}}{
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu luctus tellus, a faucibus est. Donec id facilisis ipsum. Morbi ac velit magna. Praesent tincidunt egestas ex, ac cursus neque convallis sed. Aenean enim est, feugiat eu velit quis, sodales.
}
\end{tabularx}%
\end{table}
\end{document}
如果您确实需要更大的边距,您也可以调整表格中的字体大小和值\tabcolsep
(默认为 6pt)
答案2
除了调整表格大小被认为是不好的做法之外,我还找到了解决这个问题的方法先前的答案。跨越所有列的宽度multicolumn
将自动适应表格的宽度(也适用于未调整大小的表格)。代码:
\documentclass{article}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{environ}
\newsavebox{\funnytabularbox}
\newlength{\funnytabularwd}
\makeatletter
\NewEnviron{funnytabular}[1]
{\begin{lrbox}{\funnytabularbox}
\let\multipar\@gobbletwo
\begin{tabular}{#1}\BODY\end{tabular}
\end{lrbox}%
\setlength{\funnytabularwd}{\wd\funnytabularbox}%
\addtolength{\funnytabularwd}{-2\tabcolsep}%
\let\multipar\@multipar
\begin{tabular}{#1}\BODY\end{tabular}}
\newcommand\@multipar[2]{\multicolumn{#1}{p{\funnytabularwd}}{#2}}
\makeatother
\begin{document}
\begin{table}
\centering
\resizebox{\textwidth}{!}{%
\begin{funnytabular}{llllllllll}
Column A & Column B & Column C & Column D & Column E & Column F & Column G & Column H & Column I & Column J \\
\multipar{10}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu luctus tellus, a faucibus est. Donec id facilisis ipsum. Morbi ac velit magna. Praesent tincidunt egestas ex, ac cursus neque convallis sed. Aenean enim est, feugiat eu velit quis, sodales.
}
\end{funnytabular}%
}
\end{table}
\end{document}