48

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)}{!}{% ... %}
Werner
  • 603,163
Cookie
  • 775

2 Answers2

47

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.

egreg
  • 1,121,712
  • 1
    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. – Cookie Feb 10 '13 at 05: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). – Jonathan Komar May 31 '13 at 07: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. – Martin Scharrer Jun 26 '13 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 '16 at 13:08
  • This scales the text = absolute crap. – reggie Sep 07 '16 at 08:38
  • @reggie I agree, but the customer's always right. ;-) – egreg Sep 07 '16 at 08:48
6

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.

David Carlisle
  • 757,742
cmhughes
  • 100,947
  • 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. – frougon Jul 17 '22 at 11:47