举个例子,这动画。如何将其转换为 GIF 文件?
答案1
假设您想要创建如下动画。
步骤1:
创建一个至少包含 2 页的 PDF 文件。使用latex-dvips-ps2pdf
或编译以下输入文件xelatex
将生成一个包含 30 页的 PDF 文件。每页代表一个框架。
% 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}
第2步:
安装图像魔术师。
步骤3:
安装Ghostscript。没有它,您无法使用 ImageMagick 栅格化矢量文件(PDF、EPS、PS 等)。
步骤4:
使用以下命令将 PDF 转换为 GIF:
convert -delay <integer> -loop 0 -density <integer> -alpha <on,off,remove> input.pdf output.gif
评论:
-delay <integer>
指定两个连续帧之间的延迟(以 1/100 秒为单位)。-density <integer>
指定分辨率。-alpha <on,off,remove>
指定是否启用、禁用或删除 alpha 通道。
另一个例子:
\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}
使用convert -delay 4 -loop 0 -density 200 -alpha remove shm.pdf shm.gif
。-delay 4
代表每秒25帧的速率。 公式为FPS = 100 / DELAY
。
答案2
如果您需要 PDF 动画,请使用以下模板。
% 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]{<frame rate>}{temporary}{}{}
\end{document}
例如,
% 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}
并用 进行编译pdflatex -shell-escape main
。