## Insert git log details into a LaTeX document

During lengthy writing intervals, it can become difficult to know exactly what version of a document you're looking at, especially if you're getting notes from multiple folks on printed copies or as PDF annotations. To make this easier to deal with, I like to include the date and git revision in PDFs generated from LaTeX sources. I put mine in the \date of article titles (see below).

If you're using pdflatex to build PDFs from LaTeX documents, you can use the write18 macro coupled with the --enable-write18 switch to execute a command, dump that command's output into a file, and \input that file into your LaTeX document.

Put the command you want into a file, e.g. gitrev.sh (and make it executable):

#!/bin/bash

git log -1 --pretty=format:"%ad \\\\{\\em(rev. %h)}" --date=local


Then call the script from your LaTeX source file using \write18 and dump its output to a file, e.g. \jobname.stamp

\documentclass[english]{article}

\begin{document}

\immediate\write18{./gitrev.sh > \jobname.stamp}
\input{\jobname.stamp}}

\end{document}


Then run pdflatex with the --enable-write18:

pdflatex --enable-write18 mydocument


The latest commit date and hash should appear in your PDF. You can tinker with the git log formatting using the git manual.