[Tex/LaTex] Image centered on full slide with background color in beamer

backgroundsbeamergraphics

Specifying both a background color and a background image for beamer slides is a bit tricky when notes are shown. I tried three approaches but none works perfectly (the idea is to avoid white borders above and below a full-screen image).

The first solution was inspired by this answer :

\PassOptionsToPackage{demo}{graphicx}
\documentclass{beamer}

\begin{document}

{
\setbeamercolor{background canvas}{bg=red}
\setbeamertemplate{background canvas}{
  \parbox[c][\paperheight][c]{\paperwidth}{
    \centering\includegraphics[width=\paperwidth]{name}
  }
}
\begin{frame}[plain]
\end{frame}
}

\end{document}

But setting the background color has no effect. I also tried to include the graphics inside the frame but the image is not centered anymore :

\PassOptionsToPackage{demo}{graphicx}
\documentclass{beamer}

\begin{document}

{
\setbeamercolor{background canvas}{bg=red}
\begin{frame}[plain]
\parbox[c][\paperheight][c]{\paperwidth}{
  \centering\includegraphics[width=\paperwidth]{name}
}
\end{frame}
}

\end{document}

My last test is based on this answer :

\PassOptionsToPackage{demo}{graphicx}
\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfpages}

\setbeameroption{show notes on second screen=right}\nofiles

\begin{document}

\begin{frame}[plain]
\end{frame}

{
\setbeamercolor{background canvas}{bg=red}
\begin{frame}[plain]
\begin{tikzpicture}[remember picture,overlay]
\node[at=(current page.center)] {
  \includegraphics[width=\paperwidth]{name}
};
\end{tikzpicture}
\note{Note}
\end{frame}
}

\end{document}

But the image is not positioned correctly when notes are shown (even with the nofiles trick).

Each solution has a distinct problem. Any idea on one possible fix?

Best Answer

The default background canvas is defined as

\defbeamertemplate*{background canvas}{default}
{%
  \ifbeamercolorempty[bg]{background canvas}{}{\color{bg}\vrule width\paperwidth height\paperheight}%
}

So it just draws a box filling the whole frame with the background canvas color. That's why changing the background canvas color does not have any effect anymore, if the default background canvas is overwritten.

But there is another background layer simply called background which is drawn on top of the background canvas. After changing \setbeamertemplate{background canvas} to \setbeamertemplate{background} the first solution works.

\PassOptionsToPackage{demo}{graphicx}
\documentclass{beamer}

\begin{document}

{
\setbeamercolor{background canvas}{bg=red}
\setbeamertemplate{background}{%
  \parbox[c][\paperheight][c]{\paperwidth}{%
    \centering\includegraphics[width=\paperwidth]{name}
  }
}
\begin{frame}[plain]
\end{frame}
}

\end{document}

The second solution doesn't work because every beamer frame has a text margin left (and text margin right) and this shifts the image in this case to the right because with its width of \paperwidth it doesn't fit inside anymore. To solve the issue here one could shift it back using \hspace with a negative length. In this case it is \hspace*{-1cm}.

\PassOptionsToPackage{demo}{graphicx}
\documentclass{beamer}

\begin{document}

{
\setbeamercolor{background canvas}{bg=red}
\begin{frame}[plain]
\hspace*{-1cm}%
\parbox[c][\paperheight][c]{\paperwidth}{
  \centering\includegraphics[width=\paperwidth]{name}
}
\end{frame}
}

\end{document}