Search code examples
animationlatexbeamer

Using LaTeX animate & Tikz with two nested loops


I'm trying to create an animation in LaTeX using the animate package with two nested loops, in which I draw graphics with Tikz. However, the picture keeps slightly rescaling for the first and last inner iteration. In the minimal example below, I want to draw two moving arrows: One at the top representing the outer layer and one at the bottom for the inner layer.

Notably, this problem vanishes if the inner layer has only one iteration, i.e. if the variable \jlimit is 1. This seems to suggest that something is going wrong on the inner layer.

Has anyone encountered this problem before or can you think of any solutions?

Thanks in advance :)

\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}

Solution

  • The problem are the unprotected line endings, which are interpreted by tex as spaces. Add % signs at the end of lines to avoid this problem:

    \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

    (the control keys are not visible in the image above due to the conversion into .gif, they work fine in the original pdf)