如何在 animateinline 中循环遍历 tikzpicture 列表?

如何在 animateinline 中循环遍历 tikzpicture 列表?

我有以下代码,它使用了animateinline一些tikzpicture动画效果:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}
\usetheme{Warsaw}
\usecolortheme{whale}

\def\layera{
  \path[cm={{0.8028232,-0.59621716,0.59621716,0.8028232,(0.0,0.0)}},draw=black,opacity=0.990,miter
    limit=4.00,line width=0.943pt,rounded corners=0.0000cm] (-99.8372,584.6912)
    rectangle (-30.7483,653.7801);
}

\def\layerb{
    \fill[blue!40] (0,0) circle (1);
}

\def\layerc{
    \fill[red!40] (0,0) circle (1);
}

\begin{document}
\begin{center}
\begin{frame}
\begin{animateinline}[loop,controls]{1}

\begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
    \layera{}
\end{tikzpicture}

\newframe
\begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
    \layerb{}
\end{tikzpicture}

\newframe
\begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
    \layerc{}
\end{tikzpicture}

\newcommand{\makelayer}{
    \newframe
    \begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
        \layerc{}
    \end{tikzpicture}
}

%\makelayer{}

%\def\MyLayers{\layerb, \layerc}
%\foreach \mylayer in \MyLayers {
%    \newframe
%    \begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
%        \mylayer{}
%    \end{tikzpicture}
%}

\end{animateinline}
\end{frame}
\end{center}
\end{document}

如您所见,它有 3 个层(1 个层/帧):layera、layerb 和 layerc。以下文件可以转换为 pdf,并且动画效果正确:一个正方形,然后是一个蓝色圆圈和一个红色圆圈。

但是,当我尝试使用\newcommandmakelayer 时,输出不正确:只需取消注释\makelayer{}并编译为 pdf。奇怪的是 layerb 没有正确显示,而是在里面的 layerc \makelayer{}

\newcommand对于训练有素的眼睛来说,这一定是显而易见的事情;有人能指出在这种情况下正确的使用方法是什么吗?

第二个问题是循环遍历所有 tikzpictures,我认为这可能与上面的问题有关。我还注释掉了不起作用的代码,从以下位置开始:%\def\MyLayers{\layerb, \layerc}

任何帮助深表感谢。

編輯1

@Harish 的回答解决了第一个问题。

为了澄清我的第二个问题,即如何tikzpicture使用foreach或任何其他循环机制循环遍历每个内容?我对代码做了一些更改来说明这个问题(实际的 tex 文件可能包含更多层/ tikzpicture):

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}
\usetheme{Warsaw}
\usecolortheme{whale}

\newcommand\layera{%
\begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
  \path[cm={{0.8028232,-0.59621716,0.59621716,0.8028232,(0.0,0.0)}},draw=black,opacity=0.990,miter
    limit=4.00,line width=0.943pt,rounded corners=0.0000cm] (-99.8372,584.6912)
    rectangle (-30.7483,653.7801);
  \end{tikzpicture}
}

\newcommand\layerb{%
    \fill[blue!40] (0,0) circle (1);
}

\newcommand\layerc{%
    \fill[red!40] (0,0) circle (1);
}

\begin{document}
\begin{center}
\begin{frame}
\begin{animateinline}[loop,controls]{1}
    \layera

\newcommand{\makemylayer}{%
    \newframe
    \begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
        \layerb
    \end{tikzpicture}

    \newframe
    \begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]   
        \layerc
    \end{tikzpicture}
}

\makemylayer

%\newcommand\MyLayers{\layerb, \layerc}
%\foreach \mylayer in \MyLayers {
%    \newframe
%    \begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
%        \mylayer{}
%    \end{tikzpicture}
%}


\end{animateinline}
\end{frame}
\end{center}
\end{document}

有 3 层(tikzpicture):layera、layerb 和 layerc,除了 layera,我想循环遍历每层tikzpicture(layerb 和 layerc)的内容。

因此,当我注释掉\makemylayer、取消注释该foreach块时,当我尝试编译时,出现以下错误消息:

! Package animate Error: Contents of first frame must not have zero width.

感谢您的帮助。

編輯2

loop我尝试过其他循环方法,例如forloop等,但大多数都涉及计数器或类似的东西;有人能否就如何遍历上述命令列表并使其工作提供任何建议?

非常感谢。

答案1

%通过在后面添加一个 即可解决您的第一个问题\newcommand{\makelayer}{

我觉得我没有正确理解你的第二个问题。可以按照你修改后的代码所示进行操作。

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}
\usetheme{Warsaw}
\usecolortheme{whale}

\newcommand\layera{%
\begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
  \path[cm={{0.8028232,-0.59621716,0.59621716,0.8028232,(0.0,0.0)}},draw=black,opacity=0.990,miter
    limit=4.00,line width=0.943pt,rounded corners=0.0000cm] (-99.8372,584.6912)
    rectangle (-30.7483,653.7801);
  \end{tikzpicture}
}

\newcommand\layerb{%
\begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
    \fill[blue!40] (0,0) circle (1);
\end{tikzpicture}
}

\newcommand\layerc{%
\begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
    \fill[red!40] (0,0) circle (1);
\end{tikzpicture}
}

\begin{document}
\begin{center}
\begin{frame}
\begin{animateinline}[loop,controls]{1}
    \layera
\newframe
    \layerb
\newframe
    \layerc

\newcommand{\makelayer}{%
    \newframe
        \layerb
}

\makelayer

%\def\MyLayers{\layerb, \layerc}
%\foreach \mylayer in \MyLayers {
%    \newframe
%    \begin{tikzpicture}[y=0.20pt,x=0.20pt,yscale=-1, inner sep=0pt, outer sep=0pt]
%        \mylayer{}
%    \end{tikzpicture}
%}

%% Instead of above try this:

\newcommand{\makemylayer}{%
    \newframe
        \layera
    \newframe
        \layerc
    \newframe
         \layerb
}

\makemylayer

\end{animateinline}
\end{frame}
\end{center}
\end{document}

我改变了出现的顺序以使事情变得更清楚。

相关内容