Wednesday 12 November 2014

Making GIF animated figures using LaTeX

How to convert pstricks animation to GIF file? up vote 20 down vote favorite 12 As an example, let's take this animation. How can I convert it to GIF file? pstricks conversion animations gif shareimprove this question edited Jan 7 '13 at 8:34 Peter Jansson 3,47952036 asked Sep 21 '12 at 17:28 Physicsworks 474315 1 Related: tex.stackexchange.com/q/23727/2975 – Martin Scharrer♦ Sep 21 '12 at 18:05 add a comment 2 Answers active oldest votes up vote 26 down vote accepted Assume that you want to create an animation as follows. enter image description here Step 1: Create a PDF file consists of at lease 2 pages. Compiling the following input file with either latex-dvips-ps2pdf or xelatex produces a PDF file with 30 pages. Each page represent one frame. % input.tex \documentclass[pstricks,border=0pt]{standalone} \usepackage{pstricks-add,fp} \FPeval{Size}{3.00} \FPeval{N}{30} \FPeval{Delta}{round(Size/N:2)} \begin{document} \multido{\r=\Size+-\Delta}{\N} { \begin{pspicture}(-\Size,-\Size)(\Size,\Size) \psframe*[linecolor=yellow](-\Size,-\Size)(\Size,\Size) \pscircle*{\r} \end{pspicture} } \end{document} enter image description here Step 2: Install ImageMagick. Step 3: Convert the PDF to GIF using the following command: convert -delay -loop 0 -density -alpha input.pdf output.gif Remarks: -delay specifies the delay between 2 consecutive frames in 1/100 second. -density specifies the resolution. -alpha specifies whether or not you enable, disable or remove the alpha channel. enter image description here Another example: enter image description here \documentclass[border=0pt,pstricks]{standalone} \usepackage{pst-coil,pstricks-add} \usepackage[nomessages]{fp} \newcommand\const[3][6]{% \expandafter\FPeval\csname#2\endcsname{round(#3:#1)}% \pstVerb{/#2 \csname#2\endcsname\space def}% } \newcommand\Const[3][7]{\begingroup\edef\temp{\endgroup\noexpand\const[#1]{#2}{#3}}\temp} \Const{CoilArm}{0.250} \Const{CoilWidth}{0.300} \Const{CoilTurn}{50/3} \Const{DeltaY}{0.5} \Const{Amplitude}{1.5} \Const{FPS}{25}% frame rate must be synced with the convert's delay! \Const{Vx}{2}% propagation speed \Const{Period}{1}% second \def\Y#1{-\DeltaY-\Amplitude*cos(2*\psPi*#1/\Period)+2} \psset { coilarm=\CoilArm, coilwidth=\CoilWidth, } \newcommand\System[4][0]{% #1: box, #2: x, #3: y, #4: label \uput[90](#2,4.25){#4} \Const{CoilHeight}{(4-(#3)-2*CoilArm)/(CoilWidth*CoilTurn)} \pszigzag[coilheight=\CoilHeight,linejoin=2](#2,4)(#2,#3) \ifnum#1=1 \bgroup \psset{origin={#2,#3}} \psframe[dimen=inner,fillstyle=solid,fillcolor=black](-0.5,0)(0.5,-1) \psdot[linecolor=yellow](0,-0.5) \egroup \fi } \Const{DeltaTime}{1/\FPS} \Const[0]{TotalFrames}{\FPS*\Period} \begin{document} \multido{\n=0.000+\DeltaTime}{\TotalFrames}{% \begin{pspicture*}[showgrid=false](-1.5,-2)(3.5,5) \psframe[fillstyle=vlines,hatchsep=2pt,hatchwidth=0.5\pslinewidth,hatchcolor=gray,hatchangle=45](0,4)(2,4.25) \Const{Yn}{\Y{\n}}% \System[1]{1}{\Yn}{} \psplot[algebraic,linecolor=red,plotpoints=1000]{-1.5}{3.5}{\Y{((-\Vx*\n+x-1)/\Vx)}-0.5} \end{pspicture*}} \end{document} Use convert -delay 4 -loop 0 -density 200 -alpha remove shm.pdf shm.gif. The -delay 4 represents the rate of 25 frame per second. The formula is FPS = 100 / DELAY. shareimprove this answer edited Jan 7 '13 at 4:42 answered Sep 21 '12 at 17:47 Who is crazy first 8898265 1 OK, you could just say "use Image Magick" =) Thanks! – Physicsworks Sep 21 '12 at 17:53 4 "use Image Magick" should be submitted as a comment rather than an answer that I actually want. – Who is crazy first Sep 22 '12 at 15:49 @AdorableCreature,follow your steps, I can't get animated GIF. I only get the first page picture of the 30 pages pdf file in my GIF file. How to solve it? Thanks. – Pig Cry Jan 7 '13 at 4:23 @PigCry: Make sure you had 30 pages in your PDF and you typied the convert command correctly. – Who is crazy first Jan 7 '13 at 4:44 @AdorableCreature: yes, I have 30 pages. The convert command is convert -delay 100 -loop 0 -density 200 -alpha on input.pdf output.gif. – Pig Cry Jan 7 '13 at 5:13 show 5 more comments up vote 8 down vote Just in case you need a PDF animation then use the following template. % this filename is main.tex % compile it with pdflatex -shell-escape main \documentclass[preview]{standalone} \usepackage{filecontents} \begin{filecontents*}{temporary.tex} % put you compilable PSTricks input file here! \end{filecontents*} \usepackage{animate} \immediate\write18{latex temporary} \immediate\write18{dvips temporary} \immediate\write18{ps2pdf temporary.ps} \begin{document} \animategraphics[controls,autoplay,loop,scale=1]{}{temporary}{}{} \end{document} For example, % this filename is main.tex % compile it with pdflatex -shell-escape main \documentclass[preview]{standalone} \usepackage{filecontents} \begin{filecontents*}{temporary.tex} \documentclass[border=0pt,pstricks]{standalone} \usepackage{pst-coil,pstricks-add} \usepackage[nomessages]{fp} \newcommand\const[3][6]{% \expandafter\FPeval\csname#2\endcsname{trunc(#3:#1)}% \pstVerb{/#2 \csname#2\endcsname\space def}% } \newcommand\Const[3][3]{\begingroup\edef\temp{\endgroup\noexpand\const[#1]{#2}{#3}}\temp} \Const{CoilArm}{0.250} \Const{CoilWidth}{0.300} \Const{CoilTurn}{50/3} \Const{DeltaY}{0.5} \Const{Amplitude}{1.5} \Const{FPS}{25} \Const{Vx}{2}% propagation speed \Const{Period}{1}% second \def\Y#1{-\DeltaY-\Amplitude*cos(2*\psPi*#1/\Period)+2} \psset { coilarm=\CoilArm, coilwidth=\CoilWidth, } \newcommand\System[4][0]{% #1: box, #2: x, #3: y, #4: label \uput[90](#2,4.25){#4} \Const{CoilHeight}{(4-(#3)-2*CoilArm)/(CoilWidth*CoilTurn)} \pszigzag[coilheight=\CoilHeight,linejoin=2](#2,4)(#2,#3) \ifnum#1=1 \bgroup \psset{origin={#2,#3}} \psframe[dimen=inner,fillstyle=solid,fillcolor=black](-0.5,0)(0.5,-1) \psdot[linecolor=yellow](0,-0.5) \egroup \fi } \Const[3]{DeltaTime}{1/\FPS} \Const[0]{TotalFrames}{\FPS*\Period} \begin{document} \multido{\n=0.000+\DeltaTime}{\TotalFrames}{% \begin{pspicture*}[showgrid=false](-1.5,-2)(3.5,5) \psframe[fillstyle=vlines,hatchsep=2pt,hatchwidth=0.5\pslinewidth,hatchcolor=gray,hatchangle=45](0,4)(2,4.25) \Const{Yn}{\Y{\n}}% \System[1]{1}{\Yn}{} \psplot[algebraic,linecolor=red,plotpoints=1000]{-1.5}{3.5}{\Y{((-\Vx*\n+x-1)/\Vx)}-0.5} \end{pspicture*}} \end{document} \end{filecontents*} \usepackage{animate} \immediate\write18{latex temporary} \immediate\write18{dvips temporary} \immediate\write18{ps2pdf temporary.ps} \begin{document} \animategraphics[controls,autoplay,loop,scale=1]{25}{temporary}{}{} \end{document} And compile it with pdflatex -shell-escape main. shareimprove this answer edited Oct 21 '13 at 9:07 community wiki 3 revs Marienplatz there is no need to load package fp when it is not really needed \documentclass[pstricks,border=0pt]{standalone} \usepackage{pstricks-add} \makeatletter \def\Size{3} \def\N{30} \def\Delta{\strip@pt\dimexpr \Size pt/\N} \makeatother [...] more animations are here: tug.org/PSTricks/main.cgi?file=Animation/gif/gif – Herbert Sep 21 '12 at 18:00 Thanks for this trick. I put \newcommand\const[3][3]{% \expandafter\FPeval\csname#2\endcsname{round(#3:#1)}% \pstVerb{/#2 \csname#2\endcsname\space def}% } in a package to make my project consistent. – Who is crazy first Sep 21 '12 at 18:06 looks like " why an easy solution when a complicated one is possible ..." ;-) – Herbert Sep 21 '12 at 18:13 add a comment

No comments:

Post a Comment