使用带有两个嵌套循环的LaTeX动画和Tikz

时间:2019-04-12 11:05:43

标签: animation latex beamer

我正在尝试使用带有两个嵌套循环的animate包在LaTeX中创建动画,在其中我使用Tikz绘制图形。但是,对于第一个和最后一个内部迭代,图片会保持略微缩放。在下面的最小示例中,我想绘制两个移动的箭头:一个在顶部代表外层,另一个在底部代表内层。

值得注意的是,如果内层只有一个迭代,即变量\ jlimit为1,则此问题消失。这似乎表明内层出了问题。

有人有没有遇到过这个问题,或者您能想到什么解决方案?

先谢谢您了:)

\documentclass[10pt]{beamer}

\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}

\newcounter{i}    % Outer counter
\setcounter{i}{0}   
\newcounter{j}    % Inner counter
\def\ilimit{3}    % Outer iteration limit
\def\jlimit{5}    % Inner iteration limit, rescaling doesn't happen if this is 1

\begin{document}

\begin{frame}[fragile]{Nested animated loops}
\begin{center}

\begin{animateinline}[loop, poster = first, controls]{2}
    \whiledo{\thei<\ilimit} {       % Starting outer loop
        \setcounter{j}{0}           % Resetting inner counter
        \whiledo{\thej<\jlimit} {   % Starting inner loop

        \begin{tikzpicture}
            \draw [color=black] (-0.5,-1.5) rectangle (4.5, 0.5);   % Draw a bounding rectangle
            \node[shift={(\thei,0)}] at (0,0) {\Large $\downarrow$};% Draw the first level
            \node[shift={(\thej,0)}] at (0,-1) {\Large $\uparrow$}; % Draw the second level
        \end{tikzpicture}

        \stepcounter{j}                        % Increase the inner counter
        \ifthenelse{\thej<\jlimit} {
            \newframe                          % Draw a new inner frame
        }{}
    }

    \stepcounter{i}                            % Increase the outer counter
    \ifthenelse{\thei<\ilimit} {
        \newframe                              % Draw a new outer frame
    }{
            \end{animateinline}\relax % BREAK  % End the animation
    }
  }

\end{center}
\end{frame}

\end{document}

1 个答案:

答案 0 :(得分:0)

问题是未受保护的行尾,被tex解释为空格。在行的末尾添加%标志,以避免出现此问题:

\documentclass[10pt]{beamer}

\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}

\newcounter{i}    % Outer counter
\setcounter{i}{0}   
\newcounter{j}    % Inner counter
\def\ilimit{3}    % Outer iteration limit
\def\jlimit{5}    % Inner iteration limit, rescaling doesn't happen if this is 1

\begin{document}

\begin{frame}[fragile]{Nested animated loops}
\begin{center}%
\begin{animateinline}[loop, poster = first, controls]{2}
    \whiledo{\thei<\ilimit} {%       % Starting outer loop
        \setcounter{j}{0}%           % Resetting inner counter
        \whiledo{\thej<\jlimit} {%   % Starting inner loop
        \begin{tikzpicture}%
            \draw [color=black] (-0.5,-1.5) rectangle (4.5, 0.5);   % Draw a bounding rectangle
            \node[shift={(\thei,0)}] at (0,0) {\Large $\downarrow$};% Draw the first level
            \node[shift={(\thej,0)}] at (0,-1) {\Large $\uparrow$}; % Draw the second level
        \end{tikzpicture}%
        \stepcounter{j}%                        % Increase the inner counter
        \ifthenelse{\thej<\jlimit} {%
            \newframe%                          % Draw a new inner frame
        }{}%
    }%
    \stepcounter{i}%                            % Increase the outer counter
    \ifthenelse{\thei<\ilimit} {%
        \newframe%                              % Draw a new outer frame
    }{%
            \end{animateinline}\relax% % BREAK  % End the animation
    }%
  }%
\end{center}
\end{frame}

\end{document}

enter image description here

(由于转换为.gif,控制键在上图中不可见,它们在原始pdf中工作正常)