3

I am writing an animation in TikZ, where I often have one node appearing in different contexts and want to animate the path it takes from one setting to the other, for now I am preparing the settings using invisible (opacity=0) nodes to calculate the coordinates and anchors to then create a visible node that gets animated. Here's a MWE:

\documentclass[dvisvgm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{animations}
\begin{document}
\begin{tikzpicture}[scale=4,transform shape]
    \node[circle, draw, ultra thick, fill=blue!20] (Go) at (0,-2) {Go!};

    \begin{scope}[
            every node/.style={opacity=0.2, draw},
            baseline,
            node distance=0
        ]
        \node (start-M) at (0,0) {Minimal};
        \node (start-E) [base right=of start-M] {example};
        \node (end-M) at (0,-1) {Minimal};
        \node (end-W) [base right=of end-M] {working};
        \node (end-E) [base right=of end-W] {example};
    \end{scope}

    \node (M) :position={0s="{(start-M.center)}" base, 1s="{(end-M.center)}", freeze, begin on ={click, of=Go}} {Minimal};
    \node (W) at (end-W) :opacity={0s="0" base, 1s="1", freeze, begin on={click, of=Go}} {working};
    \node (E) :position={0s="{(start-E.center)}" base, 1s="{(end-E.center)}", freeze, begin on={click, of=Go}} {example};
\end{tikzpicture}
\end{document}

Here I set opacity=0.2 instead of 0 so you can see the nodes. See the resulting SVG file (click on the go button).

I'm wondering, is it possible to have the nodes of the invisible scope not appear in the SVG file at all? I only need them to calculate positions and afterwards use these positions to draw the visisble nodes.

  • Why not opacity=0 instead of opacity=0.2? – AlexG Oct 1 at 11:13
  • So you can see the nodes, as I said. Changing the opacity to 0 still results in the nodes appearing in the SVG source. – Christoph Oct 1 at 11:16
  • BTW, always add dvisvgm option --font-format=woff. See tex.stackexchange.com/a/469440 . – AlexG Oct 1 at 11:16
  • @AlexG Thanks, I usually do that and fixed it for this MWE now. – Christoph Oct 1 at 11:22
3

UPDATE:

I did not think it would compile, but to my surprise it did.

Just wrap the entire scope used for calculating start and end coordinates of the animation paths in a \phantom{...} or an unused lrbox. This code does not produce any trace in the resulting SVG, yet fulfills its purpose of defining the needed coordinates.

Compile to SVG with latex (2x) and dvisvgm --font-format=woff --exact --zoom=-1 on the intermediate DVI:

\documentclass[dvisvgm]{article}
\pagestyle{empty}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{animations}
\begin{document}
\begin{tikzpicture}[transform shape]
    \node[circle, draw, ultra thick, fill=blue!20] (Go) at (0,-2) {Go!};

    \begin{lrbox}{0}
    \begin{scope}[
            every node/.style={opacity=0.2, draw},
            baseline,
            node distance=0
        ]
        \node (start-M) at (0,0) {Minimal};
        \node (start-E) [base right=of start-M] {example};
        \node (end-M) at (0,-1) {Minimal};
        \node (end-W) [base right=of end-M] {working};
        \node (end-E) [base right=of end-W] {example};
    \end{scope}
    \end{lrbox}

    \node (M) :position={0s="{(start-M.center)}" base, 1s="{(end-M.center)}", freeze, begin on ={click, of=Go}} {Minimal};
    \node (W) at (end-W) :opacity={0s="0" base, 1s="1", freeze, begin on={click, of=Go}} {working};
    \node (E) :position={0s="{(start-E.center)}" base, 1s="{(end-E.center)}", freeze, begin on={click, of=Go}} {example};
\end{tikzpicture}
\end{document}

  • Is it possible to automically add \phantom{...} to all the nodes within the scope? – Christoph Oct 1 at 11:42
  • While this does remove the <text> tags from the SVG file, each node still comes with a <g transform="..."> containing a bunch of nested tags ending in an empty <g stroke="none"/>. – Christoph Oct 1 at 11:56
  • @Christoph The auxiliary code for coordinate calculation can be put in an lrbox. It does not produce the redundant SVG code. See edit. – AlexG Oct 1 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.