# How can I create a pdf document exactly as big as my tikz picture?

I often create pictures in tikz that I want to either send to someone, or include in an email. The problem with that is that there is a big white space around and and especially below my pictures.

To get around this, I usually print screen my figure and crop it to get the size I want. Another solution is to use beamer and then scaling to make the figure as big as the slide size. Those two solutions, though, are not all that good. What I would really like is a way to get the pdf file to be just as big as my picture, as if it was cropped from start.

Is there a package to do that?

-

TikZ can do that itself. Have a look at section 63 of the TikZ documentation: “Externalizing graphics.”

This describes how all TikZ graphics in a given document can be pre-processed to speed the actual processing of a document. This results in one PDF file per TikZ graphic.

This is the (shortened) example document:

% This is the file survey.tex
\documentclass{article}
\usepackage{graphics}
\usepackage{tikz}
\pgfrealjobname{survey}
\begin{document}

In the following figure, we see a circle:
\beginpgfgraphicnamed{survey-f1}
\begin{tikzpicture}
\fill (0,0) circle (10pt);
\end{tikzpicture}
\endpgfgraphicnamed

\end{document}


The following command produces the image file survey-f1.pdf from the above document, cropped to just the TikZ picture:

pdflatex --jobname=survey-f1 survey.tex

-
@Konrad: but where do I put this command?? –  Vivi Jul 28 '10 at 19:39
@Vivi: just on the command line when you compile your tex file. Just run "pdflatex survey.tex" add the option Konrad supplied. –  Suppressingfire Jul 28 '10 at 19:58
@Suppressingfire: what command line? I don't use a command line, I just use an editor. I just point and click in the editor to "compile" and it does it for me (I actually use Emacs and type C-c C-c, but that doesn't change my question). Where can I go to run this command? –  Vivi Jul 28 '10 at 20:30
@Vivi: I guess that the easiest way when working with a tool chain like LaTeX is to use a command line. But you can also put this command into a shell script in your project’s folder, and execute that script. On Windows, a shell script is just a text file that ends on .cmd. You can execute it by double-clicking on it. For Linux, its usual ending is .sh and the process is similar, but it needs a shebang line and the beginning. Beware that on Windows, the system may have difficulties finding the latex executable if it’s not properly set up. –  Konrad Rudolph Jul 30 '10 at 14:55
@Konrad: I use mac OSX hehe :) –  Vivi Jul 30 '10 at 22:43

The package standalone does just that. It is a new package (released this year, I think) which will produce a document exactly as big as your figure (and you can use this for text or other things as well). Here is how you would set up your document

\documentclass{standalone}
\usepackage{tikz}
%include other needed packages here
\begin{document}

\begin{tikzpicture}
% include your tikz code here
\end{tikzpicture}

\end{document}


If you do that, then you will be able to compile this directly to get a .pdf document exactly as big as the figure, which can then be included in emails, word documents, or even as a picture in another .tex document using the \includegraphics{} command.

The best thing about this is that it can also be included in a .tex document (e.g. article, beamer, etc.) as a .tex file using the \input{} command without having to change anything in the .tex document above. The main thing is to include the package standalone in your preamble (i.e. before \begin{document}) together with any packages you used in the above code, which in my example would be:

\usepackage{standalone}
\usepackage{tikz}


and then where you want the picture to go put, for example:

\begin{figure}[!h]
\input{mytikzfig.tex}
\caption{  }
\end{figure}


where mytikzfig.tex is the .tex document with your tikz picture using the standalone package.

You can see this solution given in an answer to an StackOverflow question.

-
Hadn't seen this package before, thanks. –  Will Robertson Jul 29 '10 at 2:40

For a single TikZ picture, you can also use the preview package

\documentclass{article}

\usepackage{tikz}
\usepackage[active,pdftex,tightpage]{preview}
\PreviewEnvironment[{[]}]{tikzpicture}

\begin{document}

\begin{tikzpicture}[options]
...
\end{tikzpicture}

\end{document}

-
This is the method I use. I adapted it from CirKuit. (see on SO) –  David Z Jul 28 '10 at 19:31
It is also the method used by texample.net to prepare PDF output of submitted examples. –  Sharpie Aug 4 '10 at 4:24

Make sure that there's nothing extra on the page (page numbers, for example, using \thispagestyle{empty}) and then use the command pdfcrop which is included in TeXLive. (It's a perl script).

That's what I used to make this picture for this answer on MathOverflow:

-
@Andrew Stacey: I don't understand what you mean by "use the command pdfcrop". How would you do that? I don't see it in your .tex file. Would you be able to explain? –  Vivi Jul 28 '10 at 19:35
It's a shell command, i.e. a separate program you have to run after you generate the full PDF file, that will crop off all the white space around the edges. –  David Z Jul 28 '10 at 19:43
Sorry, can you be more specific? How can I run a shell command? I keep seeing that, but I have no idea of how to do it... Do I need to type something in the terminal? If yes, what can I type? Is there a software I need to get to "run the shell command"? –  Vivi Jul 28 '10 at 20:29
@daroczig: I just ran pdfcrop on all the PDFs in the directory where I try out hacks for this site. On a total of 67 PDFs (most single paged, but that's what this answer is targeted to) it took 1 minute to process them all. Given that there is no loss of information in this cropping, I would not expect a decrease in file size. Then the fact that the median increase in file size was 110% says that this is fairly efficient. So I'd like to see some evidence of your claim before I take any notice of it. –  Loop Space Feb 14 '11 at 10:33
@daroczig: Thanks for adding the information. It is possible that we are talking about different programs. When I search for a homepage for pdfcrop then I get pdfcrop.sourceforge.net which is not the program that I was talking about. I'm talking about the program pdfcrop by Heiko Oberdiek which comes with TeXLive. I don't understand the characterisation of this solution as "tricky"; if someone is a commandline junkie (like me) then it could be a lot simpler than an inbuilt solution. But then I'm not offering this as THE solution, just a solution. –  Loop Space Feb 14 '11 at 13:22

Since Konrad's answer didn't work for me and my version of pgf I took a look at the manual and came up with the following solution from the manual:

\documentclass{article}
%  main document, called main.tex
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/] %  activate

\begin{document}
\tikzsetnextfilename{trees}
\begin{tikzpicture} %  will be written to ’figures/trees.pdf’
\node {root}
child {node {left}}
child {node {right}};
\end{tikzpicture}

\begin{tikzpicture} %  will be written to ’figures/main-figure0.pdf’
\draw[help lines] (0,0) grid (5,5);
\end{tikzpicture}
\end{document}


The following command will generate the figures:

pdflatex -shell-escape main

-