How to Make Animated Math GIFs: LaTeX + TikZ

The first animated GIF that I ever made was made with the LaTeX package TikZ and the command line utility ImageMagick. In this post, I’ll give a quick example of how to make a simple GIF that works by layering images with transparent backgrounds on top of each other repeatedly.

TikZ code

In our first step toward making the above GIF, we’ll make a PDF where each frame contains one of the above circles on a transparent background. In this case, each circle is placed uniformly at random with its center in a \(10 \times 10\) box, with a radius in \([0,1]\), and whose color gets progressively more red and less green with a random amount of blue at each step.

\documentclass[tikz]{standalone}
\usepackage{tikz}

\begin{document}
\foreach \red[evaluate=\red as \green using {255 - \red}] in {0,1,...,255} {
  \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle (10,10);
    \pgfmathrandominteger{\blue}{0}{255}
    \definecolor{myColor}{RGB}{\red,\green,\blue}
    \fill[myColor] ({10*random()},{10*random()}) circle ({1+random()});
  \end{tikzpicture}
}
\end{document}

Starting with \documentclass[tikz]{standalone} says to make each tikzpicture its own page in the resulting PDF.

Next we loop through values of \red from 0 to 255, each time setting \green to be equal to 255 - \red so that with each step the amount of red goes up and the amount of green goes down.

The command \useasboundingbox (0,0) rectangle (10,10); gives each frame a \(10 \times 10\) bounding box so that all of the frames are the same size and positioned the same way.

The command \pgfmathrandominteger{\blue}{0}{255} chooses a random blue values between 0 and 255.

The command \fill[myColor] ({10*random()},{10*random()}) circle ({1+random()}); places a circle with its center randomly chosen in the \(10 \times 10\) box and with a radius between \(1\) and \(2\).

ImageMagick

When we compile this code, we get a PDF with one circle on each page. In order do turn this PDF into an animated GIF, we convert the PDF using ImageMagick, a powerful command-line utility for handling images. If we named our PDF bubbles.pdf then running the following command in the terminal (in the same directory as bubbles.pdf) will give us an animated GIF called bubbles.gif.

convert -density 300 -delay 8 -loop 0 -background white bubbles.pdf bubbles.gif

I hope that you use this blog post as a jumping off point for making animated GIFs of your own! If you do, please reach out to share them with me!


Comments

2 responses to “How to Make Animated Math GIFs: LaTeX + TikZ”

  1. Cyprien KANYARWANDA Avatar
    Cyprien KANYARWANDA

    Thank you for this article. I am facing the problem where when I try to make animations I get many pages each with a single image. But I don’t know where to put this command

    convert -density 300 -delay 8 -loop 0 -background white bubbles.pdf bubbles.gif

    Your help will be highly appreciated.

    1. Hi Cyprien,

      I’m using MacOS, and I put that command in the terminal. In order to do this, you need to make sure that you have ImageMagick installed, and then you need to `cd` (change directory) into the directory where the PDF lives. For me, that’s something like `cd ~/Programming/MathArt/sandbox/` (without the backticks).

      Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *