# Shrink table to fit on a page, or keep it as it is

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)}{!}{% ... %}

• What do the double backslashes mean? Are you using some other software for producing the tables? In any case, the adjustbox package might help. Feb 9, 2013 at 22:42
• scaling tables (in either direction) should really be the very last resort. It results in non standard distracting font sizes that are not consistent with the rest of the document. It's almost always better to choose a smaller document declared size such as \small and/or fiddle with column widths rather than scale the table. Feb 9, 2013 at 22:55
• @DavidCarlisle: "fiddling" presumes manual intervention, which isn't possible in the automatic creation of reports. But if you can give me an automated way of finding out what standard font size will make the table fit I'd be more than happy to use that... In the meantime I'll go with scaling... Feb 10, 2013 at 0:25
• @egreg: Indeed, they were there to escape cat-ing from R, forgot to remove those Feb 10, 2013 at 0:37

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}

\begin{document}

\begin{tabular}{p{.7\textwidth}}\lipsum[2]\end{tabular}}

\begin{tabular}{p{1.5\textwidth}}\lipsum[2]\end{tabular}}

\end{document}


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

\documentclass{article}

\begin{document}

\begin{tabular}{p{.7\textwidth}}\lipsum[2]\end{tabular}

\begin{tabular}{p{1.5\textwidth}}\lipsum[2]\end{tabular}}

\end{document}


The environment form automatically adds \noindent.

• Perfect, thank you, works like a charm. Had to upgrade my tex distro to get the package first, and then install some more ubuntu packages. apt-cache search adjustbox - and then I needed as well apt-cache search collectbox - are your friend. Feb 10, 2013 at 5:06
• Thanks egreg! This has a significant advantage over resizebox{width}{height}{object} in that it adjusts the objectonly if it needs to do so in order to fit the object on the page, whereas resizebox will adjust the object no matter what, resulting in small tables becoming awkwardly huge (unless there is a max width function that I am unaware of). May 31, 2013 at 7:34
• For tables I would suggest the adjustbox environment which also includes a \noindent. The \adjustbox macro actually also reads the content as a box and therefore works here just as fine, but the environment syntax is better suited IMHO. Jun 26, 2013 at 11:48
• you can also do: \begin{adjustbox}{max height=8cm,max width=\linewidth,keepaspectratio} which will scale the content inside those boundaries.
– dani
Mar 26, 2016 at 13:08
• This scales the text = absolute crap. Sep 7, 2016 at 8:38

Here's a slightly different approach. The idea is to

1. put the tabular environment in a box
2. measure the box
3. if it's too wide then use resizebox
4. if it's not, then just display it

example 1

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{graphicx}

\newsavebox{\mybox}
\newenvironment{mytabularwrap}{\begin{lrbox}{\mybox}}
{\end{lrbox}%
\setbox0\hbox{\usebox\mybox}%
\ifdim\wd0<\textwidth
\usebox\mybox%
\else
\resizebox{\textwidth}{!}{\usebox\mybox}%
\fi
}
\begin{document}

hello world

\begin{mytabularwrap}
\begin{tabular}{cc}
1 & 2 \\
3 & 4
\end{tabular}
\end{mytabularwrap}

\noindent\begin{mytabularwrap}
\begin{tabular}{*{30}c}
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10
\end{tabular}
\end{mytabularwrap}
\end{document}


If you'd like it automated for every tabular, then you could use the etoolbox, the important lines are

\BeforeBeginEnvironment{tabular}{\begin{mytabularwrap}}
\AfterEndEnvironment{tabular}{\end{mytabularwrap}}


example 2

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{graphicx}
\usepackage{etoolbox}

\newsavebox{\mybox}
\newenvironment{mytabularwrap}{\begin{lrbox}{\mybox}}
{\end{lrbox}%
\setbox0\hbox{\usebox\mybox}%
\ifdim\wd0<\textwidth
\usebox\mybox%
\else
\resizebox{\textwidth}{!}{\usebox\mybox}%
\fi
}

\BeforeBeginEnvironment{tabular}{\begin{mytabularwrap}}
\AfterEndEnvironment{tabular}{\end{mytabularwrap}}

\begin{document}

hello world

\begin{tabular}{cc}
1 & 2 \\
3 & 4
\end{tabular}

\noindent
\begin{tabular}{*{30}c}
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10
\end{tabular}
\end{document}


For what it's worth, I would recommend against doing this for precisely the reasons that David Carlisle mentioned.

• The line \setbox0\hbox{\usebox\mybox} which reboxes \mybox is useless. You can remove it and use \ifdim\wd\mybox<\textwidth instead of \ifdim\wd0<\textwidth. Jul 17 at 11:47