Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)}{!}{% ... %}
share|improve this question
1  
What do the double backslashes mean? Are you using some other software for producing the tables? In any case, the adjustbox package might help. –  egreg Feb 9 '13 at 22:42
1  
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. –  David Carlisle Feb 9 '13 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... –  Cookie Feb 10 '13 at 0:25
    
@egreg: Indeed, they were there to escape cat-ing from R, forgot to remove those –  Cookie Feb 10 '13 at 0:37

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
    
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 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). –  macmadness86 May 31 '13 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. –  Martin Scharrer Jun 26 '13 at 11:48

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.