# Strange behavior with TikZ animation

I was trying to create an animated TaiChi symbol with TikZ and the animate package, and I encountered the following problem:

If I don't add a bigger bounding box to the tikzpicture, then some frames in the resulting animation will "shrink".

The following MWE illustrates the problem:

\documentclass{article}
\usepackage{tikz}
\usepackage{animate}

\begin{document}
\centering

This is wrong.\\
\newcommand\TaiChiWrong[1]{
\begin{tikzpicture}[scale=3]
\fill[white](#1+180:1)arc(#1+180:#1:1)--cycle;  % black half-circle
\fill(#1:1)arc(#1:#1-180:1)--cycle;             % white half-circle
\draw[red](-1,-1)rectangle(1,1);
% \draw[blue](-1.3,-1.3)rectangle(1.3,1.3);
\end{tikzpicture}
}
\begin{animateinline}[loop]{10}
\multiframe{36}{i=90+-10}{\TaiChiWrong{\i}}
\end{animateinline}

\bigskip

This is correct.\\
\newcommand\TaiChi[1]{
\begin{tikzpicture}[scale=3]
\fill[white](#1+180:1)arc(#1+180:#1:1)--cycle;  % black half-circle
\fill(#1:1)arc(#1:#1-180:1)--cycle;             % white half-circle
\draw[red](-1,-1)rectangle(1,1);
\draw[blue](-1.3,-1.3)rectangle(1.3,1.3);
\end{tikzpicture}
}
\begin{animateinline}[loop]{10}
\multiframe{36}{i=90+-10}{\TaiChi{\i}}
\end{animateinline}

\end{document}


The only difference between the wrong and correct versions is that in the latter, a bigger bounding box (the blue one) is drawn. Moreover, the bigger bounding box has to somehow be "big enough": for example, if the blue box is \draw[blue](-1.1,-1.1)rectangle(1.1,1.1); the problem still exists.

I suspect that this problem is not related to animate because the problem still exists when I use \foreach to create a series of images.

My question is: Why does a bigger bounding box make a difference?

TikZ automatically creates a bounding box to fit the contents. To override, you can specify

\draw[use as bounding box](-1.3,-1.3)rectangle(1.3,1.3);


This is probably necessary due to the automatically-created bounding rectangle of your sequence of frames varying due to the varying placement of the elements in the picture.

You might also want to clip to this rectangle.

Edit in response to comments below: In the current instance, the problem is that the TikZ bounding box of an arc is not actually the true bounding box of the arc. Compare

\begin{tikzpicture}
\draw[color=blue] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}

\begin{tikzpicture}
\draw[color=blue] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}


The reason for this (at a guess) is that the calculated bounding box includes the Bezier control points for the arc, which actually lie off the path.

I am not 100% familiar with the animate package, but it looks like it takes the bounding box of the first image as the "canvas" and then scales all subsequent images to fit. Since the automatic bounding box of the images is wrong (as above) this creates the observed behaviour.

So following my earlier suggestion, something like this does work (I've checked)

\newcommand\TaiChiFixed[1]{
\begin{tikzpicture}[scale=3]
\path[clip,use as bounding box](-1,-1)rectangle(1,1);
\fill[white](#1+180:1)arc(#1+180:#1:1)--cycle;  % black half-circle
\fill(#1:1)arc(#1:#1-180:1)--cycle;             % white half-circle

• Thanks! "the calculated bounding box includes the Bezier control points for the arc, which actually lie off the path" is exactly what I'm looking for :) Dec 2, 2013 at 4:38