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?

5 Answers 5


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

In the following figure, we see a circle:
    \fill (0,0) circle (10pt);


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, 2010 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. Jul 28, 2010 at 19:58
  • 1
    @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](en.wikipedia.org/wiki/Shebang_(Unix%29) and the beginning. Beware that on Windows, the system may have difficulties finding the latex executable if it’s not properly set up. Jul 30, 2010 at 14:55
  • 1
    @Konrad: great advice but I still observe a white left margin on the final pdf. Is there a reason why?
    – pluton
    Apr 8, 2011 at 15:56
  • 1
    @pluton Unfortunately, tikz sometimes has difficulties calculating the bounding box. I don’t know what causes this but I suspect that it’s connected to the round shape. Usually this works better. Apr 9, 2011 at 8:52

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

%include other needed packages here   

% include your tikz code here


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:


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

\caption{  }

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.

  • Is there a simple way to output a larger PDF with standalone class (using scale=2, say, distorts the proportions).
    – PatrickT
    Jan 28, 2019 at 12:16
  • This is a nice answer if you use something like Overleaf where access to command line isn't practical. Oct 8, 2020 at 21:59

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





  • This is the method I use. I adapted it from CirKuit. (see on SO)
    – David Z
    Jul 28, 2010 at 19:31
  • 1
    It is also the method used by texample.net to prepare PDF output of submitted examples.
    – Sharpie
    Aug 4, 2010 at 4:24
  • but when I include this as standalone tex file, the main tex file would't compile
    – sunxd
    Aug 25, 2019 at 18:47

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:

Graph of n to 2n

(source file: http://www.math.ntnu.no/~stacey/myicons/2n_graph_small.tex)

  • 3
    @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, 2010 at 19:35
  • 1
    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, 2010 at 19:43
  • 2
    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, 2010 at 20:29
  • 1
    @Vivi: What operating system are you using? Jul 28, 2010 at 21:04
  • 2
    @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. Feb 14, 2011 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:

%  main document, called main.tex
\tikzexternalize[prefix=figures/] %  activate

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

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

The following command will generate the figures:

pdflatex -shell-escape main
  • +1 For being much easier and versatile. With this you can just pack all figures in one file, compile once and voila`!
    – Three Diag
    Feb 8, 2016 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.