Display Each TikZ Figure in One Page in a LaTeX standalone Document
In LaTeX, if we draw a TikZ pictures using the standalone document class, we can easily get a PDF file only including one figure, which can facilitate organizing contents sometimes1. However, if put multiple tikzpicture environments in this standalone document, then we can see they are actually put next to each other. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0)node[draw]{A} -- (1,1)node[draw]{B};
\end{tikzpicture}
\begin{tikzpicture}
\path node at (4,2)[shape=circle,draw]{}
node at (4,1)[shape=circle,draw]{}
node at (4,0)[shape=circle,draw]{}
node at (5,1)[shape=rectangle,draw]{}
node at (3,1)[shape=rectangle,draw]{};
\end{tikzpicture}
\end{document}

So, to put each TikZ figure in one separate page, we can use the option tikz in the \documentclass command (and hence no need to use \usepackage to import tikz package anymore):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0)node[draw]{A} -- (1,1)node[draw]{B};
\end{tikzpicture}
\begin{tikzpicture}
\path node at (4,2)[shape=circle,draw]{}
node at (4,1)[shape=circle,draw]{}
node at (4,0)[shape=circle,draw]{}
node at (5,1)[shape=rectangle,draw]{}
node at (3,1)[shape=rectangle,draw]{};
\end{tikzpicture}
\end{document}

What’s more, we can further split this single file into three files:
File 1: doc.tex
1
2
3
4
5
6
7
8
9
\documentclass[tikz]{standalone}
\begin{document}
\input{fig1}
\input{fig2}
\end{document}
File 2: fig1.tex
1
2
3
\begin{tikzpicture}
\draw (0,0)node[draw]{A} -- (1,1)node[draw]{B};
\end{tikzpicture}
File 3: fig2.tex
1
2
3
4
5
6
7
\begin{tikzpicture}
\path node at (4,2)[shape=circle,draw]{}
node at (4,1)[shape=circle,draw]{}
node at (4,0)[shape=circle,draw]{}
node at (5,1)[shape=rectangle,draw]{}
node at (3,1)[shape=rectangle,draw]{};
\end{tikzpicture}
At this time, when we compile the doc.tex, we’ll get the same PDF file. If we need to design plenty of figures, this method provides an easier way, more or less, to organize them.
Actually, if we just need to insert TikZ figures in LaTeX document, the way in blog1 is more elegant: put the code of each TikZ figure in a standalone class file, and insert all figures one by one using command \includestandalone in the corresponding position. The method introduced in this blog is more suitable for the cases where we want to use TikZ figures in other places, such as MS Word or PowerPoint etc. — we can compile the file that includes all TikZ figures, and then use pdftoppm.exe (which is downloaded along with the TeX Live)2 to convert the generated PDF file, page by page, into .png files, and finally we’ll get each TikZ figure in a single .png file!
References