3

I'm new to the platform, and I don't have much experience using Latex. I'm trying to learn how to use the packaged \usetikzlibrary{animations} in Tikzpicture, following the steps of the site https://tikz.dev/tikz-animations, but every time I try to run it on my TexMaker machine (windows) and Overleaf, any code of these animations it either shows errors or it doesnt execute any of the animations, which I don't understand and don't know how to fix. Does anyone out there know how I run these Tikzpicture animation codes in Overleaf and TexMaker? Here's an example of a code I tried to use

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{animations} % LATEX and plain TEX
%\usetikzlibrary[animations] % ConTEXt

\begin{document}
The code below doesnt work, keaps getting error
\begin{tikzpicture}[
animate/orbit/.style 2 args = {
    myself:shift = {
      along = {
        (0,0) circle [radius=#1]
      } sloped in #2s/10,
      repeats }} ]

\node :color = {0s = "orange",
             2s = "red",
             4s = "orange",
             repeats}
   {Sun};

\begin{scope}[animate={orbit={2.5cm}{365}}]
\node {Earth};
\node [animate={orbit={1cm}{28}}] {Moon};
\end{scope}

\useasboundingbox (-3.8,-3.8) (3.8,3.8);
\end{tikzpicture} 

The code below doesnt work the animation 
\tikz [animate = {
object = node, attribute = fill, time = 0s, value = red, entry,
object = node, attribute = fill, time = 2s, value = blue, entry,
object = node, attribute = fill, begin on = click, entry}]
\node (node) [fill, text=white] { Click me };
\end{document}

I Also notice that in other for the animation to run you need a special program like Adobe or FoxIt Reader, but how can i incorporate these animations on an Beamer presentation?

2
  • Please attribute code you copy. It makes it much easier to help if you tell people where to find it. (In this case page 380/sec 2.6 of pgf's manual.) What error do you get? I don't get an error when I compile your code. Also, which engine are you using? I do get a warning that my engine doesn't support animations.
    – cfr
    Nov 28, 2023 at 20:44
  • Did you read sec 26.1.1? To make a long story short: TikZ animations currently work only with svg output (and use the smil “flavor” of describing animations). In future, it may well happen that other “flavor” of describing animations will be added, but it is very unlikely that pdf will ever support animations in a useful way. You can apparently include snapshots in a PDF, but you can't create animations this way. If you want to create animations for Beamer, I recommend looking at the methods documented in Beamer's manual or at third-party packages.
    – cfr
    Nov 28, 2023 at 20:50

1 Answer 1

3

As cfr puts it in their comment, PGF's animations library is meant to produce animated SVG output. For this kind of animations, animation frames are computed at viewing time in the web browser.

To make use of them in PDF documents, such as beamer class presentations, snapshots at intermediate stages must be taken, which serve as animation frames. They can be animated using a frame-based animation package, such as animate for example. The PDF output can be viewed in JavaScript-supporting PDF viewers, such as Acrobat Reader or Okular. Foxit may work as well (untested).

Run pdflatex or lualatex twice on the code:

\documentclass{beamer}

\usepackage[T1]{fontenc}
\usepackage{tikz}

\usetikzlibrary{animations} % SVG animations
\usepackage{animate} % frame-based animations

\begin{document}

\newcommand\tikzsnapshot[1]{% arg #1: time of snapshot [s]
  \begin{tikzpicture}[
  make snapshot of = #1, % <== added for taking snapshots of intermediate stages
  animate/orbit/.style 2 args = {
      myself:shift = {
        along = {
          (0,0) circle [radius=##1] % <== escape `#' with 2nd `#' 
        } sloped in ##2s/10, % <== escape `#' with 2nd `#'
        repeats }} ]

  \node :color = {0s = "orange",
               2s = "red",
               4s = "orange",
               repeats}
     {Sun};

  \begin{scope}[animate={orbit={2.5cm}{365}}]
  \node {Earth};
  \node [animate={orbit={1cm}{30.41666}}] {Moon}; % <== adjusted synodic period (=365/12) to
  \end{scope}                                     %  fit 12 full cycles into 365 days

  \useasboundingbox (-3.8,-3.8) (3.8,3.8);
  \end{tikzpicture}%
}

\begin{frame}[fragile]{PGF's \texttt{animations} library example}

\begin{animateinline}[autoplay,controls,loop]{24} % <== frames per second (FPS)=24
\multiframe{\fpeval{36.5*24}}{rTime=0+\fpeval{1/24}}{ % number of animation frames=duration*FPS; snapshot delta t=1/FPS
  \tikzsnapshot{\rTime}
}
\end{animateinline}
\end{frame}

\end{document}

The tikzpicture environment (with minor code changes applied) has been placed in a user-defined LaTeX command which takes the snapshot time as its argument #1. Note the addition of make snapshot of = ... to the environment options. It allows taking snapshots with non-SVG output drivers.

The animate package can also be used to produce frame-based animations in SVG output (as in the linked document at the beginning of this answer). Instructions on making beamer-class slides in SVG format are given in this topic.

3
  • Wow, now he actually produced a video, although I don't understand much of the code you put. It works on Foxit Reader. 1) Do you know where I can get a manual or video that explains how to use this animate package? Correct me if I'm wrong. 1) From what I understand, the Animations package should generate an SVG file, but it doesn't generate this when I run the original code, why does this happen? 2) While the Animate package generates a set of frames, however on my PC it did not generate several images or a separate pdf file with several frames of that image, was that supposed to happen? Dec 9, 2023 at 16:48
  • 1) texdoc animate or on CTAN: ctan.org/pkg/animate you find the link to the manual (PDF) 2) Yes, TikZ-pgf's animation package is for SVG output. How to produce (standalone) SVG is explained in the linked topic in my answer, more precisely here. 3) In the example above, tikzpicture environment is placed in a command (\tikzsnapshot) such that it is parameterised with the snapshot time. Animation frames are then created on-the-fly, by placing \tikzsnapshot in the loop body of the \multiframe command (see animate manual for usage).
    – AlexG
    Dec 9, 2023 at 20:38
  • Also, I added a few comments to the original code of the tikzpicture environment that give you some hints.
    – AlexG
    Dec 10, 2023 at 11:53

You must log in to answer this question.

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