Is it possible to extract TikZ diagrams as image files? [closed]

I would recommend the following approach. Place the TikZ picture in question in a separate file and use the standalone class to compile it standalone. It uses the preview package mentioned in the other answers. To include the picture in the main document, load the standalone package there first and then use the \input command on the picture file.

This will allow you to get a single PDF of the TikZ picture without margins. Then you can
use, say, a PDF-to-PNG converter to get a PNG (recommended for web publishing of drawings).
The SVG format would be nicer, because it’s a vector format, but not all browsers might be able to display it.

Here some example code.
The TikZ picture file (e.g., pic.tex):

\documentclass{standalone}
\usepackage{tikz}
% all other packages and stuff you need for the picture

\begin{document}
\begin{tikzpicture}
% your picture code
\end{tikzpicture}
\end{document}

The main document:

\documentclass{article} % or whatever class you are using
\usepackage{standalone}
\usepackage{tikz}
% All other packages required
\begin{document}
% Text text text
% somewhere where you want the tikz picture
\input{pic}
% Text text text
\end{document}

Then compile the picture and convert it, e.g., with ImageMagick:

pdflatex pic
convert -density 600x600 pic.pdf -quality 90 -resize 800x600 pic.png

or try SVG:

convert pic.pdf pic.svg

Leave a Comment

tech