# Automatically size beamer images and tables

## 2018/10/11

“Democracy is the worst form of government, except for all the others” - Winston Churchill (although apparently he was quoting someone else)

This is how I feel about beamer. I still haven’t figured out the perfect solution. Markdown + pandoc can work, but I always end up hacking in LaTeX, since I can be particular about what my slides look like. Recently I’ve started using the metropolis theme, which does a lot of things better than vanilla beamer. It doesn’t avoid all of the usual beamer pain, though. My least favorite (and most repetitive) activity is resizing figures and tables to fit slides properly. In preparation for an upcoming presentation, I decided to see if there was a way around that. There is. Below is some code that autoscales images and tables (with notes) in a beamer presentation.

Here’s an archive with the code below, plus example table, figure, and PDF output.

# Autoscaled beamer presentation template

\documentclass{beamer}

\usetheme{metropolis} % Preferred beamer theme

\usepackage{booktabs} % Nice tables
\usepackage{dcolumn} % Booktabs column spacing
\usepackage{threeparttable} % Align column caption, table, and notes
\usepackage{showframe} % Useful for debugging

% Fancy fit image command with optional caption
\makeatletter
\newcommand{\fitimage}[2][\@nil]{
\begin{figure}
\includegraphics{#2}
\def\tmp{#1}%
\ifx\tmp\@nnil
\else
\caption{#1}
\fi
\end{figure}
}
\makeatother

\begin{document}

\begin{frame}{Autoscaling image}
\fitimage[Autoscaling image]{myimage.png}
\end{frame}

\begin{frame}{Autoscaling table}
\begin{table}
\begin{threeparttable}
\caption{Autoscaling table}
\input{mytable.tex}
\end{threeparttable}
\end{table}
\end{frame}

\end{document}

Note that I use threeparttable for my tables. This isn’t necessary, but it’s fairly standard for me since it handles regression tables nicely. Also note that this expands to the full width and height of the available frame. This is a lot of code for a single figure or table, but can easily be shrunk into a reusable macro.

# Bonus: autoscaled document

The setup is fairly similar for autoscaling figures in a regular document.

\documentclass{article}

\usepackage{booktabs} % Nice tables
\usepackage{dcolumn} % Booktabs column spacing
\usepackage{threeparttable} % Align column caption, table, and notes

\usepackage{showframe} % Useful for debugging

% Flexible notes environment based on minipage
\newenvironment{notes}[1][Notes]{\begin{minipage}[t]{\linewidth}\normalsize{\itshape#1: }}{\end{minipage}}

\begin{document}

\begin{figure}
\caption{Autoscaling image}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{myimage.png}
\begin{notes}
This is a really fantastic image, even more fantastic for having been autoscaled.
\end{notes}
\end{figure}

\begin{table}
* p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01. This is a really fantastic table, even more fantastic for having been autoscaled.
\end{document}
Here I include notes as well, which are more useful for a document (I try to avoid tables with notes, or even better tables at all, in my presentations). In general, however, I prefer to not use adjustbox on my tables in documents to ensure consistent font size throughout (less important in presentations).