94

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?

Vivi
  • 26,953
  • 31
  • 77
  • 79

5 Answers5

129

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.

Vivi
  • 26,953
  • 31
  • 77
  • 79
46

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
user202729
  • 7,143
Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
  • @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
  • 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](http://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. – Konrad Rudolph Jul 30 '10 at 14:55
  • @Konrad: I use mac OSX hehe :) – Vivi Jul 30 '10 at 22:43
  • @Vivi: For OS X, it works the same as for Linux, much simpler than on Windows. In case you didn’t know, there’s an application called Terminal.app that allows you to use a command line console. For cases such as this, that’s very useful. You should definitely learn the basics of working with the command line when doing LaTeX work, although that takes a bit of time at first. – Konrad Rudolph Jul 31 '10 at 07:25
  • @Konrad: sure, I know I have terminal, and I used it a few times, though I am far from familiar with it, and I have certainly not used it with LaTeX. Could I use X11 instead? Is it the same as terminal? – Vivi Jul 31 '10 at 08:18
  • @Vivi: X11 in itself is merely a “driver” program that is necessary for several programs. But it includes an own terminal application, called Xterm. Which terminal application you use is unimportant: they all work equally well. – Konrad Rudolph Jul 31 '10 at 13:16
  • @Konrad: thanks for the explanation! I have been using X11 (or xterm) to start matlab and aquamacs, and it seemed so similar to terminal... I better put more effort into learning how to use the command line! – Vivi Jul 31 '10 at 20:14
  • 1
    @Konrad: great advice but I still observe a white left margin on the final pdf. Is there a reason why? – pluton Apr 08 '11 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. – Konrad Rudolph Apr 09 '11 at 08:52
  • 2
    @pluton The new edit explains it. – user202729 Mar 05 '24 at 23:13
16

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}
Juan A. Navarro
  • 62,139
  • 32
  • 140
  • 169
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)

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • 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 '10 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 '10 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 '10 at 20:29
  • 1
    @Vivi: What operating system are you using? – Andrew Stacey Jul 28 '10 at 21:04
  • 1
    @Andrew Stacey: mac OS X. So should I type this on terminal? Do I need to be in a specific directory? – Vivi Jul 28 '10 at 21:22
  • 1
    @Vivi: Yes, type this in the terminal app and you should be in the directory where the PDF file is. – Andrew Stacey Jul 29 '10 at 08:11
  • 1
    @Andrew Stacey: so to be clear (maybe this will be useful for someone else), do I type pdfcrop 2n_graph_small.tex ??? – Vivi Jul 29 '10 at 08:25
  • 1
    @Vivi: almost! First run pdflatex on the file and then run pdfcrop on the resulting pdf. – Andrew Stacey Jul 29 '10 at 11:29
  • Just to mention: it seems to me that pdfcrop makes a (lot) bigger sized pdf than the original one, also it could take a while to process some complex pictures. – daroczig Feb 08 '11 at 12:18
  • 1
    @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. – Andrew Stacey Feb 14 '11 at 10:33
  • @Andrew Stacey: good for you and you do not have to take any notice of my comment, I am happy that this solution could work also. I just mentioned that pdfcrop is a rather tricky solution instead of the above answers which seem a lot cleaner. There is now problem with that, though I think no one should use pdfcrop for images build by himself, as there are nicer methods to do the same with planning. About file size see "Known issues" on the homepage of the software. About speed issue I can only cite a Hungarian site (http://hup.hu/node/91366), sorry. – daroczig Feb 14 '11 at 11:27
  • 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 http://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. – Andrew Stacey Feb 14 '11 at 13:22
  • 1
    @Andrew Stacey: you are really right, sorry for the trouble! Also about branding your solution as "tricky", I did not intend to offend. – daroczig Feb 14 '11 at 14:33
  • This link [https://mathoverflow.net/questions/90/fn-2n-is-not-surjective] does not exist – hola Sep 24 '18 at 17:50
  • @pushpen.paul it's been deleted (it wasn't really at the right level for MO but it was asked very early on in that site's existence). – Andrew Stacey Sep 24 '18 at 20:36
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
cmhughes
  • 100,947
  • +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 08 '16 at 18:57