# TikZ to non-PDF

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.

-
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. – Alain Matthes Mar 14 '11 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 Mar 14 '11 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?. – Loop Space Mar 14 '11 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! – Loop Space Mar 14 '11 at 8:18

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.

-
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 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: 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 4:43