Graphics, Figures & Tablesopacity in TikZ with animate package

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
hendrick
Posts: 8
Joined: Mon Sep 07, 2009 3:58 pm

opacity in TikZ with animate package

Post by hendrick »

I am creating an animation of TikZ graphics using the animate package, taking

http://www.texample.net/tikz/examples/s ... animation/

as a model. It is working fine, except that when I try to use opacity commands in TikZ within the animateinline environment, the entire TikZ picture shows up as blank in the animation. I am using MikTeX and compiling with pdftex directly to pdf, which I am then viewing with Adobe Reader 9. Does anyone have any suggestions as to what is wrong and how I might fix or work around it?

Here is a minimal working example of the problem. Thank you for any help you can give.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usepackage{animate}

\begin{document}
  
Opacity:\\
\begin{tikzpicture}
    \draw (-2,-2) rectangle (4,2);
    \fill[cyan] (0,0) circle (2);
    \fill[magenta, opacity=0.5] (2,0) circle (2);
\end{tikzpicture}%

Animation:\\
\newcounter{time}
\begin{animateinline}[controls]{30}%
%
\setcounter{time}{0}%
\whiledo{\thetime<101}{%
%
    \begin{tikzpicture}
        \draw (-2,-2) rectangle (4,2);
        \fill[cyan] (0,0) circle (2);
        \fill[magenta] (2,0) circle ({2*\arabic{time}/100});
    \end{tikzpicture}%
    %
    \stepcounter{time}%
    \ifthenelse{\thetime<101}{%
            \newframe%
    }{%
            \end{animateinline}%
    }%
}%

Opacity with Animation:\\
\begin{animateinline}[controls]{30}%
%
\setcounter{time}{0}%
\whiledo{\thetime<101}{%
%
    \begin{tikzpicture}
        \draw (-2,-2) rectangle (4,2);
        \fill[cyan] (0,0) circle (2);
        \fill[magenta,opacity=0.5] (2,0) circle ({2*\arabic{time}/100});
    \end{tikzpicture}%
    %
    \stepcounter{time}%
    \ifthenelse{\thetime<101}{%
            \newframe%
    }{%
            \end{animateinline}%
    }%
}%

Opacity with Animation, even simpler:\\
\begin{animateinline}[controls]{30}%
    \begin{tikzpicture}
        \draw (-2,-2) rectangle (4,2);
        \fill[cyan] (0,0) circle (2);
        \fill[magenta,opacity=0.5] (2,0) circle ({2*\arabic{time}/100});
    \end{tikzpicture}%
\end{animateinline}%

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

alex
Posts: 32
Joined: Mon May 19, 2008 10:53 am

opacity in TikZ with animate package

Post by alex »

I must admit that I have never tested animate with opacity. Interestingly, opacity works if you use the dvipdfmx and the xetex output drivers (although the colours are somewhat different from the static images). See this modified example and run latex+dvipdfmx on it:

Code: Select all

\documentclass{minimal}

%dvipdfmx
\usepackage[dvipdfmx]{animate}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}

%pdftex or latex+dvips
%\usepackage{animate}

\usepackage{tikz}

\begin{document}

Opacity without Animation:\\
\begin{tikzpicture}
    \draw (-2,-2) rectangle (4,2);
    \fill[cyan] (0,0) circle (2);
    \fill[magenta,opacity=0.5] (2,0) circle ({2});
\end{tikzpicture}%

Opacity with Animation:\\
\begin{animateinline}[controls]{30}%
\multiframe{101}{i=0+1}{%
    \begin{tikzpicture}
        \draw (-2,-2) rectangle (4,2);
        \fill[cyan] (0,0) circle (2);
        \fill[magenta,opacity=0.5] (2,0) circle ({2*\i/100});
    \end{tikzpicture}%
}%
\end{animateinline}%

\end{document}
I will have to do further tests with the pdftex and dvips output drivers to see what is going on here. Maybe it is the way opacity is implemented in TikZ which causes the problem.

Alexander

BTW: I have replaced the \whiledo command with the \multiframe command which is implemented in animate.sty. It is much easier to be used and less prone to error.
alex
Posts: 32
Joined: Mon May 19, 2008 10:53 am

opacity in TikZ with animate package

Post by alex »

It is a bug in pdftex. I have already submitted a bug report.

As a workaround, you could first generate the frames as a separate multipaged PDF file (source `frames.tex' below) and then animate it with the \animategraphics command in your main document:

Code: Select all

\animategraphics[controls]{30}{frames}{}{}
Alexander


File frames.tex. Run pdflatex on it to generate frames.pdf:

Code: Select all

\documentclass{minimal}
\usepackage[active,tightpage]{preview}
\usepackage{multido}

\usepackage{tikz}

\begin{document}

\multido{\i=0+1}{101}{%
  \begin{preview}%
    \begin{tikzpicture}
        \draw (-2,-2) rectangle (4,2);
        \fill[cyan] (0,0) circle (2);
        \fill[magenta,opacity=0.5] (2,0) circle ({2*\i/100});
    \end{tikzpicture}%
  \end{preview}
}%

\end{document}
alex
Posts: 32
Joined: Mon May 19, 2008 10:53 am

opacity in TikZ with animate package

Post by alex »

I uploaded a new version of animate to CTAN which can be fetched as

http://tug.ctan.org/tex-archive/install ... te.tds.zip

It resolves the issue you reported. TikZ pictures with transparent objects defined in the `animateinline' environment no longer lead to invalid PDF. Pattern fills were affected too, but can now be used as well.

Alexander
hendrick
Posts: 8
Joined: Mon Sep 07, 2009 3:58 pm

Re: opacity in TikZ with animate package

Post by hendrick »

Thank you very much! I'm glad I was able to help you find a bug in pdftex, and I'm very pleased you were able to alter the animate package to resolve the issue. This will help tremendously with some classroom demonstrations I am writing.

Sincerely,
Anders Hendrickson
Post Reply