21

Say, I have a table which doesn't fit on one page. Say, I don't want to use any special table types or so but want to solve the problem by using the \resizebox-command and keep my ratio.

If by using \resizebox{\texwidth}{!}{...} the table fits, then my problem is solved. But if the table is still too long, my instinct would be to use \resizebox{!}{\textheight}{...} instead. Allowing some space for my caption I would have to adjust the height to about 0.9\textheight to be sure for a single line caption. But in my example I have ta adjust to approx. 0.48\textheight to get rid of all warnings and fit the table on the page. This seems very odd to me. What am I missing?

\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 Answer 1

33

You are running into the common problem of confusing the height and the totalheight of things in TeX. For some reason which also surprised me the baseline of the table is the the middle of the table, so that the lower half is part of the depth of the resulting TeX box. The \resizebox scales the box height, not the totalheight (height+depth) to the given value. To scale the totalheight use the * version. You can also use \dimexpr (see also Doing maths with distance values in LaTeX source code) or the calc package to subtract e.g. two lines from the height:

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

Also note that placing a large table in a macro argument is very inefficient and doesn't allow the usage of \verb etc. in the table. You can use the \adjustbox macro or adjustbox environment from the relative new adjustbox package (written by me) to scale any content instead. Both read the content as box not as argument and avoid the mentioned drawbacks:

\usepackage{adjustbox}
% [...]

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

The adjustbox environment and macro accepts all options of the \includegraphics macro. This includes height and totalheight.

Note that adjustbox uses pgfmath to parse the given dimensions, so you can use all math supported by this package. However do not use \dimexpr directly because it isn't supported by pgfmath.

4
  • I fixed the previously mentioned issue with identical output for height and totalheight now and uploaded the correct version as 2011/03/20 v0.3 to CTAN. However, simply using totalheight as shown now works also with the previous version. Mar 20, 2011 at 20:55
  • The baseline of the tabular is in the middle as the default position argument is [c], if you use t or b then the baseline is the baseline of the top or bottom row (or rule). Oct 24, 2013 at 14:50
  • using \resizebox*{!}{\dimexpr\textheight-2\baselineskip\relax}{% didn't work in my case in beamer, the table was vertically still too large. Am I missing something?
    – ghx
    Jul 15, 2019 at 22:01
  • 2
    @ghx: the body area of beamer is smaller than \textheight so far I remember as some space is used for the headline. You should search for a better dimension provided by beamer. Jul 16, 2019 at 6:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .