How can I generate just pgf/TikZ images, not placed a whole page or slide?

I'd like to generate only a pgf/TikZ image with LaTeX, but so far I just managed to insert it into a document. The problem is that so far I can only get a page/a beamer slide rather than just the image I'd like to have.

What document class should I specify to get what I want?

Assuming that you are producing the images in a separate document, you can use the standalone class.

For example, this will produce a cropped image by default.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw [red] (0,0) rectangle (1,1);
\end{tikzpicture}
\end{document}


Alternatively, you can include this file directly into your main file and have the image produced there.

• Never saw standalone before during several years of latex'ing, but it's just what I needed. This should definitely become more well known. – Ragnar Oct 9 '15 at 20:08
• Use \documentclass[tikz]{standalone} to make this also work when you are using tikz libraries. I don't know why exactly this works. See tex.stackexchange.com/a/29875/209725 – Thomas Sep 9 '20 at 14:48

Just another option is preview. With it you can also generate cropped figures but with a great advantage: you can have several figures on the same .tex file. The result will be a pdf file with a cropped figure per page. Then you can select which figure include in your document with 'page=' option of graphicx package.

One example:

\documentclass{article}
\usepackage[active,pdftex,tightpage]{preview}
\usepackage{tikz}

\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,3);
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document}


Process this file and will obtain a pdf with two pages, then select which figure to include in your main document:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}\centering
\includegraphics[page=1]{Preview}%<-- Replace 'Preview' with your figures file's name
\caption{First rectangle}
\end{figure}
\begin{figure}\centering
\includegraphics[page=2]{Preview}%<-- Replace 'Preview' with your figures file's name
\caption{Second rectangle}
\end{figure}
\end{document}


Update:

With verson 1.x of standalone class you don't need to use preview package because my previous description is standalone class default behaviour with option tikz. So, with this simpler code:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,3);
\end{tikzpicture}
\begin{tikzpicture}
\draw (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document}


you will also get a pdf file with two pages with one cropped figure in each page.

• This could have been a marvelous idea, but unfortunately it has a flaw: figures are not placed in the same way on the vertical axis. When I use the baseline option of tikzpicture I get the figure nicely placed on the baseline, this disappears when I store it in a PDF file as you suggest. What a pity… It would be nice to have a tikz feature where figures are only compiled once as long as they are not modified (by calculating their signature). – yannis Aug 5 '17 at 17:07
• @yannis Your problem is completly different from this question, but in any case look for externalize library. I'm not sure if does what you want but serves to avoid compilation of tikz figures every time you compile your text file. – Ignasi Aug 5 '17 at 17:48
• unfortunately @Ignasi this library does not work: first of all it crashed at the slightest error (while TeX engine just continues) and then when I managed to get one figure, it refused to go further and compile other figures. In fact it somehow reads the whole TeX file again and again, for each figure, and my file is very complex… – yannis Aug 5 '17 at 21:15
• @yannis Why don't you write a new question explaining your problem? – Ignasi Aug 5 '17 at 23:18

If you create the tikzpicture in an empty document,

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf

you can use pdfcrop to trim the excess edges/whitespace.