6

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

  • 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. – user11232 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. – user11232 May 13 '12 at 11:52
14

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} 
  • 2
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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