Automating Latex Workflows: Tips And Tricks
Streamlining Document Creation
LaTeX documents require significant upfront formatting work to ensure consistency across sections. By using templates that standardized heading styles, captions, equations, and more, new documents can build on existing formatting guidelines to accelerate setup. Popular packages like memoir and koma-script provide templating functions out-of-the-box to encourage reuse.
Builder tools like arara further optimize automation by codifying actions required to generate documents, such as compiling the LaTeX file, running BibTeX, and creating final PDF output. By adding directives to an existing LaTeX file, arara can interpret and run commands in the correct sequence. The directives support rules provided by arara as well as custom user-defined rules for maximum flexibility.
% arara: pdflatex
% arara: bibtex
% arara: pdflatex
% arara: pdflatex
This arara setup demonstrates rules that will run pdflatex three times to resolve references and bibliography inclusion, with an intervening bibtex run. Builder tools lead to cleaner LaTeX file content and move complexity into specialized rule declarations.
Version Control Integration
Version control systems provide benefits regarding tracking changes over time and enabling multi-user collaboration. Systems like Git and Mercurial can integrate deeply with LaTeX workflows through committed textual diffs and using branches for major revisions. Storing documents in repositories allows users to inspect the full change history alongside the current file state.
Hosted solutions such as Overleaf and ShareLaTeX combine LaTeX editing and compilation with built-in version control, real-time collaboration, and project management support. By handling repository operations behind the scenes, they offer accessible control without needing to run CLI commands or configure local tools. Users focus on document content rather than technical processes. Sample configuration for pushing local LaTeX project files to a GitHub repository is shown below.
git init my-latex-project
cd my-latex-project
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/user/my-latex-project.git
git push -u origin master
Continuous Integration Pipelines
CI services automate building, testing, and delivery of software projects through orchestrated pipelines. The same techniques apply for automating LaTeX document processing. Upon git push events, CI tools can build PDF outputs and validate project state every commit rather than relying on local environments.
LaTeX projects configure on CI platforms like Travis CI and CircleCI using .yml files that declare the environment, dependencies, compile commands, notifications, and more. Testing components become validation checks on tables of contents, bibliographies, linked references, etc.
language: latex
latex:
- l3kernel
- hyperref
script:
- pdflatex -interaction=nonstopmode main.tex
- bibtex main
- pdflatex -interaction=nonstopmode main.tex
- pdflatex -interaction=nonstopmode main.tex
This Travis CI snippet performs a full LaTeX build comprising three pdflatex runs and bibtex. The CI checks for errors on every push and can automatically deploy successful builds to hosting platforms.
Distributing and Publishing
LaTeX documents often require output in specialized formats like PDF for print usage, ePub for mobile readers, and HTML for web publishing. Generating these builds manually using multiple CLI commands becomes repetitive. Makefiles codify compile processes for automation akin to arara directives but applicable for multiple file types like docker-compose handles environment configuration.
The following Makefile excerpt compiles all .tex files into .pdf outputs placing them into a build
folder, concatenates section outputs for a full manuscript, builds epub from markdown sources, and generates HTML rendered math derivations with MathJax support.
build:
mkdir build
$(foreach chap,$(CHAPS),pdflatex $(chap).tex -output-directory=build;)
pdflatex manuscript.tex -output-directory=build
book.epub: $(SRC)
pandoc -f markdown -t epub3 -o book.epub $(SRC)
derivations.html: equations/*.tex
for i in $$(ls equations/*.tex); do latexmlmath $$i > $${i%.tex}.html; done
Make streamlines multi-format publishing requirements into a simple interface no more complex than core LaTeX use.
Additional Resources
Custom LaTeX packages like SCons provide programmatic build capabilities using Python. JOBAD adapts continuous delivery techniques by triggering document recompilation upon source file changes. latexmk manages build dependencies and compilation reruns as needed. For further reading, the following texts provide in-depth coverage.
- LaTeX in 24 Hours
- Guide to LaTeX Automation
- Continuous Documentation with CiTEX