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?

-

migrated from stackoverflow.comSep 28 '11 at 20:09

This question came from our site for professional and enthusiast programmers.

Have a look at Fitting page size to a tikz figure, and please tell us if the answer there (or here) helped. If here, you could accept the answer by clicking the checkmark (you probably know). – Stefan Kottwitz Sep 28 '11 at 20:14

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.

-
Thank you so much, it was exactly what I was looking for – mariosangiorgio Sep 28 '11 at 20:22
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

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.

-

If you create the tikzpicture in an empty document,

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


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

-