我有这个输出:
问题
我不想对调整大小参数‘0.8’进行硬编码\resizebox{0.8\columnwidth}{!}{[...]}
,而是想制作某种自动调整大小功能。
我有以下两个要求:
- 我希望表格尽可能填满页面的其余部分(纸张的高度/长度)
- 但仅限于表格的列宽不超过文本宽度
知道如何在 Latex 中编写此功能吗?或者提供可能对我进一步完成此任务有帮助的资源链接。
特克斯代码
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{float}
\begin{document}
\section{Text}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\section{Table}
\begin{table}[H]
\resizebox{0.8\columnwidth}{!}{
\begin{tabular}{lllll}
\hline
\multicolumn{1}{|l|}{1} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{2} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{3} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{4} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{5} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{6} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
& & & & \\ \hline
\multicolumn{1}{|l|}{7} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{8} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{9} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{10} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{11} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{12} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\end{tabular}
}
\end{table}
\end{document}
答案1
如果您希望表格适合给定的宽度,tabularx
(感谢@David Carlisle!)是可行的方法。
为了简化,我设置了一种新的列类型。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{float}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X} % from array package
\usepackage{lipsum}
\begin{document}
\section{Text}
\lipsum[2]
\section{Table}
\begin{table}[H]
\resizebox{0.8\columnwidth}{!}{
\begin{tabular}{lllll}
\hline
\multicolumn{1}{|l|}{1} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{2} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{3} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{4} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{5} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{6} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
& & & & \\ \hline
\multicolumn{1}{|l|}{7} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{8} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{9} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{10} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{11} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{12} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\end{tabular}
}
\end{table}
\begin{table}[H]
\begin{tabularx}{\columnwidth}{|l*{4}{|C}|}
\hline
1 & Something & Something & Something & Something \\ \hline
2 & Something & Something & Something & Something \\ \hline
3 & Something & Something & Something & Something \\ \hline
4 & Something & Something & Something & Something \\ \hline
5 & Something & Something & Something & Something \\ \hline
6 & Something & Something & Something & Something \\ \hline
& & & & \\ \hline
7 & Something & Something & Something & Something \\ \hline
8 & Something & Something & Something & Something \\ \hline
9 & Something & Something & Something & Something \\ \hline
10 & Something & Something & Something & Something \\ \hline
11 & Something & Something & Something & Something \\ \hline
12 & Something & Something & Something & Something \\ \hline
\end{tabularx}
\end{table}
\end{document}
答案2
作为@Nbur 答案的补充......
- 正如前面提到的,使用会
\resizebox
导致表格中的字体大小与其他文本中的字体大小不一致。 - 使用
multicolumn{1}{...}{...}
只会使代码变得混乱 - 使用
[H]
作为放置规范是危险的。它可能会导致意想不到的结果,如不想要的空白处。 - 不清楚你想象的是什么桌子垂直拉伸...这也会导致字体大小改变
- 你真的想要一张设计非常丑陋的桌子吗?
@Nbur 的回答展示了如何编写表格。这里只是对使用过的代码进行了清理,并对表格的高度进行了一些改进:
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\section{Text}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\section{Table}
\begin{table}[ht]
\renewcommand\arraystretch{1.3} % <---
\begin{tabularx}{\linewidth}{|l|C|C|X|X|}
\hline
1 & Something & Something & Something & Something \\ \hline
2 & Something & Something & Something & Something \\ \hline
3 & Something & Something & Something & Something \\ \hline
4 & Something & Something & Something & Something \\ \hline
5 & Something & Something & Something & Something \\ \hline
6 & Something & Something & Something & Something \\ \hline
& & & & \\ \hline
7 & Something & Something & Something & Something \\ \hline
8 & Something & Something & Something & Something \\ \hline
9 & Something & Something & Something & Something \\ \hline
10 & Something & Something & Something & Something \\ \hline
11 & Something & Something & Something & Something \\ \hline
12 & Something & Something & Something & Something \\ \hline
\end{tabularx}
\end{table}
\end{document}
答案3
如果您确实想要调整整个表格的大小,但又希望实现一些自动化,请查看包fitbox
:您可以指定最小和最大长度来调整内容。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{float}
\usepackage{lipsum}
\usepackage{fitbox}
\begin{document}
\section{Text}
\lipsum[2]
\section{Table}
\begin{table}[H]
% \resizebox{0.8\columnwidth}{!}{
\fitbox[maxwidth=\columnwidth]{
\begin{tabular}{lllll}
\hline
\multicolumn{1}{|l|}{1} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{2} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{3} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{4} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{5} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{6} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\end{tabular}
}
\end{table}
\begin{table}[H]
\fitbox[maxwidth=\textwidth]{
\begin{tabular}{|l|c|}
\hline
1 & Something\\% & Something & Something & Something \\ \hline
2 & Something\\% & Something & Something & Something \\ \hline
3 & Something\\% & Something & Something & Something \\ \hline
4 & Something\\ \hline
\end{tabular}}
\end{table}
\end{document}