Tracking Git commits info in LaTeX docs

Last time we wrote how to use Git + TeXStudio (especially with Overleaf) bundle to collaborate on papers and track changes. During the work it may be useful to show information about current commit/author/(time of change) directly in the document.

We found a few ways to do it.

The simplest one

The most straightforward way to include commit info is just to include it from git working files:

\begin{document}
\preprint{\textcolor[rgb]{0.00,0.50,0.75}{{\input{.git/refs/heads/master}}}}

with result like

It allows always to have some unique hash, but nothing more. Note, that file .git/refs/heads/master has no extension, so additional tricks depending on your LaTeX system may be needed just to include file without extension in the file name.

gitinfo2

The most useful way to my opinion is to include a gitinfo2 latex package. Shortly speaking, one needs to add it in scope of the latex document with needed options:

...
\usepackage[mark]{gitinfo2}

\begin{document}
...

mark option shown here generates the following footer:

One can customize message by predefined macroses in gitinfo2. For examle, with the \preprint command from revtex4-1 style, short version info may be the following:

...
\usepackage[mark]{gitinfo2}
\begin{document}
\preprint{\textcolor[rgb]{0.00,0.50,0.75}{{\texttt{Draft \gitAbbrevHash{} by \gitCommitterName{} on \gitCommitterDate}}}}

with

See other possible options and macroses in the documentation for gitinfo2. All this pretty output requires a few preliminary work. First you need to go to .git/hooks folder and copy a few scripts into it:

# inside <repo>/.git/hooks !!
cp /usr/share/texlive/texmf-dist/doc/latex/gitinfo2/post-xxx-sample.txt post-checkout
cp post-checkout post-commit
cp post-checkout post-merge

Do not forget about permissions and adding execution flag if needed (all three should be executable by your user). Then return to the top level of your repository and run command

git checkout

once in your repository. Copying of scripts can be automatized. Just copy them into your git templates folder, /usr/share/git-core/templates/hooks in my case. Now new repos will be created with these new scripts and gitinfo2 will be used without any manual preparations.

write18 option

Using write18 option of pdflatex one can execute shell options in latex document in the following way:

\begin{document}

\makeatletter
\immediate\write18{git log --pretty=format:"\@percentchar h \@percentchar ad \@percentchar an" --date=short > \jobname.info}
\makeatother
\preprint{\textcolor[rgb]{0.00,0.50,0.75}{{\input{\jobname.info}}}}

Then, run of

pdflatex --enable-write18 document.tex

will result in

Sources and useful links

  1. https://ctan.org/pkg/gitinfo2
  2. https://mimischi.github.io/blog/latex-with-git-revisions/
  3. https://balist.es/blog/2017/01/27/git-latex-gitinfo-draft-watermark/
  4. https://tex.stackexchange.com/questions/75336/inserting-git-commit-date-without-hooks
  5. https://tex.stackexchange.com/questions/17462/write18-pass-through-to-shell

Leave a Reply