50

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

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw [step=0.5] (-1.4,-1.4) grid (1.4,1.4);
\end{tikzpicture}

\end{document}

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.

Martin Scharrer
  • 262,582
Kit
  • 16,430

2 Answers2

62

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:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw [step=0.5] (-1.4,-1.4) grid (1.4,1.4);
\end{tikzpicture}
\end{document}

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:

\documentclass[convert={density=300,size=1080x800,outext=.png}]{standalone}

compile with:

pdflatex -shell-escape file

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

Martin Scharrer
  • 262,582
  • 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 ? – Alain Matthes Mar 14 '11 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. – Martin Scharrer Mar 14 '11 at 11:36
  • @Martin Scharrer: standalone.cls from texlive 2011 appears to be version 0.4a - is version 1.0 available somewhere else? – prettygully Dec 21 '11 at 21:53
  • @prettygully: It's on CTAN since yesterday. TeXLive has it as well now. Please use tlmgr to update the package. See http://www.tug.org/texlive/tlmgr.html if you need help with that. – Martin Scharrer Dec 22 '11 at 11:04
  • I made a short script based off of this answer, assuming you have figures in a file "figures.tex" wrapped in \newcommand definitions: https://gist.github.com/gangeli/6fdfebce8ab748a15ea5 . Useful if you need to convert lots of figures (e.g., my use case of moving a beamer presentation to powerpoint); feel free to ignore otherwise. – Gabor Angeli Sep 11 '15 at 04:43
  • How do you compile pdflatex -shell-escape file in TeXstudio? I can't file the "-shell-escape file". Anyone knows? – Baraliuh May 02 '18 at 23:41
  • 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 Apr 17 '20 at 21:47
3

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

    \input{standalone/standalone.cfg}
    \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

    \documentclass[tikz,border=7pt]{standalone}
    % \usetikzlibrary{ }
    \begin{document}
      \begin{tikzpicture}
        \fill[red] (0,0) circle (1);
      \end{tikzpicture}
    \end{document}
    

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.

Kpym
  • 23,002