21

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).

5 Answers 5

27

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}
4
  • 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, 2012 at 15:04
  • 1
    @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." May 14, 2012 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, 2012 at 15:18
  • @Gopi: I've updated my answer; I've included the quote from the manual. May 14, 2012 at 15:24
13

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}
7
  • This works for me (and is easier than using \overlayarea, which seems to require further jigging of the layout). Didn't need the \mbox, though.
    – Roly
    May 27, 2014 at 11:13
  • @Roly if you don't use \mbox the code here is identical to the MWE in the question, and so presumably has the issue in the question. The \mbox is the only point of this answer, so I don't understand your comment? May 27, 2014 at 11:57
  • 5
    When I tried this, I needed to collapse all the \only's into a single line of input, as you did, using %. Otherwise I get the problem. With \mbox on its own, the problem remains. Conversely, just including the %'s and omitting \mbox fixes it, at least for me.
    – Roly
    May 27, 2014 at 13:45
  • Well that makes me very uneasy. Latex's handling of whitespace leaves something to be desired it seems.
    – daknowles
    Apr 30, 2018 at 23:20
  • 1
    @daknowles some languages ignore white space but TeX is optimised for typing text in a latin script where white space is significant and separates words. The spacing rules of \includegraphics are exactly the same as those of letters and abc is not the same as a b c Apr 30, 2018 at 23:23
11

As Roly pointed out, the solution I found worked best was adding a % to the end of all but the last \includegraphics commands (What is the use of percent signs (%) at the end of lines?).

So this works:

\begin{frame}
  \frametitle{Slide 1}
  \begin{figure}[H]
    \centering
    \only<1>{\includegraphics[width=0.7\textwidth]{thinBody/thinBody0.png}}%
    \only<2>{\includegraphics[width=0.7\textwidth]{thinBody/thinBody1.png}}%
    \only<3>{\includegraphics[width=0.7\textwidth]{thinBody/thinBody2.png}}%
    \only<4>{\includegraphics[width=0.7\textwidth]{thinBody/thinBody3.png}}%
    \only<5>{\includegraphics[width=0.7\textwidth]{thinBody/thinBody4.png}}
    \caption{The caption}
    \label{fig:label}
  \end{figure}
\end{frame}

Or for the code in the question:

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

I don't know why this happens, but it works very well. Perhaps someone else could enlighten us.

1
  • Wow! This is the simplest solution!
    – Tropilio
    Jan 21, 2020 at 16:14
5

I had the same problem, but fixed it by avoiding \only and specifying the frame numbers within \includegraphics.

\documentclass{beamer}

\begin{document}

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

\end{document}
2
1

I think \begin{overprint} with \forloop gives better solution. There is less coding and fitting:

\documentclass{beamer}

\usepackage{forloop} 
\newcounter{slideno}

\begin{document}
\begin{frame}
    \begin{figure}
    \centering
    \begin{overprint}
    \forloop{slideno}{1}{\value{slideno} < 27}{%
            \only<\value{slideno}>{
                \includegraphics[page=\value{slideno}]
                {tikz/my_standalone_beamer_slides}}}
    \end{overprint}
    \caption{Add caption here}
\end{figure}
\end{frame}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.