Is there a script to automate externalizing TikZ graphics?

(As suggested by Scott here)


5 Answers 5


The TikZ 2.10 library, external, addresses this issue. Externalizing all TikZ graphics in a LaTeX document is as easy as:


If your document is named report.tex, this will dump out a series of images: report-figure0.pdf, report-figure1.pdf, etc, etc. To get a more manageable output, you can specify a directory for in which the files are to be placed and names that are more descriptive than report-figure0.pdf:


% Rest of preamble
% Begin document, write stuff

% Set a filename for the next tikzpicture.
\begin{tikzpicture}  % This will be output to figures/importantFigure.pdf
  % Picture code

To compile the document, you will need to ensure the figures directory exists and run pdflatex with shell execution enabled:

mkdir figures
pdflatex -shell-escape <tex file>

A whole pile of options is available to customize the way the figures are output. See section 52.4 (this numbering is subject to change!) of the development version documentation for complete info and options. The manual also covers how to obtain EPS output- but this option looks less polished than PDF output.

  • 1
    Checking out with CVS is more fun than trying to contribute back with CVS, I think. Though I've been shying away from it lately as I can't bring myself to use a non-distributed VCS directly anymore :-), and it's unlikely that there is a DVCS that can pull from CVS do to it's lack of atomicity :-(.
    – SamB
    Commented Dec 19, 2010 at 5:36
  • 5
    Wow, this produces … “interesting” crashes (spurious, random “file ended prematurely” messages etc.) when used together with e.g. todonotes, which also uses TikZ under the hood. I’ve tried conditionally enabling that package and disabling externalization for the to do entries – to no avail. Commented Jan 29, 2011 at 19:34
  • 6
    This external library is now available as part of PGF 2.10. Commented Feb 26, 2011 at 21:20
  • Has anyone been able to solve the problem between tikzexternalize + todonotes?
    – fabikw
    Commented Mar 30, 2011 at 19:02
  • @KonradRudolph: I am having the same problem now. Did you ever figure out how to use todonotes with external?
    – Neil G
    Commented Oct 27, 2011 at 6:56

When I was writing my MSc thesis, I had created all of my figures and diagrams with tikz and pgfplots (using matplotlib2tikz). Keeping all of them within the source was not acceptable, since the compilation was getting really slow, so I was searching for something that would allow me to automatically compile all those figures to pdf files that I could include in my text.

For various reasons, I was not entirely satisfied with any of the other solutions so I wrote a Python script that suited my purpose.

The things you must keep in mind are:

  1. You put the script on the same level as your main.tex file
  2. You write your tikz code. You place each figure within a file with extension *.tikz. Each file contains only \begin{tikzpicture} ... \end{tikzpicture}.
  3. You write the preamble that you want to use with the *.tikz files. This doesn't necessarily contains the same packages as the preamble of your main.tex file. The reason you keep a common preamble for all the figures is to make it easy to make changes in things like fonts etc.
  4. You run the script!

The script searches recursively within a folder structure for *.tikz files and compiles them using the specified preamble. Each time the scripts runs, it stores the modification time of each *.tikz file and on subsequent runs it compiles only the newly created files of the files that have been modified since the last run. If you make changes in your preamble, you just pass a command-line argument and it compiles all the *.tikz files.

On linux it works great. I haven't tested it on windows, but I don't see why it shouldn't run. Anyway, even if there is any problem, the fix is going to be really trivial

  • The hyperlink appears to be broken now.
    – Peiffap
    Commented Jun 9, 2021 at 11:45
  • @Peiffap Thanks for letting me know. I 've updated the link
    – pmav99
    Commented Jun 10, 2021 at 15:27

Here's my alternative; Konrad's answer is probably just as good, and certainly better documented.

for job in `grep -o beginpgfgraphicnamed\{[0-9A-Za-z-]*\} < $1 | sed -e 's/beginpgfgraphicnamed{//' | sed -e 's/}//'`; do
    pdflatex --jobname $job $1;
    rm $job.log;
    rm $job.out;
    rm $job.aux;

I've made a shell script, which builds PDF files from all my tikz/pgfplots/...-figures. Compared to existing solutions, my approach has some advantages: (I don't know the existing solutions very good., but I hope some of the advantages are real advantages...)

  • The build of the individual images only happens if necessary, i.e. if the source or included data or included headers are newer than the (existing?) PDF. If you plot e.g. some data with pgfplots, you can just put a new csv file in the right folder, and in the next compilation of the main document, this is recognized and just the image using this file is rebuilt.

  • Another advantage of my solution is that the PDFs are compiled in parallel. But you get noticed if one or multiple pdfs fail to compile including detailed information from the log files, and then the remaining compilation of the main document stops. The number of compilations running in the background can be limited to a number of processes like 4 or 8 (depending on the number of CPU cores you want to use). This is especially helpfull, if one process needs much RAM (in my case, some extensive pgfplots images need > 1GB of RAM during compilation...)

  • draft option is recognize to avoid unneccessary compilations. Also, there is a buildall and buildnone option, which might be helpfull in case of changing the headers again and again.

  • Not limited tikz, but any packge should be possible, as each image is just a normal, complete .tex document.

  • Concerning tikz, I had some problems with text being cut away at the borders of the pictures using standalone. This behavior does not happen with my solution, as I crop the A4-pages with the images with pdfcrop after compilation.

I have got an .sty-file for the script-call like shown in the following. There is a definition for the images-Folder, two example images and the script call and return value handling. \usepackage{imgs} is the first line after \documentclass in my main document, so it's made sure all pdfs are always up to date.

%%% Input Parameters                                                %%%


% rest


\newcommand*\blank{ }

    \newcommand*\blank{ }

      \immediate\write18{find \imgs\blank -name '*.tex' | sed 's/^\@backslashchar(.*\@backslashchar)\@backslashchar.tex$/\@backslashchar1.pdf/' | xargs rm -f}

    %\immediate\write18{bash -x \imgs img.sh \imgs\blank % for debugging
    \immediate\write18{bash    \imgs img.sh \imgs\blank 
    \imgb\blank % insert additional images like this...
      ; echo $? > pdfscreated.res

    \read\myscriptresult to \ScriptResult

      \@latexerr{Image Compilation failed}\@eha

The image is included like this in a (very short) main document:

\usepackage{imgs} % see code above!
\usepackage{header} % may be the same as for images, or different, as you like

An example for an image would be

\usepackage{../header} % own header for images, may be the same as for main document or not...
    %draw sthg...
    \draw (0,0) -- (1,1);

The called shell script is shown in the following. It searches in each .tex file for usepackage, input, pgfplotstableread and recognizes also if used files are newer than than an possibly existing PDF.


# first argument: folder to work in
cd $1

# counter for pdfs to create

# relevant programms
texbin="lualatex --shell-escape " # Might be necessary with BIG memory needs
#texbin="pdflatex --shell-escape "
pdfcrop='pdfcrop --hires '

# wait for max number of jobs function
waitforjobs() {
    while test $(jobs -p | wc -w) -ge "$1"; do wait -n; done

# loop through all arguments and (re)build PDFs if necessary
while [[ $# > 0 ]] ; do


    # analyze file name
    # $1 = ... regex cut .tex away if there
    tmp=`echo "$1" | sed s/\.tex//`

    # test if tex file exists
    if [ ! -e $tex ] ; then
        echo "$tex doesn't exists. Maybe forgot to create it?"
        exit 2

    # search in file for included, read or inputed files....
    included=`grep 'pgfplotstableread' $tex | sed  's/^.*pgfplotstableread{\(.*\....\)}.*$/\1/'`
    libs=`grep 'usepackage' $tex | sed  's/^.*usepackage\(\[.*\]\)\?{\(.*\)}.*$/\2.sty/'`
    graphics=`grep 'includegraphics' $tex | sed  's/^.*includegraphics\(\[.*\]\)\?{\(.*\)}.*$/\2/'`
    inputs=`grep 'input' $tex | sed  's/^.*input{\(.*\)}.*$/\1/'`
    ## add import and include?!?
    # if inputs don't end on \.\w+, append \.tex...
    list=`echo -e "$included\n$libs\n$graphics\n$inputs"`

    # set buld flag dependent on many conditions....
    if [ ! -e $pdf ] ; then
    for item in ${list//$'\n'/ } ; do
        #if [ -e $item ] ; then # nonexistent items make error in latex, I assume this is preferred...
        if [ $item -nt $pdf ] ; then
    if [ $tex -nt $pdf ] ; then

    # no (re)build necessary
    if [ $bld -eq 0 ] ; then
        echo "$pdf is already up to date."

    # rebuild pdf if necessary 
    waitforjobs 4
    $texbin $tex >/dev/null 2>&1 &


# now we have all PIDs of running compilation.
# check each and wait if necessary...

while [[ $cnt>0 ]]; do
    if [ ${pids[$cnt]} ]; then
        wait ${pids[$cnt]}
        if [ 0 -ne $? ] ; then
            # error occured!
            logs="${names[$cnt]}.log $logs"
            errorstring="${names[$cnt]} $errorstring"
            rm -f ${names[$cnt]}.log
            $pdfcrop $pdf $pdf &
        rm -f ${names[$cnt]}.aux ${names[$cnt]}.nlo ${names[$cnt]}.nls ${names[$cnt]}-blx.bib ${names[$cnt]}.ilg ${names[$cnt]}.idx ${names[$cnt]}.run.xml ${names[$cnt]}.bcf ${names[$cnt]}.out #${names[$cnt]}.toc
rm -f Ort.idx

while [[ $cnt < $max ]] ; do
    wait ${pids[$cnt]}

if [ 0 -ne $errorflag ]; then
    cat $logs
    echo --------------------------------
    echo errors occured in: $errorstring
    exit 1
    echo --------------------
    echo - all pdfs created -
    echo --------------------
    exit 0

I’ve just come up with the following. It seems to work quite well (on Unix/Linux and Mac):

for name in $( \
    grep '^[:space:]*\\beginpgfgraphicnamed' $SOURCE | \
    sed s'/[:space:]*\\beginpgfgraphicnamed{\(.*\)}/\1/' \
); do
    pdflatex --jobname=$name $SOURCE

An explanation:

It uses grep to filter out all lines that contain \beginpgfgraphicsnamed, but only if there’s only whitespace before the command in the line. This prevents that commented-out commands are also found.

Next, it invokes sed to filter from those lines only the relevant part, i.e. the command argument that’s to be used as the pdflatex jobname. It then uses for to iterate over all these jobnames and calls pdflatex in turn for each of them.

  • 1
    On Ubuntu 14.04 I get the following error: grep: character class syntax is [[:space:]], not [:space:]
    – linello
    Commented Aug 1, 2016 at 10:48
  • @linello Quite correct, it's simply a typo in the answer. Commented Aug 1, 2016 at 10:56
  • How should I call the script? SOURCE is the main.tex file that I need to compile?
    – linello
    Commented Aug 1, 2016 at 11:04
  • @linello exactly. Commented Aug 1, 2016 at 11:06

You must log in to answer this question.

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