Introduction

If you’re working on your thesis and tracking changes via Git, you might want to add some information in the PDF to quickly see which revision a draft was generated from. An example is at the bottom, which depicts my master’s thesis.

This helped me share drafts with friends and my advisor, who gave me feedback and, in turn, allowed me to quickly see which revision the feedback referred to.

An image of a pdf page with a red marker on the top and bottom describing which git revision and at which date and time the pdf was created.

Setup

I assume you’re already using Git to track your changes.

  1. Create a file scripts/git-ref.sh with the following content:
#!/usr/bin/env bash

git describe --tags --dirty --always | tr -d '\n' | sed 's/^v\(.*\)/\1/'

This script gets the current revision and tag (if the current commit is tagged), adds the suffix -dirty if there are untracked changes, and replaces v1.0.0 style tags with 1.0.0.

  1. Make the script executable:
chmod +x ./scripts/git-ref.sh
  1. Add the following to your Makefile
FILE := main
OUT  := build

.PHONY: pdf
pdf: git_ref
	+latexmk -interaction=nonstopmode -outdir="$(OUT)" -pdf -halt-on-error $(FILE)

.PHONY: watch
watch: git_ref
	+latexmk -interaction=nonstopmode -outdir="$(OUT)" -pdf -pvc -halt-on-error $(FILE)

.PHONY: git_ref
git_ref:
	./scripts/get_ref_name.sh > git_ref.tex
  1. Finally, include the following in main.tex:
\usepackage[hhmmss]{datetime}
\renewcommand{\dateseparator}{-}
\usepackage{background}
\IfFileExists{git_ref.tex}{%
    \newcommand{\draft}{\upshape{\texttt{Draft: \input{git_ref.tex} {\yyyymmdddate\today}T\currenttime } } }
    \backgroundsetup{
        angle=0,
        contents={
            \begin{tikzpicture}
                \node[text=red, above=-1cm] at (current page.north) {\draft};
                \node[text=red, below=-1cm] at (current page.south) {\draft};
            \end{tikzpicture}
        },
        opacity=1,
        scale=1
    }
}