10

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}
  • Did you try to add a blank line between two images and insert the vertical space there? – Sigur Apr 12 '13 at 0:55
  • 2
    No need to use [t!] since beamer disables the floating mechanism. – Gonzalo Medina Apr 12 '13 at 1:02
  • @GonzaloMedina That's good to know, I didn't realize that. – abayesed Apr 12 '13 at 1:44
  • 1
    @Sigur see my response to Kevin. – abayesed Apr 12 '13 at 1:46
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 1: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 1: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 1: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 4:03
  • @GonzaloMedina: See my comment several days ago. – kiss my armpit Apr 12 '13 at 4:10
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}
  • 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 1: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}
  • 1
    Using figure does not make sense in beamer. – kiss my armpit Apr 12 '13 at 1:52
  • @Karl'sstudents why is that? – abayesed Apr 12 '13 at 1:58
  • @abayesed: Because floating in a frame does not work. – kiss my armpit Apr 12 '13 at 2:01

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.