您的位置:首页 > 其它

[Latex]beamer中实现tikz的动画绘图的示例

2013-04-02 21:43 477 查看
在beamer中经常使用tikz绘图,有时想把图形的绘制过程动态地展现出来,就更好了.

下面是个例子:

% Animated beamer frame for tikz
%Author:
%wu xuping
%Date:
%2013-04-02
% FileName:
%main.tex
% run:
%  xelatex -shell-escape main
%  xelatex -shell-escape main
%required:
%  Acrobat Reader version > 6

\documentclass{beamer}
\usepackage{verbatim}

\usepackage{tikz}
\usepackage[autoplay,loop]{animate}

\begin{document}

\begin{frame}
Animated beamer frame for tikz

\begin{center}
\begin{animateinline}[
begin={
\begin{tikzpicture}[blue,scale=1,line width=2pt]
\useasboundingbox[draw] (0,0)rectangle(4,4);
},
end={
\end{tikzpicture}
}
]{2}

\draw[->](0,0)--(1,1);\newframe
\draw[->](0,0)--(2,2);\newframe
\draw[->](0,0)--(3,3);\newframe
\draw[->](0,0)--(4,4);
\end{animateinline}
\end{center}

\end{frame}

\end{document}

编译上面的例子,打开生成的pdf文件就可以发现tikz的动态绘图过程是如何实现的了.

如下图中的带箭头的线段是如何动态生成的?



再看一个tikz动画(或动态)绘制正弦函数的例子:

\documentclass{beamer}
\usepackage{verbatim}

\usepackage{ctex}
\usepackage{tikz}
\usepackage{animate}

\newcommand{\plotsin}[1]{%
\draw[red] plot[domain=-3:#1]
(\x,{sin(\x r)});
}

\begin{document}

\begin{frame}
绘制正弦函数的动画:\\
\begin{animateinline}[autoplay,palindrome,
begin={\begin{tikzpicture}[scale=1,line width=1.5pt]
\draw[line width=0.5pt,step=0.5,dashed,use as bounding box] (-4,-1.5)grid(4,1.5);
\draw[->] (-4,0)--(4,0) node[below left]{$x$};
\draw[->] (0,-1.5)--(0,1.5)node[below left]{$y$}; },
end={\end{tikzpicture}}]{4}
\plotsin{0};\newframe
\plotsin{1};\newframe
\plotsin{1.5};\newframe
\plotsin{2};\newframe
\plotsin{2.5};\newframe
\plotsin{3};\newframe
\plotsin{3.5};\newframe
\plotsin{4};
\end{animateinline}

\end{frame}

\end{document}


绘图如下所示:



设置两个参数画正弦函数,总共生成20帧画面,已每秒10帧的速度播放:

\documentclass{beamer}
\usepackage{verbatim}

\usepackage{tikz}
\usepackage{calc}
\usepackage{animate}

\newcommand{\plotsin}[2]{%
\draw[red,<->] plot[domain=#1:#2]
(\x,{sin(\x r)});
}

\begin{document}

\begin{frame}
绘制正弦函数的动画:\\
\begin{animateinline}[autoplay,palindrome,
begin={\begin{tikzpicture}[scale=1,line width=1.5pt]
\draw[line width=0.5pt,step=0.5,dashed,use as bounding box] (-4,-1.5)grid(4,1.5);
\draw[->,blue] (-4,0)--(4,0) node[below left]{$x$};
\draw[->,blue]  (0,-1.5)--(0,1.5)node[below left]{$y$}; },
end={\end{tikzpicture}}]{10}
\multiframe{20}{Rxa=-0.2+-0.2, Rxb=0.2+0.2}{%
%Rxa=-0.2,-0.4,...,-4.0
%Rxb=+0.2,+0.4,...,+4.0
\plotsin{\Rxa}{\Rxb}%
}%
\end{animateinline}

\end{frame}

\end{document}


效果如下:



最关键的宏包

\usepackage[autoplay,loop]{animate}
里面提供的命令和环境

\begin{animateinline}
<code text>
\end{animateinline}
具体参见手册吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: