Skip to content

Instantly share code, notes, and snippets.

@maxanier
Created November 18, 2025 09:39
Show Gist options
  • Select an option

  • Save maxanier/4655537dd6f8eeacde2b8f5220329e91 to your computer and use it in GitHub Desktop.

Select an option

Save maxanier/4655537dd6f8eeacde2b8f5220329e91 to your computer and use it in GitHub Desktop.
Tikz externalize automatic png conversion with LuaTex and ImageMagick
REM First argument is image name (or rather path relative to out/ director), second is tex code
setlocal enabledelayedexpansion
REM copy arg and replace / with \
set "JOB=%~1"
set "JOB=!JOB:/=\!"
lualatex -shell-escape -file-line-error -halt-on-error -output-directory=out -interaction=batchmode -jobname %1 %2
if not "%ERRORLEVEL%"=="0" (
echo "Failed compilation"
if exist "out\%JOB%.pdf" del /f /q "out\%JOB%.pdf"
if exist "out\%JOB%.png" del /f /q "out\%JOB%.png"
exit /b %errorlevel%
)
REM If failed, abort and return error
REM If a third argument png is given convert the first pdf page to png using ImageMagick and remove any alpha for PDF/A-1B compatibility.
REM The tilde character is used to remove any enclosing quotation marks
IF "%~3"=="png" (
magick -density 600 "out/%~1.pdf[0]" -background white -alpha remove -alpha off "out/%~1.png"
if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%
)
#!/usr/bin/env bash
# First argument is image name (path relative to out/ directory), second is tex file, third optional "png"
set -euo pipefail
echo lualatex -shell-escape -file-line-error -halt-on-error -output-directory=out -interaction=batchmode -jobname "$1" "$2"
# run lualatex
lualatex -shell-escape -file-line-error -halt-on-error -output-directory=out -interaction=batchmode -jobname "$1" "$2"
rc=$?
if [ $rc -ne 0 ]; then
rm -f "out/${1}.pdf"
rm -f "out/${1}.png"
exit $rc
fi
# If third argument is "png", convert first PDF page to PNG and remove alpha -> necessary for PDF/A-1B
if [[ "${3-}" == "png" ]]; then
magick -density 600 "out/${1}.pdf[0]" -background white -alpha remove -alpha off "out/${1}.png"
fi
%== Placed in local environment file for system dependent configuration ===================
%If defined, use tikz externalize. Either "list and make" on linux or "convert with system call"
\def\myExternalizeTikz{convert with system call}
%Luatex compile script path - Adjust depending on Windows or Linux
\def\myLuatexScriptPath{scripts/compile_image_luatex.bat}
%===========================================================================================
\usetikzlibrary{external}
\ifx\myExternalizeTikz\undefined
\else
\ifx\myLuatexScriptPath\undefined
\GenericError{Missing def}{When myExternalizeTikz is defined, myLuatexScriptPath must be given as well}{tikz_config.tex}{Custom}
\fi
\tikzexternalize[
prefix=out/tikz/,
mode=\myExternalizeTikz,
system call={"\myLuatexScriptPath" "\image" "\texsource" }
]
\fi
\tikzset{
% Defines a custom style which generates a png (without alpha channel) from the pdf and includes the png.
% Use this for very high density images that otherwise slowdown PDF rendering
% Activate by \tikzset{png export} before the tikzpicture
png export/.style={
external/system call/.append={ png }, %Append png flag to the system call. The custom luatex script used will do PDF->PNG conversion then
/pgf/images/external info, %Export dimensions to .dpth file for use in the include code below
/pgf/images/include external/.code={%
\includegraphics
[width=\pgfexternalwidth,height=\pgfexternalheight]
{##1.png}%
},
}
}

TODO

This is helpful for images with insane details that slow down PDF rendering.

Can compile main document with PdfLaTex, only images are compiled with LuaTex (because it does not have problems with Tex memory limitations.)

Of course need magick and lualatex in PATH.

\usepackage{luatex85} %Make luatex compatible with the pdflatex command below \pdfminorversion=4 % For PDFA-1B (PDF 1.4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment