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

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.


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.

Tags:

Scaling

Tables