Make a thesis with latex – with latexmk and Makefile

I’m a fan of automatic builds. A lot of my gitlab/github projects are configured to do continuous integration (CI), which checks for errors in the projects.

This can also be used to automatically build your latex projects, in order to have a PDF for every build. Nice, isn’t it.

But let us not reach to the heaven until we did some dirty work by ourselfs. Builds must work on your computer first, then in the cloud.

Local builds

To build local PDF, I use latexmk. It recognizes easily when a latex file has been modified and the PDF not. Let’s assume you have a paper called main.tex which uses a BibTeX reference file (called reference.bib, but that doesn’t matter for now). For the build, you usually must do

pdflatex main.tex
bibtex main.aux
pdflatex main.tex
(pdflatex main.tex)

There you go with builds.

latexmk does that for you. The build command is simple as

latexmk -pdf main.tex

Now, I have some SVG which need conversion to PDF before the build. The tool for that is cairosvg, a python program. You can get it on their webpage.

Now, it would be cumbersome to convert every SVG file even tough it has not changed. For that, we use the magic of the Makefile 🙂

When executing make it checks whether the SVG files have changed and only builds files which are newer than their corresponding PDF. Nice, right? To do that, i have a Makefile like this

# My Makefile
SVGS := $(wildcard chapter_*/images/*.svg)
PDFIMGS := $(patsubst %.svg, %.pdf, $(SVGS))

default: all

all: svg2pdf

svg2pdf: $(PDFIMGS)

%.pdf: %.svg
	cairosvg --format pdf --output $@ $<

This is a typical Makefile. When executing make (or make all or make svg2pdf), it converts the only modified SVGs. Who this works, I have to lead you to other sources.

OK, so I can use latexmk to create my PDFs out of the tex files. And the Makefile to convert SVGs. Why not bring everything together, right?

There we go

SVGS := $(wildcard chapter_*/images/*.svg)
PDFIMGS := $(patsubst %.svg, %.pdf, $(SVGS))

default: all

all: svg2pdf mk
	
svg2pdf: $(PDFIMGS)

%.pdf: %.svg
	cairosvg --format pdf --output $@ $<

mk:
	latexmk
	
clean:
	latexmk -c

I can only create the PDFs when executing make mk or only convert SVG when executing make svg2pdf. And when I want to clean the files from the latex compilation like aux and log files, I can use the command latexmk -c or with our Makefile make clean

Summary

We performe a local build for our latex project with

  • latexmk to build tex files multiple times, especially when using BibTex files.
  • cairosvg to convert SVG files to PDFs.
  • make with a Makefile to performe dependency build of cairosvg.

Gitlab build

I use a simple gitlab configuration for my build in the cloud, which basically does the same thing as on the local machine. As there is no machine I set up myself in the cloud, I use a docker build. The file for the gitlab configuration is called .gitlab-ci.yml.

compile:
  stage: build
  image: daesters/latex
  script:
    - latexmk -c
    - ./convertsvg2pdf.sh
    - latexmk -pdf
    #- latexmk
  artifacts:
    paths:
      - ./dissertation.pdf

Cheers

Electronic Labbook

Looking for a good electronic labbook. Have a look at Elog. We use it in our research group. It is highly customizable and can be automated as well. Give it a try.