29

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:

click

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

example-manual

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?
AndréC
  • 24,137
  • 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 09 '19 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 09 '19 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 09 '19 at 22:22
  • @HenriMenke One can even embed EPS files with \includegraphics without any driver option. dvisvgm delegates PSFile special to libgs. – AlexG Jan 09 '19 at 22:23
  • 1
    dvisvgm specials are mostly animation-related and simply ignored during conversion to PDF. – AlexG Jan 09 '19 at 22:26
  • @HenriMenke The code I gave as an example was given to me by AlexG and I am unable to answer you on this subject. As for \tikzset{make snapshot if necessary} I don't understand what this is about. Perhaps you could explain your point of view in a response? – AndréC Jan 11 '19 at 12:55

4 Answers4

32

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}

enter image description here

berkus
  • 1,168
Ulrike Fischer
  • 327,261
25

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=woff2

LaTeX input (compile with latex or dvilualatex):

\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=woff2 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=woff2!!! 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 dvilualatex):

\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=woff2 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"/>

AlexG
  • 54,894
  • 2
    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 '19 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 '19 at 09:10
  • 1
    @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 '19 at 09:52
  • @God Maybe, but my files are innocent ;) – AlexG Jan 11 '19 at 09:55
  • @God The popular Blog sites only permit the tag to embed linked graphics. This tag ignores scripts. animate-generated, JS-driven animations only work if they are embedded with the <object> tag. This one is usually not available on Blog sites. (For the reasons you mentioned.) – AlexG Jan 11 '19 at 10:00
  • Which ones do you mean? The ones in the answer above are not animate generated. They are SMIL-type animations without scripting and thus play in the <img> tag. – AlexG Jan 11 '19 at 10:11
  • 1
    @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 '19 at 10:49
  • @martin : The one in my (vanilla) TeXLive-18 installation, I guess. The man-pages says "Copyright © 2005-2016" at the bottom. – AlexG Jan 11 '19 at 11:02
  • @AlexG Ok, that's a pretty old one. Isn't there a version number and a date given at the very bottom? Obviously, TL doesn't provide the documentation matching the version of dvisvgm. You can get the latest one here. – Martin Jan 11 '19 at 11:12
  • @martin : Thanks for the link! It also documents the font format option. – AlexG Jan 11 '19 at 11:36
  • When I click on the images I see on your answer, the animations do not work. However, it seems that you have embeded them from your personal web site and not from tex.stackexchange. Is that normal? – AndréC Jan 12 '19 at 13:37
  • They should work without click. Do you use MS Edge, par hasard? According to a comment to Ulrike's answer above, Edge does not display animated SVG. Try Chrome instead as Web browser. And yes, I embedded them from my web site, because Imgur does not accept SVG for upload. – AlexG Jan 12 '19 at 15:16
  • @AlexG Okay, you're right, I'm changing browsers. But going from Microsoft to Google is always the GAFA.... – AndréC Jan 12 '19 at 16:12
  • BTW, MS recently announced that they will give up their own rendering engine in Edge in favour of "Blink", which is Google's open-source rendering engine used in their Chrome browser. It is very fast, btw. So, things will improve. This will happen this year. – AlexG Jan 12 '19 at 16:17
  • Is it possible for you to add on this wiki the example from page 221 of manual 3.1? What's new in the latest version 3.1 of TikZ? – AndréC Jan 12 '19 at 17:01
  • When I click on an animation, I am redirected to your website where the image is displayed, is that normal? – AndréC Jan 12 '19 at 21:12
  • @AndréC Yes, in the last edit I turned the <img> tag into a link to the svg location. – AlexG Jan 13 '19 at 11:15
  • @AndréC I did the same in the example added to your wiki answer. The <img> tag forbids any user interaction (e. g. start-on-click) with an SVG, but the added example requires just this. – AlexG Jan 13 '19 at 11:19
  • +1: Simply great. Very cool stuff. – Dr. Manuel Kuehner Jan 16 '19 at 06:19
11

Configuring Texstudio for dvisvgm

Magic comments

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":

enter image description here

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

enter image description here

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

9

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]
\draw (1,.5) circle [radius=1mm];
\node :shift = {
along = {(0,0) circle[radius=5mm]} upright,
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}

enter image description here

vi pa
  • 3,394
  • 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 '19 at 09: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 '19 at 09: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 '19 at 22:39