Over the past two years, the PDF version of Bartosz Milewski’s Category Theory for Programmers became a highly-successful open-source book, which was adapted to other programming languages, such as Scala and OCaml. Unfortunately, building this 400+ page PDF from LaTeX sources in multiple editions took a significant amount of time, sometimes upwards of 15 minutes. One of the reasons was, all the code snippets are loaded from external code files (so that they can be easily adapted to other programming languages), and they have to be compiled each time in a format LaTeX understands.
I recently explored some possibilities to reduce the time it takes for the snippets to build, and I’m happy to report the results: a 60% improvement overall! Big thanks to muzimuzhi from the minted github, who generously helped me to arrive at the solution below.
Here’s a quick summary of my changes, that resulted in reducing my Travis CI builds from 14-16 minutes to mere 6!
Changes to minted
Configure minted cache parameter
cachedir
to output to a single directory (e.g._minted-caches
) in all your.tex
files (or in a common preamble)Pass the
-jobname
argument tolatexmk
with the same name for all builds (e.g.-jobname=ctfp
)- Note: this causes the output PDF filename to be
{jobname}.pdf
, you will have to tweak your Makefile/build script to rename/copy this filename to the right location with a new name.
- Note: this causes the output PDF filename to be
Override the
\minted@cleancache
command to do nothing, to prevent it from cleaning out the unused cache files:% prevents cleaning up the cache at the end of the run (needed to keep the unused caches, generated by other editions)
\makeatletter
\renewcommand*{\minted@cleancache}{}
\makeatother(optional) If you’re using a
-pdf
flag in yourlatexmk
command, consider using-pdfxe
instead - it will cause latexmk to generate the pdf file for the last run only, generating intermediate xdv files (hence saving the time of xdv-to-pdf conversion) for other runs.
Changes to Travis CI
If you’re building the PDF on a CI server, such as Travis CI, configure caching for the minted caches, to enable retrieving them between builds. Read the caching documentation for Travis CI for details.