Graphics, Figures & TablesOnly resize table/figure down to fit, otherwise leave as is

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
jayonker
Posts: 2
Joined: Wed Mar 09, 2011 12:38 pm

Only resize table/figure down to fit, otherwise leave as is

Post by jayonker »

Is there a way to only resize a figure or table down to fit in the available width? I regularly insert a given table (from an external file) into multiple documents with varying \linewidth parameters. I would like an efficient way to have a table scaled down to fit \linewidth but not scaled up if it fits without scaling.

That is, if the table or figure is larger than the current \linewidth, then the figure or table is automatically scaled down to fit \linewidth. If the table or figure fits within \linewidth without resizing, then leave as is.

I normally use the \resizebox command using \linewidth as the width parameter, which works well for large tables that always need to be scaled down. However, this will resize a small table up (make larger) to fit \linewidth even when it is not necessary. Given the number of tables I have and the number of different \linewidth parameters I’m dealing with, I would like to figure out a way to have this done automatically. Ideally this code or modification would be part of the table itself (external file) so that wherever the table is inserted I get the desired result. I would appreciate any help.

Below is a simple example demonstrating my point using twocolumn and onecolumn modes within the same document (just for purposes of modifying \linewidth). The first table is correctly scaled down to fit twocolumn mode. The second table in onecolumn mode, however, is scaled up. I do not want this. I want the equivalent of the third table. Remember, in my particular case I am pulling the table from an external file (using the \input command) and would like to only have one version that can be inserted into multiple documents with varying \linewidth parameters.

Code: Select all

\documentclass{article}
\usepackage{graphicx}

\newcommand{\texample}{\resizebox{\linewidth}{!}{
	               \begin{tabular}{ccc}
                         Cell 1,1 Contents & Cell 1,2 Contents & Cell 1,3 Contents \\
                         Cell 2,1 Contents & Cell 2,2 Contents & Cell 2,3 Contents \\
                         Cell 3,1 Contents & Cell 3,2 Contents & Cell 3,3 Contents \\
                         Cell 4,1 Contents & Cell 4,2 Contents & Cell 4,3 Contents \\
                         Cell 5,1 Contents & Cell 5,2 Contents & Cell 5,3 Contents \\
                       \end{tabular}}}


\begin{document}

  \centering
  \twocolumn
  This table is correctly scaled down to fit two-column mode.
  
  \texample

  \onecolumn
  This table is scaled up, but would fit without rescaling in one-column mode.
  
  \texample
  
  This is the table without scaling.
  
  \begin{tabular}{ccc}
    Cell 1,1 Contents & Cell 1,2 Contents & Cell 1,3 Contents \\
    Cell 2,1 Contents & Cell 2,2 Contents & Cell 2,3 Contents \\
    Cell 3,1 Contents & Cell 3,2 Contents & Cell 3,3 Contents \\
    Cell 4,1 Contents & Cell 4,2 Contents & Cell 4,3 Contents \\
    Cell 5,1 Contents & Cell 5,2 Contents & Cell 5,3 Contents \\
  \end{tabular}
\end{document}
Last edited by jayonker on Sun Mar 13, 2011 3:43 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Only resize table/figure down to fit, otherwise leave as

Post by west.logan »

That's a tough one. I'm thinking of possibly doing some kind of comparative conditional statement, but one would need to know the approximate length of the table to be inserted, which I would assume would mean typesetting it, then getting the length, then re-typesetting it with the correct size based on the condition of being smaller than the linewidth. I'm not seeing a quick an easy way for this.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Only resize table/figure down to fit, otherwise leave as is

Post by gmedina »

The process has been described by west.logan; using boxes the implementation is quite simple (there's no need to type the table twice):

Code: Select all

\documentclass{article}
\usepackage{graphicx}

\newsavebox\mybox
\savebox\mybox{\begin{tabular}{ccc}
                         Cell 1,1 Contents & Cell 1,2 Contents & Cell 1,3 Contents \\
                         Cell 2,1 Contents & Cell 2,2 Contents & Cell 2,3 Contents \\
                         Cell 3,1 Contents & Cell 3,2 Contents & Cell 3,3 Contents \\
                         Cell 4,1 Contents & Cell 4,2 Contents & Cell 4,3 Contents \\
                         Cell 5,1 Contents & Cell 5,2 Contents & Cell 5,3 Contents \\
                       \end{tabular}}

\newlength\TableWidth

\newcommand\mytable{%
  \settowidth\TableWidth{\usebox\mybox}
  \ifnum\TableWidth>\linewidth
    \setlength\TableWidth{\linewidth}
  \fi
  \resizebox{\TableWidth}{!}{\usebox\mybox}
}

\begin{document}

  \centering
  \twocolumn
  This table is correctly scaled down to fit two-column mode.
  
  \mytable

  \onecolumn
  The table fits without rescaling in one-column mode, so the table won't be rescaled.

  \mytable

  \begin{minipage}{8cm}
    \mytable
  \end{minipage}

  \begin{minipage}{3cm}
    \mytable
  \end{minipage}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
jayonker
Posts: 2
Joined: Wed Mar 09, 2011 12:38 pm

Re: Only resize table/figure down to fit, otherwise leave as

Post by jayonker »

Thank you. This worked great and was very helpful.
Post Reply