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

Synapict and gparted – cannot open display

When I try to open synaptic or gparted in Ubuntu 17.10, I run into problems. The error looks like this

No protocol specified
Unable to init server: Verbindung ist gescheitert: Verbindungsaufbau abgelehnt

(synaptic:8832): Gtk-WARNING **: cannot open display: :0

To solve the problem, simple use the following command

xhost +SI:localuser:root

Thanks to https://askubuntu.com/questions/961967/why-dont-gksu-gksudo-or-launching-a-graphical-application-with-sudo-work-with-w

URL link in linux

It is possible to create URL links in linux (well, I only have tested in in nautilus) to open in in browser by clicking.

This goes rather easily, but needs the right contents (link only is not sufficient). One can open a text editor and create a .desktop file. (i named it ethz-link.destkop). Important is the URL parameter

Now step by step

  • Open a text editor, for example gedit
  • Put the following content in the file

[Desktop Entry]
Encoding=UTF-8
Name=ETH Homepage
Type=Link
URL=https://www.ethz.ch
Icon=text-html

  • Change the URL parameter and the Name according to your purpose
  • Save it with the a .desktop extension, for example ethz-link.desktop

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.