[Tex/LaTex] Shrink table to fit on a page, or keep it as it is

scalingtables

I am using

\resizebox{\columnwidth}{!}{% ... %}

to fit a table which might be too wide onto a page by scaling it down.

However, my problem is that if the table is smaller than the column width, it gets scaled up, which looks really ugly, and, more importantly, it can then become too long to fit on the page.

So how do I change above to either shrink it or keep it the same, but not scale it up?

I need something along the lines of

\resizebox{min(\columnwidth,\originalwidth)}{!}{% ... %}

Best Answer

With the adjustbox package you can say

\adjustbox{max width=\columnwidth}{...}

that will scale the contents only if it exceeds the \columnwidth, according to the documentation:

A good example is max width=\textwidth which will limit large content to the text width but will not affect smaller content.

Of course \textwidth is just by way of example and any dimension can be used.

Here's an example:

\documentclass{article}
\usepackage{adjustbox,lipsum}

\begin{document}

\noindent\adjustbox{max width=\textwidth}{%
 \begin{tabular}{p{.7\textwidth}}\lipsum[2]\end{tabular}}

\noindent\adjustbox{max width=\textwidth}{%
 \begin{tabular}{p{1.5\textwidth}}\lipsum[2]\end{tabular}}

\end{document}

enter image description here

As suggested by Martin Scharrer in a comment, the environment form can be even handier in this case:

\documentclass{article}
\usepackage{adjustbox,lipsum}

\begin{document}

\begin{adjustbox}{max width=\textwidth}
  \begin{tabular}{p{.7\textwidth}}\lipsum[2]\end{tabular}
\end{adjustbox}

\begin{adjustbox}{max width=\textwidth}
  \begin{tabular}{p{1.5\textwidth}}\lipsum[2]\end{tabular}}
\end{adjustbox}

\end{document}

The environment form automatically adds \noindent.