为 TikZ 图片制作动画

为 TikZ 图片制作动画

我想为下面的图片制作动画,使蓝色框的长度变得越来越小。使用overprint环境和\transduration{0.2}可以完成这项工作。但是,这需要复制和粘贴一段代码,每次都减少 x 坐标topRight_corner。我相信有合适的方法可以做到这一点……

PS:我用这个来说明当'delta t'趋近于0时的极限。


这是TikZ图片 enter image description here

下面是我为此编写的代码

\documentclass{beamer}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

%------ tikZ ------%
\usepackage{tikz}
\usetikzlibrary{positioning, arrows}
%------------------%

\begin{document}

\begin{frame}[c]

\begin{center}
\begin{tikzpicture}
\node[coordinate] at (0, 0) (bottomeLeft_corner) {};
\node[coordinate] at (2.50, 1.25) (topRight_corner) {};
\path[fill=blue!20] (bottomeLeft_corner) rectangle (topRight_corner);
\node[above left=0.625cm and 2cm of bottomeLeft_corner, coordinate] (start1) {};
\node[above left=0.625cm and 0cm of bottomeLeft_corner, coordinate] (end1) {};
\draw[semithick] (start1) -- (end1);
\node[above right=-0.625cm and 0cm of topRight_corner, coordinate] (start2) {};
\node[above right=0.625cm and 4.5cm of bottomeLeft_corner, coordinate] (end2) {};
\draw[->, >=stealth', semithick] (start2) -- (end2);
\draw[semithick, black!40] (end1) -- (start2);
\node[below=0cm of end2, xshift=0.25cm] {time}; 
\node[yshift=-0.25cm] at (bottomeLeft_corner) {$t$};
\end{tikzpicture}
\end{center}

\end{frame}

\end{document}

答案1

以下是 TikZ 解决方案,与 OP 一样,使用包animate

enter image description here

\documentclass{beamer}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

%------ tikZ ------%
\usepackage{tikz}
\usetikzlibrary{positioning, arrows}
%------------------%

\usepackage{animate}

\begin{document}

\begin{frame}[c]

\begin{center}
\pgfmathtruncatemacro\N{10}
\begin{animateinline}[controls,loop]{5} % 5 fps, same as 0.2 s transduration
  \multiframe{\N}{i=1+1}{
    \begin{tikzpicture}
      \pgfmathsetmacro\x{2.5*(\N-\i+1)/\N}
      \node[coordinate] at (0, 0) (bottomeLeft_corner) {};
      \node[coordinate] at (\x, 1.25) (topRight_corner) {};
      \path[fill=blue!20] (bottomeLeft_corner) rectangle (topRight_corner);
      \node[above left=0.625cm and 2cm of bottomeLeft_corner, coordinate] (start1) {};
      \node[above left=0.625cm and 0cm of bottomeLeft_corner, coordinate] (end1) {};
      \draw[semithick] (start1) -- (end1);
      \node[above right=-0.625cm and 0cm of topRight_corner, coordinate] (start2) {};
      \node[above right=0.625cm and 4.5cm of bottomeLeft_corner, coordinate] (end2) {};
      \draw[->, >=stealth', semithick] (start2) -- (end2);
      \draw[semithick, black!40] (end1) -- (start2);
      \node[below=0cm of end2, xshift=0.25cm] {time}; 
      \node[yshift=-0.25cm] at (bottomeLeft_corner) {$t$};
    \end{tikzpicture}
  }
\end{animateinline}
\end{center}

\end{frame}

\end{document}

答案2

为了节省更多幻灯片,我认为使用animategraphics更好。使用 编译以下内容pdflatex -shell-escape input.tex

% input.tex
\documentclass{beamer}
\usepackage{filecontents,animate,pgffor}

\begin{filecontents*}{limit.tex}
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{multido}

\begin{document}
\multido{\r=3.0+-0.1}{30}{%
\begin{pspicture}(-1,0)(6,2)
    \psline{->}(-1,1)(5,1)
    \pscircle*[linecolor=red](0,1){2pt}
    \psframe*[linecolor=cyan,opacity=0.5](0,0)(\r,2)
    \uput[-135](0,1){$t$}
    \uput[0](5,1){time}
\end{pspicture}}
\end{document}
\end{filecontents*}


\foreach \compiler/\ext in {latex/tex,dvips/dvi,ps2pdf/ps}
    {\immediate\write18{\compiler\space limit.\ext}}

\begin{document}
\begin{frame}{Animation}
    \animategraphics[autoplay,loop,controls,scale=1]{5}{limit}{}{}
\end{frame}
\end{document}

截图如下。

enter image description here

答案3

您可以\onslide<...>在 for 循环 ( \foreach) 内使用,如下所示:

enter image description here

\documentclass{beamer}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

%------ tikZ ------%
\usepackage{tikz}
\usetikzlibrary{positioning, arrows}
%------------------%

\begin{document}

\begin{frame}[c]

\begin{center}
\begin{tikzpicture}
\pgfmathtruncatemacro\N{10}
\foreach \k in {1,...,\N}%
{%
    \pgfmathsetmacro\x{2.5*(\N-\k+1)/\N}
    \onslide<\k>
    {
        \node[coordinate] at (0, 0) (bottomeLeft_corner) {};
        \node[coordinate] at (\x, 1.25) (topRight_corner) {};
        \path[fill=blue!20] (bottomeLeft_corner) rectangle (topRight_corner);
        \node[above left=0.625cm and 2cm of bottomeLeft_corner, coordinate] (start1) {};
        \node[above left=0.625cm and 0cm of bottomeLeft_corner, coordinate] (end1) {};
        \draw[semithick] (start1) -- (end1);
        \node[above right=-0.625cm and 0cm of topRight_corner, coordinate] (start2) {};
        \node[above right=0.625cm and 4.5cm of bottomeLeft_corner, coordinate] (end2) {};
        \draw[->, >=stealth', semithick] (start2) -- (end2);
        \draw[semithick, black!40] (end1) -- (start2);
        \node[below=0cm of end2, xshift=0.25cm] {time}; 
        \node[yshift=-0.25cm] at (bottomeLeft_corner) {$t$};
    }
}
\end{tikzpicture}
\end{center}

\end{frame}

\end{document}

相关内容