adjust the array size to the size of the frame+Beamer

I inserted a table into a page of a presentation Beamer but the problem is that the array size exceeds the frame. How to adjust the array size to the size of the frame?

-

migrated from stackoverflow.comJul 3 '12 at 18:04

This question came from our site for professional and enthusiast programmers.

Use the shrink option of frame environment (beamer manual v3.18, p.63). If the correct value of shrink exceeds 15 or 20 (this is a percentage), ask yourself if this table is really needed... ;-) –  Paul Gaborit Jul 3 '12 at 21:39

You can resize a tabular to fit exactly inside the text block of the frame using the functionality provided by graphicx - loaded by default by the beamer class. More specifically, you can use \resizebox{<width>}{<height>}{<stuff>} to resize <stuff> to width <width> and height <height>. If you wish to maintain the aspect ratio of <stuff>, replace one of the lengths with !:

\documentclass{beamer}% http://ctan.org/pkg/beamer
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\begin{document}
\begin{frame}
\frametitle{A large table that doesn't fit}

​  \begin{tabular}{*{26}{c}}
\toprule
A & B & C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q & R & S & T & U & V & W & X & Y & Z \\
\midrule
01 & 02 & 03 & 04 & 05 & 06 & 07 & 08 & 09 & 10 & 11 & 12 & 13 &
14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\
26 & 25 & 24 & 23 & 22 & 21 & 20 & 19 & 18 & 17 & 16 & 15 & 14 &
13 & 12 & 11 & 10 & 09 & 08 & 07 & 06 & 05 & 04 & 03 & 02 & 01 \\
\bottomrule
\end{tabular}
\end{frame}

\begin{frame}
\frametitle{A large table that does fit!}

\resizebox{\textwidth}{!}{%
\begin{tabular}{*{26}{c}}
\toprule
A & B & C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q & R & S & T & U & V & W & X & Y & Z \\
\midrule
01 & 02 & 03 & 04 & 05 & 06 & 07 & 08 & 09 & 10 & 11 & 12 & 13 &
14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\
26 & 25 & 24 & 23 & 22 & 21 & 20 & 19 & 18 & 17 & 16 & 15 & 14 &
13 & 12 & 11 & 10 & 09 & 08 & 07 & 06 & 05 & 04 & 03 & 02 & 01 \\
\bottomrule
\end{tabular}%
}
\end{frame}
\end{document}​


Of course, this also shrinks the font size (actually everything, including the rule widths). If this is not ideal, you can use some methods provided in Column padding in tables (to remove some space) or perhaps reduce the number of columns altogether. Perhaps even displaying them on separate frames using column selection. For this see Easiest way to delete a column?

Although the above discussion references tabular, it applies equally to array as well. The syntax would resemble

\resizebox{\textwidth}{!}{%
$\begin{array}{<col spec>} %... \end{array}$%
}

-

use a smaller value for \tabcolsep and enlarge the width:

\documentclass{beamer}
\usepackage{booktabs}
\begin{document}

\begin{frame}
\frametitle{A large table that does fit!}
\makebox[\linewidth]{\resizebox{1.15\linewidth}{!}{%
\tabcolsep=1.5pt%
\begin{tabular}{@{}*{26}{c}@{}} \toprule
A & B & C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q & R & S & T & U & V & W & X & Y & Z \\ \midrule
01 & 02 & 03 & 04 & 05 & 06 & 07 & 08 & 09 & 10 & 11 & 12 & 13 &
14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\
26 & 25 & 24 & 23 & 22 & 21 & 20 & 19 & 18 & 17 & 16 & 15 & 14 &
13 & 12 & 11 & 10 & 09 & 08 & 07 & 06 & 05 & 04 & 03 & 02 & 01 \\\bottomrule
\end{tabular}}}
\end{frame}
\end{document}​


-

With \resizebox of the graphicx package you could perfectly fit anything to the frame width, as Werner and Herbert mentioned, tables as well as pictures and text.

However, this way you would also scale the text in the table. With some fonts it may be ok, but imagine you would scale a huge font down or a tiny font to large - perhaps you won't get the best quality. Also, the table is scaled as a whole, including column separation and line widths. Different tables on different frames could get an inconsistent design.

As I already answered to your cross-post beamer | Array Size Adjustment to Frame Size in the LaTeX-Community.org forum, I would spend some effort on the tables and

• Choose a font size which fits well
• Use tabularx which supports a width argument to get the perfect size matching the frame width, specifying \textwidth, \linewidth, \columnwidth or use one with a factor
• Shrink the column separation

Example:

\documentclass{beamer}
\usepackage{tabularx}
\begin{document}
\begin{frame}\small
\frametitle{Fitting a table}
\begin{tabularx}{\textwidth}{*{20}{@{}X@{}}}
01 & 02 & 03 & 04 & 05 & 06 & 07 & 08 & 09 & 10 &
11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20
\end{tabularx}
\normalsize
\end{frame}
\end{document}​


Here, \normalsize might not be important since the size was changed locally in the frame, but to be on the safe side, call it or use a scope with a group or environment.

-