12

I have a sequence of images I would like to animate in beamer. In each frame, I would like to have two images, say A and B, aligned vertically and centred on the page. All images are the same size.

I tried aligning the images using \vspace{}, but then the vertical spaces accumulate and the images slide down as I transition through them.

How should I align the images in each frame?

Here is a minimal working example:

\documentclass{beamer}
\begin{document}
\frame{
    \begin{figure}[t!]
    \includegraphics<1>[scale=.5]{fig/imageA1} \vspace{.1in}        
    \includegraphics<1>[scale=.5]{fig/imageB1}

    \includegraphics<2>[scale=.5]{fig/imageA2} \vspace{.1in}        
    \includegraphics<2>[scale=.5]{fig/imageB2} 

    \includegraphics<3>[scale=.5]{fig/imageA3} \vspace{.1in}        
    \includegraphics<3>[scale=.5]{fig/imageB3}

    \includegraphics<4>[scale=.5]{fig/imageA4} \vspace{.1in}        
    \includegraphics<4>[scale=.5]{fig/imageB4} 

      \end{figure} 
    }   
\end{document}
lockstep
  • 250,273
abayesed
  • 155

3 Answers3

8

Using floating environments (such as figure or table) in a frame does not work in the way it should. To accomplish your objective, turn on allowpagebreak and T. Adjust the graphics with height option. See the code below for the remaining.

enter image description here

\documentclass{beamer}
\usepackage{graphicx}
\begin{document}
\frame[allowpagebreak,T]
{%
        \only<1>
        {%
            \centering
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image-a}

            \vfill
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image-a}
        }%

        \only<2>
        {%
            \centering
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image-b}

            \vfill
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image-b}
        }%

        \only<3>
        {%
            \centering
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image-c}

            \vfill
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image-c}
        }%

        \only<4>
        {%
            \centering
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image}

            \vfill
            \includegraphics[height=\dimexpr0.5\textheight-0.5in]{example-image}
        }%
}
\end{document}

For creating GIF animation see my other answer here.

  • Thanks for this solution, it works. How did you generate the gif, by the way? – abayesed Apr 12 '13 at 01:48
  • @abayesed: If it works the way you want, why don't you accept it with a green check mark below the downward arrow? I will show you how to generate GIF soon. – kiss my armpit Apr 12 '13 at 01:50
  • sorry, I didn't realize you were supposed to do that. This is my first post on TeX.sx. – abayesed Apr 12 '13 at 01:53
  • Why do you say using figure doesn't work? It It won't produce a floating object (since beamer disables them), but it gives the possibility of using \caption. – Gonzalo Medina Apr 12 '13 at 04:03
  • @Karl'sstudents yes, as I said, beamer disables floating, but that doesn't mean that using figure doesn't work. It works (without flotation) for captioninig tables or figures with the standard \caption command. (Not that I'm a fan of captioning things in a presentation but I know people who does it and even people who requires it). – Gonzalo Medina Apr 12 '13 at 04:17
  • @GonzaloMedina: OK. I have to say the floating in a frame does not work in the way it should instead. – kiss my armpit Apr 12 '13 at 04:44
3

Instead of \vspace, you can use \\ to force a line break.

\documentclass{beamer}
\begin{document}
\frame{
    \begin{figure}[t!]
    \includegraphics<1>[scale=.5]{img} \\
    \includegraphics<1>[scale=.5]{img}

    \includegraphics<2>[scale=.5]{img} \\
    \includegraphics<2>[scale=.5]{img} 

    \includegraphics<3>[scale=.5]{img} \\
    \includegraphics<3>[scale=.5]{img}

    \includegraphics<4>[scale=.5]{img} \\
    \includegraphics<4>[scale=.5]{img} 

      \end{figure} 
    }   
\end{document}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
Herr K.
  • 17,946
  • 4
  • 61
  • 118
  • I tried forcing a line break this way, but I get an error when I compile saying "There's no line here to end". – abayesed Apr 12 '13 at 01:43
2

If you only have one line break in total instead of one for every image pair, your problem should disappear. The point is that your \vspace{.1in} commanded is not masked by the <1>,<2>,<3>, or <4>:

\documentclass{beamer}
\begin{document}
\frame{
  \begin{figure}[t!]
    \includegraphics<1>[scale=.5]{fig/imageA1}
    \includegraphics<2>[scale=.5]{fig/imageA2}
    \includegraphics<3>[scale=.5]{fig/imageA3}
    \includegraphics<4>[scale=.5]{fig/imageA4}
    \vspace{.1in}        
    \includegraphics<1>[scale=.5]{fig/imageB1}
    \includegraphics<2>[scale=.5]{fig/imageB2}
    \includegraphics<3>[scale=.5]{fig/imageB3}
    \includegraphics<4>[scale=.5]{fig/imageB4}
  \end{figure} 
}   
\end{document}
Mensch
  • 65,388
DCTLib
  • 1,914