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

6

86

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?

Salbu answered 23/4, 2010 at 20:50 Comment(3)
I use a program called CirKuit which serves as a TikZ editor, and allows you to save the diagram as any of various image formats. I think what it does behind the scenes, though, is use latex and dvitopng or something like that. I'll post an answer if I can find out the details before anyone else posts something.Vicechairman
Do you want to isolate the diagrams from the text automatically, or are you happy to cut and paste the TikZ code?Disembody
See also Compile a LaTeX document into a PNG image that's as short as possible and the other questions linked to it.Tati
V
32

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.

Vicechairman answered 23/4, 2010 at 21:19 Comment(1)
Thanks! All I wanted to avoid was having to manually isolate the diagrams, so this should work fine.Salbu
T
70

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
Tati answered 24/4, 2010 at 8:14 Comment(4)
I've found pdf2svg to be a more reliable tool for converting PDF to SVG than imagemagick.Caseous
@Glutanimate: In the meantime I started to use inkscape for it.Tati
@MartinScharrer I was able to embed pdf file into the parent document, without converting it to png or svg: \includegraphics{file_name.pdf}Ablate
@user4035: Sure, that's the normal way to inlcude PDFs into your (pdf)LaTeX document. However, this question is about how to get the diagrams as raster image files so that they can be displayed online.Tati
V
32

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.

Vicechairman answered 23/4, 2010 at 21:19 Comment(1)
Thanks! All I wanted to avoid was having to manually isolate the diagrams, so this should work fine.Salbu
D
12

See the TikZ manual section “Externalizing Graphics”. This lets you make EPS or PDF versions of your graphics. I use EPS files, convert them to TIFFs and can then put them wherever I need to.

Dearing answered 24/4, 2010 at 15:57 Comment(3)
As of 2013, I think this is probably the best approach.Unfrock
In 2017 this is section 50.4 (page 607) of the pgfmanual. It seems we can't give the link to a web page for the section (it's only in pdf). :(Mexican
Theoretically, this works nicely. In practice, after using tikzexternalize for quite a while, I have reverted to the standalone approach, because just seems easier to get it working. I've had quite a bit of a hassle with tikzexternalize.Umbrian
C
7

I'm generally using something along these lines:

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
%\setlength\PreviewBorder{2mm} % use to add a border around the image
\usepackage{tikz}
\begin{document}
\begin{preview}
\begin{tikzpicture}
    \shade (0,0) circle(2); % background
    \draw (0,0) circle(2);  % rim
    \draw (.75,1) circle(.5);   % right eye
    \fill (.66,.9) circle(.25); % right pupil
    \draw (-.75,1) circle(.5);  % left eye
    \fill (-.66,.9) circle(.25);% left pupil
    \fill (.2,0) circle (.1);   % right nostril
    \fill (-.2,0) circle (.1);  % left nostril
    \draw (-135:1) arc (-135:-45:1) -- cycle; % mouth
  \end{tikzpicture}
\end{preview}
\end{document}

The pdf file generated contains the standalone TikZ picture. To convert it to any other file format, simply open it with Gimp.

Clingstone answered 23/4, 2010 at 23:31 Comment(0)
T
2

I use this to Makefile rule create PNG files and thumbnails from .tex files that contain one tikzpicture each. It works similar to the creation of images at TeXample.net:

%.png: %.tex
        @sed 's/^\\begin{document}/\
\\pgfrealjobname{dummy}\\begin{document}\\beginpgfgraphicnamed{example}/' $< | \
sed 's/^\\end{document}/\\endpgfgraphicnamed\\end{document}/' > example.tex ; \
        pdflatex --jobname=example example.tex ; \
        gs -dNOPAUSE -r120 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sDEVICE=png16m \
-sOutputFile=$@ -dBATCH example.pdf ; \
        convert -thumbnail 200 $@ $(addsuffix .thumb.png, $(basename $@)) ; \
        mv example.pdf $(addsuffix .pdf, $(basename $<)) ; rm example.*

ghostview (gs) can probably be replaced by convert and you can replace example with another tempfile prefix.

Transudation answered 10/8, 2010 at 20:21 Comment(0)
C
2

Windows: Just to be completely explicit for noobs like me

Use the prewiew package as Habi suggested in the above in your latex code to remove margins.

Install MIktex and Inkscape and use the following bat file:

cd = "C:\Path to tex"
pdflatex input-file-name.tex
"C:\Program Files (x86)\Inkscape\inkscape.exe" input-file-name.pdf --export-plain-svg=output-file-name.svg
Charleencharlemagne answered 7/2, 2012 at 14:15 Comment(1)
In macos I am getting following error: zsh: permission denied: /Applications/Inkscape.appDominick

© 2022 - 2024 — McMap. All rights reserved.