通过文本高度调整表格大小

通过文本高度调整表格大小

假设我有一个表格,它不适合一页。假设我不想使用任何特殊的表格类型,但想通过使用 -command 来解决问题\resizebox并保持我的比例。

如果使用\resizebox{\texwidth}{!}{...}表格合适,那么我的问题就解决了。但如果表格仍然太长,我的直觉是使用\resizebox{!}{\textheight}{...}。为标题留出一些空间,我必须将高度调整到大约,0.9\textheight以确保单行标题。但在我的例子中,我必须将高度调整到大约。0.48\textheight以消除所有警告并使表格适合页面。这对我来说似乎很奇怪。我遗漏了什么?

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\begin{table}[htbp]
\centering
%   \resizebox{\textwidth}{!}{
    \resizebox{!}{0.48\textheight}{
        \begin{tabular}{|lll|}\hline
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\
            TextTextTextTextTextText & TextTextTextTextText & TextTextTextTextText\\ \hline
        \end{tabular}}
    \caption{test}
\end{table}
\end{document}

答案1

你遇到了一个常见问题,混淆了高度总高TeX 中的东西。出于某种原因,这也让我感到惊讶,表格的基线是表格的中间,因此下半部分是深度生成的 TeX 框。\resizebox缩放框高度,而不是总高(高度+深度)为给定值。要缩放总高度,请使用版本*。您还可以使用\dimexpr(另请参阅在 LaTeX 源代码中使用距离值进行数学运算)或calc包从高度中减去例如两条线:

\resizebox*{!}{\dimexpr\textheight-2\baselineskip\relax}{%

还要注意,将大型表格放在宏参数中效率非常低,并且不允许\verb在表格中使用等。您可以使用相对较新的包(由我编写)中的\adjustbox宏或环境来缩放任何内容。两者都将内容读取为框而不是参数,并避免了上述缺点:adjustboxadjustbox

\usepackage{adjustbox}
% [...]

\begin{adjustbox}{totalheight=\textheight-2\baselineskip}
  \begin{tabular}{...}
     ...
  \end{tabular}
\end{adjustbox}

环境adjustbox和宏接受\includegraphics宏的所有选项。这包括heighttotalheight

请注意,adjustbox使用pgfmath来解析给定的尺寸,因此您可以使用此包支持的所有数学运算。但是,不要\dimexpr直接使用,因为它不支持pgfmath

相关内容