How do I tell TikZ to output to a standalone image file? Like JPG, PNG, etc?

Yes, there is a way to output it to PDF, then do a snapshot. But this is for mathematical-visual comparisons, and I need precision trimming so that I can put the images side-by-side in a Word document.

Here's a sample using the standalone class. It only takes care of the length, but the width stays the same as the textwidth



  \draw [step=0.5] (-1.4,-1.4) grid (1.4,1.4);


I want the whole PDF to be trimmed to the edges of the tikzpicture. Or, even better, trim the PDF so that that I can leave (custom) margins around the graphic.

  • 1
    possible duplicate of Compile a latex document into a png image that's as short as possible.
    – Caramdir
    Commented Mar 14, 2011 at 3:14
  • There are some possibilities to get only a picture but with pdf format. Then you need to use extern tool to transform the pdf file into a png or jpg file. I think you can find some links about this question. Commented Mar 14, 2011 at 6:29
  • @Caramdir: The duplicate you mentioned only takes care of the length of the standalone. Anyway, knowing better now from that other question, I'll edit my question.
    – Kit
    Commented Mar 14, 2011 at 7:23
  • The other one that you should look at is How can I create a pdf document exactly as big as my tikz picture?. Commented Mar 14, 2011 at 7:37
  • Now that I see Martin's solution, I must admit that I would never have guessed that that was the problem! I suspect that this will now get closed as a duplicate (and maybe be merged with that other question), but this is definitely a valuable piece of information to have on this site. So good question! Commented Mar 14, 2011 at 8:18

2 Answers 2


You have empty lines between the document and tikzpicture environment which puts the picture in an paragraph (which is \textwidth wide). Simply removing the lines fixes this:


  \draw [step=0.5] (-1.4,-1.4) grid (1.4,1.4);

The rest of your question is already answered in Compile a LaTeX document into a PNG image that's as short as possible.

In short:

pdflatex file
convert -density 300 file.pdf -quality 90 file.png

or with v1.0 of the standalone class:


compile with:

pdflatex -shell-escape file

You might also want to use the new border option to set the border to 0pt.

  • With preview, we have some options like activ, pdftex Is it possible to use the same options with the standalone class directly ? And we can fix the border \setlength\PreviewBorder{...} and we can make a choice between the environments with \PreviewEnvironment{...}, I suppose we can make the same things ? Commented Mar 14, 2011 at 11:26
  • @Altermundus: You could disable the use of preview with preview=false. This does effectively the same as removing the 'active' from 'preview'. The develop version has the border={<dimension(s)>} option which sets \PreviewBorder (one dimension) or \PreviewBbAdjust directly (two or four dimension, space separated). All other options are passed to the loaded class and are therefore global, so pdftex would be passed to preview. Commented Mar 14, 2011 at 11:36
  • @Martin Scharrer: standalone.cls from texlive 2011 appears to be version 0.4a - is version 1.0 available somewhere else? Commented Dec 21, 2011 at 21:53
  • @prettygully: It's on CTAN since yesterday. TeXLive has it as well now. Please use tlmgr to update the package. See tug.org/texlive/tlmgr.html if you need help with that. Commented Dec 22, 2011 at 11:04
  • 2
    As security protections go crazy right now, this is not possible on newer systems. If you get a weird error convert-im6.q16: not authorized then edit /etc/ImageMagick-6/policy.xml and change the rights for pdf, i.e. change <policy domain="coder" rights="none" pattern="PDF" /> to <policy domain="coder" rights="read" pattern="PDF" />. (add ,write if writing PDF files is also required).
    – kap
    Commented Apr 17, 2020 at 21:47

This is a complement to the Martin Scharrer's answer for the particular case of Windows. This answer is not exhaustif, but gives a particular configuration that works for me.

Installation under Windows (checked with XP/7/10)

  1. Install GhostScript;
  2. Put the folder (something like C:\...\Ghostscript\bin) containing gswin32c.exe in the PATH;
  3. Create a folder named StackExchange where you will put all your questions and answers for TeX.SX;
  4. In this folder create a file standalone.cfg with the following content

    \standaloneconfig{convert={ghostscript, density=700, command={\gsexe\space -dDownScaleFactor=4 -dSAFER -dBATCH -dNOPAUSE -sDEVICE=\gsdevice\space -r\density\space -sOutputFile=\outfile\space \infile}}}
  5. Use as model for your question/answer in this folder something like

    % \usetikzlibrary{ }
        \fill[red] (0,0) circle (1);

And now just compile your tex file with the option -shell-escape and you will obtain a PNG with the same name and "resonable" default size.

You must log in to answer this question.

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