在 beamer 中使用 tikz 制作 foreach 循环动画

在 beamer 中使用 tikz 制作 foreach 循环动画

我目前正在尝试以动画形式展示卷积滤波器(以及二维数组上的卷积运算)的工作方式。为了做到这一点,我从这项工作中得到了启发:https://github.com/vdumoulin/conv_arithmetic 我已经使用 tikz 完成了大部分工作,但我不知道如何结合 tikz 插图使用动画包来制作动画。

我的代码在那里(请注意,此时,我只执行 x 在 0 到 5 范围内的 foreach 循环):

\documentclass[hyperref={pdfpagemode=FullScreen},xcolor=table,t, compress]{beamer}

\useoutertheme[subsection=false, compress]{miniframes}

\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows, arrows.meta, shapes.geometric}
\usepackage{pgfplots}

\usetikzlibrary{fit}
\makeatletter
\tikzset{
  fitting node/.style={
    inner sep=0pt,
    fill=none,
    draw=none,
    reset transform,
    fit={(\pgf@pathminx,\pgf@pathminy) (\pgf@pathmaxx,\pgf@pathmaxy)}
  },
  reset transform/.code={\pgftransformreset}
}
\makeatother

\begin{document}
\begin{tikzpicture}
%Input
\draw[black, fill=blue!10, xstep=1, ystep=1, yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25] (1,1) grid (6,6) rectangle (1,1);  
\draw[yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25, black, thin, dashed, xstep=1, ystep=1] (0,0) grid (7,7);
\foreach \x in {0,1,...,5} {
%Filter
\draw[yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.4] (\x,0) rectangle (3,3) node[fitting node] (filtering) {};
%Init result position
\draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.4] (0,0) rectangle (1,1) node[fitting node] (result) {};

\foreach \coord in {north, south, west, east} {
    \draw (result.\coord) -- (filtering.\coord);
}

%Feature map
\draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, black, fill=blue!30, xstep=1, ystep=1] (0,0) grid (5,5) rectangle (0,0);
\draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.5] (\x,0) rectangle (1,1) node[fitting node] (result) {};
}
\end{tikzpicture}
\end{document}

回答后的更新如下mxnc baud

\begin{tikzpicture}
    %Input
    \draw[black, fill=blue!10, xstep=1, ystep=1, yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25] (1,1) grid (6,6) rectangle (1,1);  
    \draw[yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25, black, thin, dashed, xstep=1, ystep=1] (0,0) grid (7,7);
    \begin{animateinline}[poster=first, controls=true, autoplay=false]{4}
        \multiframe{5}{npos=0+1}{%
            \tikz{%
                \useasboundingbox (0,0)rectangle(10,10);
                %Filter
                \draw[yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.4] (\npos,0) rectangle (3,3) node[fitting node] (filtering) {};
                %Init result position
                \draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.4] (0,0) rectangle (1,1) node[fitting node] (result) {};
                \foreach \coord in {north, south, west, east} {%
                    \draw (result.\coord) -- (filtering.\coord);
                }
                %Feature map
                \draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, black, fill=blue!30, xstep=1, ystep=1] (0,0) grid (5,5) rectangle (0,0);
                \draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.5] (\npos,0) rectangle (1,1) node[fitting node] (result) {};
            }
        }
    \end{animateinline}
\end{tikzpicture}

这是我遇到的错误:

! TeX capacity exceeded, sorry [grouping levels=255].

答案1

您读过动画文档吗?

下面是一个在动画环境中包含 tikz 图片的简单示例:

\usepackage{animate}
\usepackage{multido}
\usepackage{tikz}
\begin{document}
    \begin{animateinline}[poster=first,controls]{10}
        \multiframe{11}{npos=0+1}{%
        \tikz{%
            \useasboundingbox (-1,-1)rectangle(11,1);
            \draw(0,0)--(\npos,0)node[above]{$A$};
            }
        }
    \end{animateinline}
\end{document}

在此处输入图片描述

现在应该可以工作了:

\documentclass[hyperref={pdfpagemode=FullScreen},xcolor=table,t, compress]{beamer}

\useoutertheme[subsection=false, compress]{miniframes}

\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows, arrows.meta, shapes.geometric}
\usepackage{pgfplots}

\usetikzlibrary{fit}
\makeatletter
\tikzset{
  fitting node/.style={
    inner sep=0pt,
    fill=none,
    draw=none,
    reset transform,
    fit={(\pgf@pathminx,\pgf@pathminy) (\pgf@pathmaxx,\pgf@pathmaxy)}
  },
  reset transform/.code={\pgftransformreset}
}
\makeatother
\usepackage{animate}
\usepackage{multido}
\begin{document}
\begin{animateinline}[poster=first, controls=true, autoplay=false]{4}
\multiframe{5}{npos=0+1}{%
\begin{tikzpicture}
    %Input
    \draw[black, fill=blue!10, xstep=1, ystep=1, yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25] (1,1) grid (6,6) rectangle (1,1);  
    \draw[yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25, black, thin, dashed, xstep=1, ystep=1] (0,0) grid (7,7);
                %Filter
\draw[yshift=-10, xshift=30,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.4] (\npos,0) rectangle (3,3) node[fitting node] (filtering) {};
                %Init result position
\draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.4] (0,0) rectangle (1,1) node[fitting node] (result) {};
\foreach \coord in {north, south, west, east} {\draw (result.\coord) -- (filtering.\coord);}
                %Feature map
\draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, black, fill=blue!30, xstep=1, ystep=1] (0,0) grid (5,5) rectangle (0,0);
\draw[yshift=35, xshift=40,yslant=-0.5,xslant=1, scale=0.25, fill=black!50, opacity=0.5] (\npos,0) rectangle (1,1) node[fitting node] (result) {};
\end{tikzpicture}
            }
\end{animateinline}
\end{document}

相关内容