95

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?

1

5 Answers 5

47

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}%  <<< Note that `%` here, otherwise a space will be inserted before the picture, refer to https://tex.stackexchange.com/q/7453/250119
  \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
12
  • @Konrad: but where do I put this command??
    – Vivi
    Commented Jul 28, 2010 at 19:39
  • 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. Commented 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
    Commented 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. Commented Apr 9, 2011 at 8:52
  • 2
    @pluton The new edit explains it.
    – user202729
    Commented Mar 5 at 23:13
133

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.

3
17

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}
4
  • This is the method I use. I adapted it from CirKuit. (see on SO)
    – David Z
    Commented Jul 28, 2010 at 19:31
  • 1
    It is also the method used by texample.net to prepare PDF output of submitted examples.
    – Sharpie
    Commented Aug 4, 2010 at 4:24
  • but when I include this as standalone tex file, the main tex file would't compile
    – sunxd
    Commented Aug 25, 2019 at 18:47
  • Note that pdftex is only intended to be used if you're compiling with PDFLaTeX. Probably better to remove that option. Also: if the image is too large compared to the page there will be a warning.
    – user202729
    Commented Mar 5 at 23:20
15

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)

15
  • 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
    Commented 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
    Commented 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
    Commented Jul 28, 2010 at 20:29
  • 1
    @Vivi: What operating system are you using? Commented 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. Commented Feb 14, 2011 at 13:22
8

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
1
  • +1 For being much easier and versatile. With this you can just pack all figures in one file, compile once and voila`!
    – Three Diag
    Commented Feb 8, 2016 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .