6

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.

Christoph
  • 678

1 Answers1

6

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}

AlexG
  • 54,894
  • Is it possible to automically add \phantom{...} to all the nodes within the scope? – Christoph Oct 01 '19 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 01 '19 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 01 '19 at 13:12