[Tex/LaTex] How to create a pdf document exactly as big as the tikz picture

paper-sizetikz-pgf

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?

Best Answer

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