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

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:

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

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.