Converting LaTeX TikZ figures to images

This note shows how to externalize TikZ figures from a LaTeX project into high-DPI images (PNG and JPEG), plus a small automation snippet to batch-generate outputs. The workflow relies on tikzexternalize, pdflatex with --shell-escape, and ImageMagick conversions.

1) Add the tikzexternalize preamble

Place this before \begin{document} in your LaTeX source:

\usepgfplotslibrary{external}
\tikzexternalize[mode=list and make]
\makeatletter
\tikzset{
    external/mode=list and make,
    external/new make rule/.code args={make #1; ext .#2}{%
        \tikzexternalwritetomakefile{}%
        \tikzexternalwritetomakefile{ALL_FIGURES_#2=\tikzexternal@DOLLARchar(ALL_FIGURE_NAMES:\tikzexternal@PERCENTchar=\tikzexternal@PERCENTchar .#2)}%
        \tikzexternalwritetomakefile{}%
        \tikzexternalwritetomakefile{#1: \tikzexternal@DOLLARchar(ALL_FIGURES_#2)}%
        \tikzexternalwritetomakefile{\tikzexternal@TABchar @echo All #2 images exist now. Use make -B to re-generate them.}%
        \tikzexternalwritetomakefile{}%
    },
    external/add fig rule/.code args={.#1 depends: .#2,cmd:#3}{%
        \tikzset{
            external/system call/.add={}{\noexpand\@firstoftwo ^^J},
            external/system call/.add={}{\noexpand\@firstoftwo ^^J\image.#1: \image.#2},
            external/system call/.add={}{^^J#3}
        }
    }
}
\makeatother
\tikzset{
    external/new make rule={%
        make allimagespng; ext .png
    },
    external/add fig rule={%
        .png depends: .pdf,cmd:convert -density 300 "\image.pdf" "\image.png"
    },
    external/new make rule={%
        make allimagesjpeg; ext .jpeg
    },
    external/add fig rule={%
        .jpeg depends: .pdf,cmd:convert -density 300 "\image.pdf" "\image.jpeg"
    }
}

2) Compile once with shell-escape

Run a full compile so TikZ writes a Makefile with figure targets. In TeXworks with MiKTeX you can pass either of these arguments:

  • --pdf --synctex=1 --tex-option=--shell-escape $fullname
  • --shell-escape

After this pass, the PDF will not yet show figures because they are externalized.

3) Build the figures via make

Open the generated Makefile and run only the pdflatex rules it defines (or simply make). TikZ will emit each figure as *.pdf in the working directory.

4) Convert PDFs to images

Use ImageMagick (via convert) or pdftoppm to create high-DPI images. For maximum quality, convert at 600–6000 DPI depending on your target. Example with pdf24 or pdftoppm:

pdftoppm -png -r 600 figure.pdf figure
pdftoppm -jpeg -r 600 figure.pdf figure

Optional: batch PowerShell helper

This snippet regenerates all externalized figures and converts them to PNG and JPEG. Adjust the job name to your main LaTeX file and figure count.

function Generate-PdfFigure {
    param ([int]$FigureNumber)
    $jobName = "current_project-figure$FigureNumber"
    Write-Host "Generating PDF for figure $FigureNumber..."
    pdflatex -shell-escape -interaction=batchmode -jobname $jobName "\def\tikzexternalrealjob{Current_Project}\input{Current_Project}"
}

function Convert-PdfFigure {
    param ([int]$FigureNumber)
    $pdfFile = "figure-$FigureNumber.pdf"
    $pngFile = "project-figure-$FigureNumber.png"
    $jpegFile = "project-figure-$FigureNumber.jpeg"
    Write-Host "Converting $pdfFile to PNG..."
    pdftoppm -png -r 600 $pdfFile $pngFile
    Write-Host "Converting $pdfFile to JPEG..."
    pdftoppm -jpeg -r 600 $pdfFile $jpegFile
}

for ($i = 0; $i -le 106; $i++) {
    try {
        Generate-PdfFigure -FigureNumber $i
        Convert-PdfFigure -FigureNumber $i
        Write-Host "Successfully processed figure $i"
    }
    catch {
        Write-Host "Error processing figure $i : $_" -ForegroundColor Red
    }
}