Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I’m using TikZ to draw diagrams in LaTeX that I then want to isolate as image files to put online. I’m using TeXnicCenter to edit the sources.

Is there is a way to extract these diagrams directly without having to extract them from the finished PDF file?

like image 600
Qiaochu Yuan Avatar asked Apr 23 '10 20:04

Qiaochu Yuan


People also ask

How do you save a TikZ image?

After you generate a preview, you can export the image to a file or the clipboard by right-clicking on the preview itself and selecting the appropriate option. If you select Export Image... , the Export Image dialog will be shown, where you can choose where to save the file, as well as its size and format.

How do I save a TikZ image as a PNG?

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).

What is the use of the TikZ package in LaTeX?

TikZ is a LaTeX package that allows you to create high quality diagrams—and often quite complex ones too. In this first post we'll start with the basics, showing how to draw simple shapes, with subsequent posts introducing some of the interesting things you can do using the tikz package.


2 Answers

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 
like image 78
Martin Scharrer Avatar answered Sep 21 '22 02:09

Martin Scharrer


Following up on my comment: Cirkuit converts TikZ diagrams into images by running something like the following sequence of commands:

pdflatex img.tex pdftops -eps img.pdf convert -density 300 img.eps img.png 

Here img.tex would be a LaTeX file that follows this template:

\documentclass{article} \usepackage{tikz,amsmath,siunitx} \usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks} \usepackage[graphics,tightpage,active]{preview} \PreviewEnvironment{tikzpicture} \PreviewEnvironment{equation} \PreviewEnvironment{equation*} \newlength{\imagewidth} \newlength{\imagescale} \pagestyle{empty} \thispagestyle{empty} \begin{document} \begin{tikzpicture}     % (your TikZ code goes here) \end{tikzpicture} \end{document} 

If you are able to use Cirkuit or a similar editor, or write a script for yourself to put your diagram into that template and run the appropriate tools, you'll have a quick way to convert TikZ code into a PNG image.

To answer your question more directly... no, I don't know of any way to convert a TikZ diagram directly to PNG without going through a PDF file (or at least DVI) at some stage.

like image 28
David Z Avatar answered Sep 21 '22 02:09

David Z