4

I am expecting the animation starts appearing from the fourth frame rather than from the first frame. How to do so?

\documentclass[12pt,aspectratio=1610]{beamer}
\usepackage{adjustbox}
\usepackage{animate}
\begin{document}
\section{Section 1}
\begin{frame}[t]{Frame Title}
    \begin{columns}[t]
        \begin{column}{0.5\textwidth}
            \pause
            \adjincludegraphics[width=\linewidth,valign=t,center]{example-image-a}
            \pause
            \[
                Kim=Jong+Un
            \]
        \end{column}
        \begin{column}{0.5\textwidth}
            \pause
            \begin{adjustbox}{width=\linewidth,valign=t,center}
                \animategraphics[controls,loop]{1}{example-image-duck}{}{}
            \end{adjustbox}
            \pause
            \begin{itemize}
                \item Kim Jong Un\pause
                \item Peace Be Upon Him\pause
            \end{itemize}
            \begin{center}
                The end of the world!
            \end{center}
        \end{column}
    \end{columns}
\end{frame}
\end{document}
  

enter image description here

Bonus question:

How to prevent the animation from restarting when slides are navigated forward? The next slide must show the current frame of the animation rather than the first frame of the animation.

1
  • I will grant a bounty of 500 for the accepted answer and another bounty of 500 for the first user who can solve the bonus question. Is it fair enough, right? May 10, 2021 at 8:30

1 Answer 1

4
+1000

In essence, it is a recycled version of an older post that was adapted to the specific example given in the question. The solution is based on animate's JS API, and on the beamer class' internal \ifbeamer@anotherslide.

All requested features have been addressed. Also, the current playing state (paused or playing, forwards or backwards) and the current frame rate of the animation are communicated between overlays. Successfully tested to work with Acrobat Reader and with Okular.

Use it as follows and note the way of how the animation is labelled, <label>_\thepage, and of how the label is used with the bracketing commands, \putBeforeAnim{<label>}/\putAfterAnim{<label>}. Also, mind the draft option in the alternative clause of \doifvisible.

\doifvisible{%
  \putBeforeAnim{duck}%
  \animategraphics[label=duck_\thepage,controls,loop]{1}{example-image-duck}{}{}%
  \putAfterAnim{duck}%
}{
  \phantom{\animategraphics[draft,controls,loop]{1}{example-image-duck}{}{}}%
}

Complete example. Just ignore the undefined references warning. (The warning is harmless and is because of placing \animategraphics in a \phantom box. To be fixed in the next version.)

\documentclass[12pt,aspectratio=1610]{beamer}
\usepackage{adjustbox}
\usepackage{animate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \putBeforeAnim{<label>}
%   \animategraphics[label=<label>_\thepage,...]{..}{..}{..}{..}
%   or
%   \begin{animateinline}[label=<label>_\thepage,...]{..}
%   ...
%   \end{animateinline}
% \putAfterAnim{<label>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\putBeforeAnim[1]{%
  \myPdfAnnot{1ex}{1ex}{0pt}{%
    /Subtype/Widget/FT/Btn/Ff 65537/T (#1_a_\thepage)%
    /AA <<%
      /PC << % exec on page-close
        /S/JavaScript /JS (
          try{
            var #1CurFrame  =anim['#1_\thepage'].frameNum;
            var #1CurSpeed  =anim['#1_\thepage'].speed;
            var #1PlayingFwd=anim['#1_\thepage'].isPlaying&&  anim['#1_\thepage'].playsFwd;
            var #1PlayingBwd=anim['#1_\thepage'].isPlaying&&(!anim['#1_\thepage'].playsFwd);
          } catch(e){}
        )
      >>
    >>%
  }%
}
\newcommand\putAfterAnim[1]{%
  \myPdfAnnot{1ex}{1ex}{0pt}{%
    /Subtype/Widget/FT/Btn/Ff 65537/T (#1_b_\thepage)%
    /AA <<%
      /PO << % exec on page-open
        /S/JavaScript /JS (
          try{
            anim['#1_\thepage'].frameNum=#1CurFrame;
            anim['#1_\thepage'].speed=#1CurSpeed;
            if(#1PlayingFwd){anim['#1_\thepage'].playFwd();}
            if(#1PlayingBwd){anim['#1_\thepage'].playBwd();}
          } catch(e){}
        )
      >>
    >>
  }%
}
\ExplSyntaxOn
  \let\myPdfAnnot\pbs_pdfannot:nnnn
\ExplSyntaxOff
\makeatletter
\newcommand\doifvisible[2]{\ifbeamer@anotherslide #2\else #1\fi}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\section{Section 1}
\begin{frame}[t]{Frame Title}
    \begin{columns}[t]
        \begin{column}{0.5\textwidth}
            \pause
            \adjincludegraphics[width=\linewidth,valign=t,center]{example-image-a}
            \pause
            \[
                Kim=Jong+Un
            \]
        \end{column}
        \begin{column}{0.5\textwidth}
            \pause
            \begin{adjustbox}{width=\linewidth,valign=t,center}
              \doifvisible{%
                \putBeforeAnim{duck}%
                \animategraphics[label=duck_\thepage,controls,loop]{1}{example-image-duck}{}{}%
                \putAfterAnim{duck}%
              }{%
                \phantom{\animategraphics[draft,controls,loop]{1}{example-image-duck}{}{}}%
              }
            \end{adjustbox}
            \pause
            \begin{itemize}
                \item Kim Jong Un\pause
                \item Peace Be Upon Him\pause
            \end{itemize}
            \begin{center}
                The end of the world!
            \end{center}
        \end{column}
    \end{columns}
\end{frame}
\end{document}
4
  • 1
    1000 will come in a couple of days. Thank you very much. Will you make the auxiliary macros putBeforeAnim and putAfterAnim available in a package (kimjongun package)? Reusing these macros across multiple input files is a bit cumbersome and difficult to maintain. :-) May 10, 2021 at 10:29
  • Thank you a lot! Much appreciated!
    – AlexG
    May 10, 2021 at 10:30
  • 2
    Fixed to also support Okular PDF viewer.
    – AlexG
    May 10, 2021 at 12:33
  • 2
    one more fix: \alt<...> replaced with \doifvisible to get rid of overlay specification
    – AlexG
    May 10, 2021 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .