Include Tikz figures

TikZ is a powerful tool for creating vector graphics in LaTeX. The best way to include TikZ figures in Slipshow is to compile them with LaTeX, and import the resulting file into a presentation.

We need to use the standalone class as we are only interested in the TikZ figure, not a full page. See this resource for details. A minimal .tex file containing a standalone TikZ figure looks like:

\documentclass[tikz]{standalone}
% \usetikzlibrary{…} % tikz package already loaded by 'tikz' option
\begin{document}
\begin{tikzpicture}
  % Your TikZ code here
\end{tikzpicture}
\end{document}

If the image is static and we don’t need to execute actions on some of its elements, then the easiest approach is to compile the file into .pdf format.

$ pdflatex file.tex

Integrating it into your Slipshow presentation is then a matter of including the PDF, which is done with the following syntax:

![](file.pdf)

Reveal a TikZ figure bit by bit

Just as for SVGs, if you want to use specific parts of your figure as targets for actions (for instance, to reveal parts of it, bit by bits), you’ll need two things:

  • First, you’ll need to be able to assign ids to parts of your image

  • Then, you’ll need to compile it into a format in which Slipshow is able to access the identifiers; we will use the SVG format.

In order to assign ids, define an environment at the beginning of the file:

\newenvironment{svggroup}[1]
  {\special{dvisvgm:raw <g id="#1">}}
  {\special{dvisvgm:raw </g>}}

Then, consider the following simple picture of a triangle:

\begin{tikzpicture}

  \node[dot] (A) at (0,1) {};
  \node[dot] (B) at (1,0) {};
  \node[dot] (C) at (2,1) {};

  \draw (A) -- (B);
  \draw (B) -- (C);
  \draw (C) -- (A);

\end{tikzpicture}

And suppose you want the node C to appear at a later step. Enclose the node and the edges in groups with an id. Here is the final complete file:

\documentclass[tikz]{standalone}
\newenvironment{svggroup}[1]
  {\special{dvisvgm:raw <g id="#1">}}
  {\special{dvisvgm:raw </g>}}
\begin{document}
\begin{tikzpicture}[dot/.style={circle, fill=black, inner sep=2pt}]

  \node[dot] (A) at (0,1) {};
  \node[dot] (B) at (1,0) {};
  \begin{svggroup}{c}
    \node[dot] (C) at (2,1) {};
  \end{svggroup}

  \draw (A) -- (B);
  \begin{svggroup}{c-edges}
    \draw (B) -- (C);
    \draw (C) -- (A);
  \end{svggroup}

\end{tikzpicture}
\end{document}

Then we need to compile the file to SVG. Here we use latex and dvisvgm. The --no-fonts option improves the precision of text layouts, and --exact-bbox improves the precision of the bounding box.

$ latex triangle.tex
$ dvisvgm --no-fonts --exact-bbox triangle.dvi

Now we need to include the SVG file as a Slipshow image, and add dynamic behaviour with actions:

---
toplevel-attributes: slip enter=~duration:0 unreveal="c c-edges"
external-ids: c c-edges
---

![](triangle.svg){style="width:75%"}

{reveal=c}

{reveal=c-edges}

The external-ids field of the frontmatter tells the compiler not to warn you that it cannot find the given ids. Here is the final result:

Editor
Presentation
Both

    

TikZ and auto-refresh

In serve mode, Slipshow will automatically refresh when the SVG file (eg triangle.svg) changes. However, it will not handle the recompilation of the .tex file if you change the upstream TikZ file.

One way to solve this is to use a general-purpose file watcher tool, to recompile the .tex file when it changes. For instance, with entr command line tool:

$ echo "triangle.tex"| entr sh -c "latex triangle.tex && dvisvgm -n -e triangle.dvi"