[Tex/LaTex] Centering images and overprint in Beamer

beamerhorizontal alignmentoverlays

I'm trying to create a beamer presentation with several images replacing each other on consecutive slides. I've gotten it to work with overprint and \onslide, but cannot get the images centered on the slide.

\begin{center}
  \begin{overprint}
    \onslide<2>\includegraphics[scale=0.3]{images/stack1.pdf}    
    \onslide<3>\includegraphics[scale=0.3]{images/stack2.pdf}
    \onslide<4>\includegraphics[scale=0.3]{images/stack3.pdf}
    \onslide<5>\includegraphics[scale=0.3]{images/stack4.pdf}
    \onslide<6>\includegraphics[scale=0.3]{images/stack5.pdf}
    \onslide<7>\includegraphics[scale=0.3]{images/stack6.pdf}
    \onslide<8>\includegraphics[scale=0.3]{images/stack7.pdf}
  \end{overprint}
\end{center}

Anything outside of the overprint environment gets properly centered, but not the images themselves.

Best Answer

The overprint environment has a fixed width which defaults to the \textwidth (or maybe the similar \linewidth) and can be given using the optional argument. The whole environment is then centered, not just the image(s) inside it, which are internally left aligned. Because the environment is already as wide as the given space the centering effectively doesn't change its placement. You need to center the images inside the overprint area instead (e.g. using \centerline{\includegraphics[..]{..}}}) or reduce the overprint environment to the image width manually so that it can be effectively centered.

\documentclass{beamer}

\begin{document}

\begin{frame}{Test}
  \begin{center}
  text before
  \pause
  \begin{overprint}%
    \onslide<2>\centerline{\includegraphics[scale=0.3]{images/stack1.pdf}}%
    \onslide<3>\centerline{\includegraphics[scale=0.3]{images/stack2.pdf}}%
    \onslide<4>\centerline{\includegraphics[scale=0.3]{images/stack3.pdf}}%
    \onslide<5>\centerline{\includegraphics[scale=0.3]{images/stack4.pdf}}%
    \onslide<6>\centerline{\includegraphics[scale=0.3]{images/stack5.pdf}}%
    \onslide<7>\centerline{\includegraphics[scale=0.3]{images/stack6.pdf}}%
    \onslide<8>\centerline{\includegraphics[scale=0.3]{images/stack7.pdf}}%
  \end{overprint}
  text after
  \end{center}
\end{frame}

\end{document}

or

\documentclass{beamer}

\begin{document}

\begin{frame}{Test}
    \begin{center}
  text before
  \pause

  \begin{overprint}[7cm]%
    \onslide<2>\includegraphics[width=7cm]{images/stack1.pdf}%
    \onslide<3>\includegraphics[width=7cm]{images/stack2.pdf}%
    \onslide<4>\includegraphics[width=7cm]{images/stack3.pdf}%
    \onslide<5>\includegraphics[width=7cm]{images/stack4.pdf}%
    \onslide<6>\includegraphics[width=7cm]{images/stack5.pdf}%
    \onslide<7>\includegraphics[width=7cm]{images/stack6.pdf}%
    \onslide<8>\includegraphics[width=7cm]{images/stack7.pdf}%
  \end{overprint}%

  text after
  \end{center}
\end{frame}

\end{document}

I replaced the scale key with width here to ensure that the images have the same width as the overprint environment. I prefer the first \centerline solution because you don't have to match any widths.