[Tex/LaTex] Custom format file: How to automate the generation of the “precompiled preamble” whenever one of the files that are used there are changed

automationformat-files

I would like to speed up compilation of my LaTeX documents using the precompiled preamble approach, and I was wondering whether any of the Tools for automating document compilation have support for this.

The main idea is:

  • split the document into a "static" and a "dynamic" part, preamble.tex and main.tex

  • create a custom format file main.fmt from the static part using

      pdflatex -ini -jobname=main "&pdflatex preamble.tex\dump"
    
  • compile your document using

      pdflatex -fmt main main
    

As a consequence, only the contents of main.tex are processed on "regular" compilation, thus accelerating the build. However, if preamble.tex or any of the files used there changes, the format file will have to be rebuilt.
I would like to automate this: Rebuild main.fmt if and only if necessary. Of course, main.tex would also be recompiled if the static part (=the format file) has changed and/or if the contents or includes of the dynamic part have changed.

I have tried latexmk and scons, both excellent tools otherwise, but no luck so far.

EDIT: Philipp's answer reminded me that portability between platforms cannot be taken for granted. I'm running Linux, but I'd be most interested in fully portable solutions.

MWE

preamble.tex:

\documentclass{article}
\usepackage{lipsum}

main.tex:

\begin{document}
  \lipsum
\end{document}

Best Answer

See my solution using latexmk at Latexmk can't see a dependency on a .fmt format file. It now tracks changes in the files used to make the format file.