0

I am trying to create a GIF using Tikz "animations" library. I want to create snapshots of frames of the GIF as latex PDF and then merge them into a GIF using online tools. Below is my code. For some reason, my initial state whose "fill" attribute I change is not displayed without generating error. Any suggestions on how to fix the problem? Also I would like to have each snapshot in a separate page without having to change the coordinate of my automaton each time I want take snapshot. How can I do that?

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows.meta,calc,cd,animations}
\usetikzlibrary{patterns}
\usetikzlibrary{datavisualization}
\tikzset{
->, % makes the edges directed
>=stealth, % makes the arrow heads bold
node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
every state/.style={thick, fill=gray!10}, % sets the properties for each ’state’ node
initial text=$ $, % sets the text that appears on the start arrow
}
\usepackage{animate}

\title{GIF Example} \author{Mostafa Tavakkoli Anbarani} \date{May 2023}

\begin{document}

\maketitle \clearpage

\begin{tikzpicture}[node distance = 2cm, on grid,>={Stealth[inset=0pt,length=6pt,angle'=28,round]}]

\foreach \t in {0,2}{
    \tikz [make snapshot of = \t]
    \tikz [animate = {q1:fill = {0s = "red", 2s = "blue"}}]
    \node[state, initial,fill] at (0,0-10*\t) (q1) {$q^1$};
    \node[state] at (1,-2.5-10*\t) (q2) {$q^2$};
    \node[state, right of=q2] (q3) {$q^3$};
    \node[state, right of=q3] (q4) {$q^4$};
    \node[state, right of=q4] (q5) {$q^5$};
    \node[state,accepting] at (8,0-10*\t) (q6) {$q^6$};


    \node[state] at (2,0-10*\t) (q7) {$q^7$};
    \node[state, right of=q7] (q8) {$q^8$};
    \node[state] at (6,0-10*\t) (q9) {$q^9$};

    \node[state] at (1,2.5-10*\t) (q10) {$q^{10}$};
    \node[state, right of=q10] (q11) {$q^{11}$};
    \node[state, right of=q11] (q12) {$q^{12}$};
    \node[state, right of=q12] (q13) {$q^{13}$};

    \node[state,fill=red!20, densely dashed] at (2,-1.25-10*\t) (q14) {$q^{14}$};
    \node[state,fill=red!20, densely dashed] at (4,-1.25-10*\t) (q15) {$q^{15}$};
    \node[state,fill=red!20, densely dashed] at (2,1.25-10*\t) (q16) {$q^{16}$};
    \node[state,fill=red!20, densely dashed] at (6,1.25-10*\t) (q17) {$q^{17}$};

    \draw 

   (q1) edge[bend right] node [below left]{$e^1$} (q2)
   (q2) edge node [below] {$e^2$} (q3)
   (q3) edge node [below]{$e^3$} (q4)
   (q4) edge node [below]{$e^4$} (q5)
   (q5) edge[bend right] node [below right] {$e^5$} (q6)

   (q1) edge node [above]{$e^6$} (q7)
   (q7) edge node [above]{$e^7$} (q8)
   (q8) edge node [above]{$e^8$} (q9)
   (q9) edge node [above]{$e^9$} (q6)

   (q1) edge[bend left] node [below left]{$e^{10}$} (q10)
   (q10) edge node [above]{$e^{11}$} (q11)
   (q11) edge node [above]{$e^{12}$} (q12)
   (q12) edge node [above]{$e^{13}$} (q13)
   (q13) edge[bend left] node [above right]{$e^{14}$} (q6)

   (q2) edge[bend right, densely dashed] node [above left]{$e^{15}$} (q14)
   (q14) edge[bend right, densely dashed] node [ right]{$e^{16}$} (q7)
   (q4) edge[bend right, densely dashed] node [ right]{$e^{17}$} (q15)
   (q15) edge[bend right, densely dashed] node [ right]{$e^{18}$} (q9)
   (q12) edge[bend left=10, densely dashed] node [ above]{$e^{19}$} (q16)
   (q16) edge[bend left=10, densely dashed] node [ above right]{$e^{20}$} (q9)
   (q12) edge[bend left=10, densely dashed] node [ above right]{$e^{21}$} (q17)
   (q17) edge[bend left, densely dashed] node [ above right]{$e^{22}$} (q9);
   }
\end{tikzpicture}

\end{document}

Mostafa
  • 57
  • You load the animate package, so maybe you can use this (see an example here: https://tex.stackexchange.com/a/660779/47927). I have to admit that I never used the animations library provided by TikZ so far. – Jasper Habicht May 25 '23 at 16:21
  • 1
    You need to produce SVG, other output is not supported for animations by TikZ. The library provides support for snapshots but that's best used with standalone to produce just a PDF of the picture and not a full document if you want to create a gif. – Qrrbrbirlbel May 25 '23 at 19:08

0 Answers0