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

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

1 Answer 1

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
  • 2
    Great, thanks! Of course, for this to work we have to call pdflatex -shell-escape file.tex instead of pdflatex file.tex.
    – Turion
    Commented May 13, 2012 at 13:00
  • @Turion: Yes, you're right, I forgot to mention that.
    – Jake
    Commented May 13, 2012 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .