# Centering images and overprint in Beamer

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.

-
AFAIK, the overprint environment acts as a box with the maximum size of any included content. Are all images the same size? Also just try \centering instead of the center environment, or place each \includegraphics[..]{..} into a \centerline{..} to manually center it (if it is supposed to be on a line of its own). –  Martin Scharrer Jun 28 '11 at 16:04
Thanks! \centerline{} did the trick! All images are of the same width, but not the same height; \centering doesn't help –  texman Jun 28 '11 at 16:14

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.

-