Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a long article with many, say, TikZ images. Let's say it looks like:

\documentclass{article}
\usepackage{tikz}
\begin{document}
    ...much more stuff...
\begin{tikzpicture}
    \draw[red] (0,0) -- (1,1);
\end{tikzpicture}
    ...much more stuff...
\end{document}

Now from this file, I want to render the whole article into a pdf and also the tikzpicture into a PNG. This has to happen automatically with latex since I want to write a package that uses this.

(So answering "Oh, just copy and paste the image code to a separate file." is not helpful.)

share|improve this question
    
Also, if someone knows a good tag for this question, I will be happy to include it! –  Turion May 13 '12 at 10:51
1  
Not to png, but to pdf.... Yes you can use tikz external library. –  Harish Kumar May 13 '12 at 11:42
2  
related: tex.stackexchange.com/questions/8641/… –  Marco Daniel May 13 '12 at 11:45
    
@HarishKumar: You can also generate PNGs using the external library, if you have ImageMagick (or presumably some other converter) installed. –  Jake May 13 '12 at 11:50
    
@Jake: I knew from outside we can. But from inside... well now I know. Thanks and upvoted. –  Harish Kumar May 13 '12 at 11:52
add comment

1 Answer 1

up vote 13 down vote accepted

You can use the TikZ external library to automatically generate PNGs of your tikzpictures.

The style

\tikzset{
    png export/.style={
        external/system call={
            pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";
            convert -density 300 -transparent white "\image.pdf" "\image.png"
        }
    }
}

specifies that after the tikzpicture is compiled using pdflatex, ImageMagick (convert) is used for generating a PNG from the PDF. This requires pdflatex to be called with -shell-escape.

Here's a complete example:

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzset{
    png export/.style={
        external/system call={
            pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";
            convert -density 300 -transparent white "\image.pdf" "\image.png"
        }
    }
}
\tikzexternalize
\tikzset{png export}

\begin{document}

\tikzset{external/force remake}
\begin{tikzpicture}
\node [orange,scale=10] {1};
\end{tikzpicture}
\begin{tikzpicture}
\node [cyan,scale=10] {2};
\end{tikzpicture}
\end{document} 
share|improve this answer
1  
Great, thanks! Of course, for this to work we have to call pdflatex -shell-escape file.tex instead of pdflatex file.tex. –  Turion May 13 '12 at 13:00
    
@Turion: Yes, you're right, I forgot to mention that. –  Jake May 13 '12 at 13:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.