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

33 篇文章 3 订阅
17 篇文章 0 订阅

在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}
具体参见手册吧

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值