Reuse TikZ Figures in Both Articles and Slides

  20 June 2019  │ 
  LaTeX, TikZ, Beamer

I use LaTeX a lot. I found it an effective tool to write articles, slides (using beamer), and posters (using tikzposter). Further more, I use TikZ/PGF to draw all the figures. Not only the source code of the figure can be easily managed by version control software like git, TikZ is also well-integrated in beamer to create beautiful animations for slides.

It is common to have the same figure used in multiple places among articles, beamer slides, and posters. Therefore, it is desired to share the TikZ source codes without duplicating the files. Noted, the figures used in the beamer often contain the animations in the form of \onslide<overlay specification>{...}. As such, we need to a way to manage these differences in the TikZ source codes and avoid the duplication at the same time.

To tackle this issue, I found a nice solution is to use the standalone package. As its name suggests, it can be used to create pictures as standalone or as part of a document. For example, we can create a figure as the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
\documentclass[tikz]{standalone}

\usetikzlibrary{calc,positioning}

% Create fake \onslide and other commands for standalone picture
\usepackage{xparse}
\NewDocumentCommand{\onslide}{s t+ d<>}{}
\NewDocumentCommand{\only}{d<>}{}
\NewDocumentCommand{\uncover}{d<>}{}
\NewDocumentCommand{\visible}{d<>}{}
\NewDocumentCommand{\invisible}{d<>}{}

\begin{document}

\begin{tikzpicture}
  \node[draw] (start) { Start };
  \node[draw, right=2cm of start] (end) { End };
  % The following animation will only have affect in beamer.
  % In standalone mode, the figure is static.
  \onslide<2-> { \draw[-latex] (start) -- (end); }
  \coordinate (mid) at ($(start)!.5!(end)$);

  % We could control parts of figure only shown in beamer or vice versa.
  \ifstandalone
    \node[below=1cm of mid] {Only Shown in Standalone Figure};
  \else
    \node[below=1cm of mid] {Only Shown in Beamer};
  \fi
\end{tikzpicture}

\end{document}

This file can be compiled directly to create standalone picture, which could be helpful if you want to create image used by web pages or other similar scenarios. To include this figure in the article or poster, you can use standalone package in the following manner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass{article}

% `mode=build` tells LaTeX to build the image from the source then use it.
\usepackage[mode=build]{standalone}

\begin{document}

\begin{figure}[t]
  \centering
  \includestandalone[width=0.8\linewidth]{./figure} % without the `.tex` extension
  \caption{TikZ Figure in Article}
\end{figure}

\end{document}

Finally, this figure can be used in beamer with its animations functioning as normal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\documentclass{beamer}

% `mode=tex` means we will use the source file of the figures directly.
\usepackage[mode=tex]{standalone}

% We need to add TikZ definitions again. You could put them in a separated file
% and use `\input{...}` command to avoid the duplication.
\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\begin{document}

\begin{frame}{TikZ Figure in Beamer}
  \begin{figure}
    \centering
    \includestandalone[width=0.8\linewidth]{./figure} % without the `.tex` extension
  \end{figure}
\end{frame}

\end{document}

Note: In order to use mode=build, you need to pass -shell-escape flag to the LaTeX compiler. Further, for latexmk users, mode=buildnew is preferred than mode=build. Otherwise, latexmk could be stuck in a loop of compiling the files repeatedly.