# Problem with \only and alignment of graphics (pdf) in beamer

I have been trying to include pdf graphics in a document with this simple command:

\documentclass{beamer}

\begin{document}

\begin{frame}
\only<1>{\includegraphics[width=\textwidth]{test}}
\only<2>{\includegraphics[width=\textwidth]{test}}
\only<3>{\includegraphics[width=\textwidth]{test}}
\end{frame}

\end{document}


Unfortunately, the three pictures (that are supposed to be identical), are not aligned identically (from the top).

Here is the test.pdf used for generating the pdf: http://imgur.com/1nOiT

Here is the generated pdf: http://imgur.com/UUu0v (as you can(not?) see), the first and second image are not aligned.

Now if someone knows why it acts like this? Here I put a VME since it seems to me that no package were necessary except for the class Beamer. I have tried with my "usual" packages, maybe I am just missing the right one?

Just so you know, what I was able to do so far: if I put a ~ after {test}, then it gets aligned (note that it will not work on this minimal example, there are some packages missing).

Also something I noticed (because when I first met this probleme I had many more \only), it is always the one before last only that messes up! No matter how many \only, and then the last one will be aligned with the previous one (this is why we need at least three only lines).

-

This is a well known fact; the beamer documentation mentions it in Section 9.5 Dynamically Changing Text or Images:

The trouble with this approach [using the \only command] is that it may lead to slight, but annoying differences in the heights of the lines, which may cause the whole frame to “wobble” from slide to slide. This problem becomes much more severe if the replacement text is several lines long. To solve this problem, you can use two environments: overlayarea and overprint. The first is more flexible, but less user-friendly.

So, you can use an overlayarea:

\documentclass{beamer}

\begin{document}

\begin{frame}
\begin{overlayarea}{\textwidth}{\textheight}
\only<1>{\includegraphics[width=\textwidth]{test}}
\only<2>{\includegraphics[width=\textwidth]{test}}
\only<3>{\includegraphics[width=\textwidth]{test}}
\end{overlayarea}
\end{frame}

\end{document}

-
Thanks, this is indeed a solution, as is mine using the tilde mark, though I would really like to understand why it does not work. –  Gopi May 14 '12 at 15:04
@Gopi: I don't know the cause behind this well known fact. The beamer documentation mentions this in Section 9.5 Dynamically Changing Text or Images: "The trouble with this approach [using the \only command] is that it may lead to slight, but annoying differences in the heights of the lines, which may cause the whole frame to “wobble” from slide to slide. This problem becomes much more severe if the replacement text is several lines long. To solve this problem, you can use two environments: overlayarea and overprint. The first is more flexible, but less user-friendly." –  Gonzalo Medina May 14 '12 at 15:15
O! I didn't know it was a well known fact! (and didn't though it could be, or else I would have looked it up in the man)). Thanks then, care to put your comment in your answer? Then I'll accept it. –  Gopi May 14 '12 at 15:18
@Gopi: I've updated my answer; I've included the quote from the manual. –  Gonzalo Medina May 14 '12 at 15:24

This is just the usual issue with whatsit nodes in vertical mode. You see the same using color changes (even without beamer).

If you make sure they come in horizontal mode then everything works out fine.

\documentclass{beamer}

\begin{document}

\begin{frame}
\mbox{\only<1>{\includegraphics[width=\textwidth]{test}}%
\only<2>{\includegraphics[width=\textwidth]{test}}%
\only<3>{\includegraphics[width=\textwidth]{test}}}
\end{frame}

\end{document}

-