# How do we compile and visualize the animations of TikZ 3.1?

I copied and pasted the example from page 221 of manual 3.1 and tried to compile it with Texmaker.

\documentclass[dvisvgm]{standalone}
\usepackage{tikz}
\usetikzlibrary{animations}

\begin{document}

\tikz
\node :fill opacity = { 0s="1", 2s="0", begin on=click }
:rotate = { 0s="0", 2s="90", begin on=click }
[fill = blue!20, draw = blue, ultra thick, circle]
{Click me!};

\end{document}


The .pdf file result produced does is this one:

The result given as an example on the manual is this one:

I opened the .pdf file produced on Chrome, Opera and Firefox, I don't have any animation.

1. How should I compile the animations of TikZ 3.1?

• on the command line
• with Texmaker
• with TeXstudio
2. And how do we open these files?

• Is it possible to view them with a PDF reader?
• Is it necessary to use a web browser?
• You can't use the dvisvgm driver when compiling to PDF. I'm quite surprised that you even get a usable PDF out of that. Also you have to add \tikzset{make snapshot if necessary}, to force a snapshot for the PDF. – Henri Menke Jan 9 at 22:17
• @HenriMenke : Because the drawing specials in the DVI are still PostScript. dvisvgm is linked against Ghostscript's libgs.so library. – AlexG Jan 9 at 22:20
• @AlexG OP is not using dvisvgm but compiling to PDF. So you end up with dvisvgm specials in the PDF file which is likely to corrupt the file. – Henri Menke Jan 9 at 22:22
• @HenriMenke One can even embed EPS files with \includegraphics without any driver option. dvisvgm delegates PSFile special to libgs. – AlexG Jan 9 at 22:23
• dvisvgm specials are mostly animation-related and simply ignored during conversion to PDF. – AlexG Jan 9 at 22:26

Animations works only with the SVG format. So you should compile your example at first with latex (not pdflatex). Then you should run dvisvgm file which will show something like this in a command line

C:\Users\XXX\Documents\tests>dvisvgm test-utf8
pre-processing DVI file (format version 2)
processing page 1
graphic size: 66.670012pt x 51.11758pt (23.431829mm x 17.965775mm)
output written to test-utf8.svg
1 of 1 page converted in 1.64 seconds


You will then find a .svg-file in your folder which you can open in a browser or some other application that understand svg.

You can't see the animations in pdf, but you can take snapshots:

1. remove the dvisvgm option

2. then run this with pdflatex

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{animations}

\begin{document}
\foreach \t in {0.5, 1, 1.5, 2}
{
\tikz[make snapshot of=\t]
\node :fill opacity = { 0s="1", 2s="0", begin on=click }
:rotate = { 0s="0", 2s="90", begin on=click }
[fill = blue!20, draw = blue, ultra thick, circle]
{Click me!};}

\end{document}


• Is the dvisvgm command available on MikTeX? I don't see it on texmaker.... – AndréC Jan 9 at 18:09
• @UlrikeFischer +1 Worked for me. However resulting svg file worked on Google Chrome and Firefox, not MS Edge. – berkus Jan 9 at 18:18
• @AndréC sorry, typo. I meant "you can't see ...". – Ulrike Fischer Jan 9 at 18:29
• The snapshots are for a pdf. Don't mix up the two methods. – Ulrike Fischer Jan 9 at 18:41
• @berkus Soon, MS Edge will use the same rendering engine ("Blink") as Chrome. Everything's gonna be alright then ;-). – AlexG Jan 9 at 22:13

There are some dvisvgm command line options that are not mentioned in the TikZ manual, but which should generally be used.

# Font format: --font-format=woff

LaTeX input (compile with latex or lualatex --output-format=dvi):

\documentclass[varwidth]{standalone}
\usepackage{amsmath}

\begin{document}
Hello World!
\begin{equation*}
\int_0^1 2x dx = 1
\end{equation*}
\end{document}


Compile with:

latex texsx-469409-a
dvisvgm --zoom=-1 --exact --font-format=woff texsx-469409-a


The default font format that dvisvgm embeds in the SVG output is not correctly interpreted by most WEB browsers. Instead, browsers use some replacement font, e. g. Times. Also, mathematical symbols may be incorrectly displayed:

Therefore, always add option --font-format=woff!!! The default font format may change in future dvisvgm versions.

# Improved bounding box calculation: --exact

SVG-1.1, the current standard, is a single-page document format, primarily intended for creating vector graphics to be embedded in the HTML code of Web pages. Therefore, dvisvgm tries to calculate the tight Bounding Box around the content of the page, similar to what the standalone class is made for. One could also use the article class together with \pagestyle{empty} to achieve the same result without using standalone; dvisvgm tries to crop the page around the visibile content.

In case of pure-text documents, without using graphical objects that push the page borders outwards, the resulting bounding box of the output is determined by the glyph boxes from the tfm files of the used font. However, these tend to be smaller than the glyphs themselves. This may lead to cropped output where parts of the glyphs are invisible because they are outside of the document edges.

dvisvgm provides option --exact to calculate the document's bounding box in such a way that glyph outlines fall entirely within the final bounding box.

Alternatively, the border option of the standalone document class can be set with a non-negative length value in order to add space around the content. This requires dvisvgm to be called with option --bbox=papersize. [Comment by @Martin]

# Responsive SVG: --zoom=-1

Option --zoom=-1 produces "responsive SVG" that automatically scale to fill the available space. Such SVG don't have a fixed size. This is important for SVG to be embedded into a web page using HTML containers, such as <object> or <img>.

LaTeX input (compile with latex or lualatex --output-format=dvi):

\documentclass[dvisvgm]{standalone}
\usepackage{tikz}
\usetikzlibrary{animations}

\begin{document}

\tikz
\node :fill opacity = { 0s="0", 5s="1" }
:rotate = { 6s="0", 10s="360", repeats, restart=false}
[fill = blue!20, draw = blue, ultra thick, circle]
{Hello!};

\end{document}


Compile with:

latex texsx-469409
dvisvgm --zoom=-1 --exact --font-format=woff texsx-469409


HTML code for embedding:

<img src="https://agrahn.gitlab.io/svg/texsx-469409.svg" width="200"/>


<img src="https://agrahn.gitlab.io/svg/texsx-469409.svg" width="400"/>


<img src="https://agrahn.gitlab.io/svg/texsx-469409.svg"/>


• The borders set with the border option of standalone can be applied to the SVG by calling dvisvgm with option --bbox=papersize. It tells the program to evaluate the papersize specials present in the DVI file rather than computing a tight bounding box (which is the default). – Martin Jan 10 at 21:17
• @Martin I didn't try the obvious, but --bbox=dvi which didn't have the desired effect. Thank you for your comment! I edited the answer accordingly. (papersize isn't mentioned in the man page, version 2.3.5.) – AlexG Jan 11 at 9:10
• @God No, :(. I linked them from my GitLab page. I used the the allowed <img> tag. It does not permit script driven animations, but the SMIL-type animations, that can be created with the new TikZ release. – AlexG Jan 11 at 9:52
• @God Maybe, but my files are innocent ;) – AlexG Jan 11 at 9:55
• @AlexG Thanks for updating your answer. --bbox=papersize is available since version 1.16. I also updated the man page back then. So there should be some info about papersize specials in it. Perhaps you still have man page belonging to an older dvisvgm? The corresponding version number is mentioned at the bottom of the page. – Martin Jan 11 at 10:49

# Configuring Texstudio for dvisvgm

For texstudio a quick way to compile a svg animation are magic comments, i.e. the first line in the following file

% !TeX TS-program = latex % | latex % | dvisvgm --exact --font-format=woff --zoom=-1 %.dvi
\documentclass[dvisvgm]{standalone}
\usepackage{tikz}
\usetikzlibrary{animations}

\begin{document}

\tikz
\node :fill opacity = { 0s="1", 2s="0", begin on=click }
:rotate = { 0s="0", 2s="90", begin on=click }
[fill = blue!20, draw = blue, ultra thick, circle]
{Click me!};

\end{document}


This will run latex two times and afterwards convert it animated svg

### Create a custom user command

Another approach is to create a custom user command. In the texstudio preferences->build add the line

 latex % | latex % | dvisvgm --exact --font-format=woff --zoom=-1 %.dvi


under "user commands":

Then one can find this open in the menu bar Tools->user->svg (or whatever name you chose in the previous step)

(In case dvisvgm is not in your path variable, replace dvisvgm in the above commands with the full path to its installation location)

# Arara rule for dvisvgm

With the following rule for the cool automation tool arara

!config
identifier: dvisvgm
name: DVISVGM
commands:
- name: The dvisvgm program
command: >
@{
base = getBasename(file).concat('.dvi');
return getCommand('dvisvgm', base, options);
}
arguments:
- identifier: options
flag: >
@{
if (isList(parameters.options)) {
return parameters.options;
}
else {
throwError('I was expecting a list of options.');
}
}


the following document will produce an animated svg if compiled with arara

% arara: latex
% arara: latex
% arara: dvisvgm : {options: ['exact', 'font-format=woff', 'zoom=-1']}
\documentclass[dvisvgm]{standalone}
\usepackage{tikz}
\usetikzlibrary{animations}

\begin{document}

\tikz
\node :fill opacity = { 0s="1", 2s="0", begin on=click }
:rotate = { 0s="0", 2s="90", begin on=click }
[fill = blue!20, draw = blue, ultra thick, circle]
{Click me!};

\end{document}

• Does TeXmaker offer the same possibilities as TexStudio? – AndréC Jan 10 at 2:44
• @AndréC I think that texmaker does not support magic comments -- I don't know if one can defines user commands (I'd always prefer texstudio over texmaker, it offers much more options) – samcarter Jan 10 at 8:49
• Is it possible to add to the compilation command line "Open with Opera"? "Open with Chrome"? "Open with the default web browser"? etc. – AndréC Jan 11 at 6:13
• @AndréC Yes, for example % !TeX TS-program = latex % | latex % | dvisvgm --exact --font-format=woff --zoom=-1 %.dvi | firefox %.svg works for me – samcarter Jan 11 at 9:19
• @AndréC This depends on your operating system. On mac I can do open %.svg to open the file with the default program associated with the file type .svg (which is not necessarily a browser, depending how you configured your system) – samcarter Jan 11 at 10:52

Incredible! Work even in PDF with the animate package of Alexander Grahn.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{animations}
\usepackage{animate}
\newcommand{\myAnim}[1]{
\begin{tikzpicture}[make snapshot of=#1]
\node :shift = {
0s="0", 2s="1", begin on=click }
at (1,.5) [fill = blue, opacity=.5, circle] {Click};
\end{tikzpicture}
}
\begin{document}
\pagestyle{empty}

\begin{animateinline}[autoplay,loop]{2}
\multiframe{5}{nSec=0+0.5}{
\myAnim{\nSec}
}
\end{animateinline}
\end{document}


• Nice! I like your idea to build a wrapper around TikZ's SMIL-type animations that allows us to output frame-based animations with pkg animate. – AlexG Jan 11 at 9:18
• But this only works with linear animations, not branching ones, where objects are animated individually and interacted with by user action. – AlexG Jan 11 at 9:34
• @AlexG Thanks, I don't know very well the animate package, certainly not well as you, I think. I make this try because the \multiframe command look very similar at the \foreach command suggested in the pgfmanual for get snapshots of the animation at a give time. – vi pa Jan 11 at 22:39