# Set image to full all available space in Beamer (without overlapping other elements)

I want to have an image take up as much as possible of the content area in a beamer presentation, without overlapping the title, the sidebars, or the footers. How can I achieve this?

The code below gives an example of what goes wrong using \includegraphics and setting the width and height from \textwidth and \textheight -- if you try it, you'll see that the footer at the bottom is covered.

One can, of course, do something like height=0.9\textheight but that is annoying and not particularly robust if one changes themes... I'm looking for a more elegant solution.

\documentclass{beamer}
\mode<presentation>{
\usetheme{Marburg}
}
\usepackage[utf8x]{inputenc}

\begin{document}
\includegraphics[width=\textwidth,height=\textheight]{tmp.jpg}
\end{frame}
\end{document}

-
Somewhat related question (the problem is the same, however no really satisfactory solution was provided): Automatically scale big and small graphics for beamer presentations – diabonas Feb 11 '12 at 23:20
The post [tex.stackexchange.com/questions/1858/… also provides references to \beamer@frametextheight but I haven't been able to figure out how to get it to work – Mark Feb 12 '12 at 6:55
Great! \beamer@frametextheight seems like a good way to go. I had two problems with it: 1. It seems to be defined too late, when beamer actually typesets the slide. So in order to use it, you'll have to define your own version of the macro emulating its behaviour. 2. It only into account the height of the title, not the height of head- or footlines. – diabonas Feb 12 '12 at 13:36

The following code demonstrates how to include an image full-size. To customize it, simply change image.jpg to the file name of the picture you want to display. If you're using a beamer theme without a sidebar (such as the default one, Ilmenau, AnnArbor, ...), alter \sidebarthemetrue to \sidebarthemefalse.

\documentclass{beamer}

\usetheme{Marburg}

\newif\ifsidebartheme
\sidebarthemetrue

\newdimen\contentheight
\newdimen\contentwidth
\newdimen\contentleft
\newdimen\contentbottom
\makeatletter
\newcommand*{\calculatespace}{%
\contentheight=\paperheight%
\ifx\beamer@frametitle\@empty%
\setbox\@tempboxa=\box\voidb@x%
\else%
\setbox\@tempboxa=\vbox{%
\vbox{}%
{\parskip0pt\usebeamertemplate***{frametitle}}%
}%
\ifsidebartheme%
\fi%
\fi%
\ifbeamer@plainframe%
\contentbottom=0pt%
\else%
\contentbottom=\footheight%
\fi%
\contentwidth=\paperwidth%
\ifbeamer@plainframe%
\contentleft=0pt%
\else%
\contentleft=\beamer@leftsidebar%
\fi%
}
\makeatother

\begin{document}
{
\setbeamertemplate{background canvas}{%
\calculatespace%
\begin{pgfpicture}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\paperwidth}{\paperheight}}
\ifbeamercolorempty[bg]{background canvas}{}{\color{bg}\pgfusepath{fill}}
\pgftext[at=\pgfpoint{\contentleft+0.5\contentwidth}{\contentbottom+0.5\contentheight}]{\includegraphics[width=\contentwidth,height=\contentheight]{image.jpg}}
\end{pgfpicture}%
}
Frame content
\end{frame}
}
\end{document}


### Result:

(The image used is by Steve Peter and can be downloaded at http://www.tug.org/publicity/wallpaper/.)

## Explanation of the code:

### 1. Calculating the space available to content

These dimensions will hold the height and width available and the absolute position of the content in relation to the lower left corner of the page (which is necessary when there is a left sidebar or a footline):

\newdimen\contentheight
\newdimen\contentwidth
\newdimen\contentleft
\newdimen\contentbottom


Themes with sidebars, such as Marburg, require some extra treatment, as there's a \vskip-1em in the definition of the frametitle template which distorts the calculations:

\newif\ifsidebartheme
\sidebarthemetrue


The macro \calculatespace is the core piece which computes all the lengths defined above:

\makeatletter
\newcommand*{\calculatespace}{%


We begin with \paperheight and subtract the height of the frame title and subtitle (code adapted from beamerbaseframe.sty, ll. 114-129):

\contentheight=\paperheight%
\ifx\beamer@frametitle\@empty%
\setbox\@tempboxa=\box\voidb@x%
\else%
\setbox\@tempboxa=\vbox{%
\vbox{}%
{\parskip0pt\usebeamertemplate***{frametitle}}%
}%
\ifsidebartheme%
\fi%
\fi%


If it is a plain frame, this is everything that has to be taken into account. Otherwise, also subtract the height of head- and footline (cf. beamerbaseframecomponents.sty, ll. 161-182):

\ifbeamer@plainframe%
\contentbottom=0pt%
\else%
\contentbottom=\footheight%
\fi%


Like this, we have calculated \contentheight and \contentbottom. To obtain the content width, one has to subtract the width of the sidebars from \paperwidth if it's not a plain frame (beamerbaseframesize.sty, ll. 80-85):

\contentwidth=\paperwidth%
\ifbeamer@plainframe%
\contentleft=0pt%
\else%
\contentleft=\beamer@leftsidebar%
\fi%


### 2. Displaying the image

We now have a macro \calculatespace that computes all the necessary dimensions. It has to be called separately for each frame as the dimensions might change depending on whether you use titles and subtitles. The inclusion of the image is done in the background canvas template. It is scaled to the full width and height available and placed in the middle of the content space:

\setbeamertemplate{background canvas}{%
\calculatespace%
\begin{pgfpicture}
\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\paperwidth}{\paperheight}}
\ifbeamercolorempty[bg]{background canvas}{}{\color{bg}\pgfusepath{fill}}
\pgftext[at=\pgfpoint{\contentleft+0.5\contentwidth}{\contentbottom+0.5\contentheight}]{\includegraphics[width=\contentwidth,height=\contentheight]{image.jpg}}
\end{pgfpicture}%
}


If you don't want the image to be distorted, simply add keepaspectratio as an optional argument to \includegraphics.

-
Brilliant. I have kept modified versions of the beamer base definition files for years, in order to have a frametextheight. Your code is now the core of a rewrite of a set of image-frame macros. Thank you! – CPBL Nov 20 '14 at 16:07
Actually, I'm having trouble getting your example frame to respect a \setbeamercolor{background canvas}{bg=black} command. If I include a keepaspectratio option in the includegraphics call, then there is some empty space in the content area. I to be able to control the color of that space, but cannot do it the way I am used to. Can you see the reason? – CPBL Nov 21 '14 at 10:14
@CPBL I've modified the code so that \setbeamercolor{background canvas}{bg=black} should work as usual. – diabonas Nov 22 '14 at 9:33