Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Produces a diff PDF of a LaTeX repo against a specified hash using latexdiff.
#!/usr/bin/env bash
# Shell script to run latexdiff against the same file from a specified commit hash.
# Example:
# ./git-latexdiff.sh main.tex 12dfa1a
# $1 is the name of the old tex file (relative to the remote repo)
# $2 is the hash of the commit that we'd like to compare against
# paths
DIFFDIR=$(mktemp -du ./git-latexdiff.XXXXXX)
ORIDIR=`pwd`
# command line arguments
TEX=$1
VER=$2
# get the name of the TeX file without extension
TEXNAME=$(basename "$TEX")
EXTENSION="${TEXNAME##*.}"
TEXNAME="${TEXNAME%.*}"
# if no hash is specified, use head
if [ -z "$2" ];then
VER='HEAD'
fi
# if no file is specified, guess from file with `begin{document}''
if [ -z "$1" ];then
TEX=`grep 'begin{document}' *.tex | grep -m 1 begin | cut -d\: -f1`
fi
# clone current repo into DIFFDIR, checkout version hash
git clone . "$DIFFDIR/"
cd "$DIFFDIR"
git checkout $VER
git reset --hard
# run latex, bibtex, latex, pdflatex (for refs)
latex -interaction=nonstopmode "$TEX" $OPTIONS
bibtex main.aux
latex -interaction=nonstopmode "$TEX" $OPTIONS
pdflatex -interaction=nonstopmode -shell-escape "$TEX" $OPTIONS
# run latexdiff on tex files (with flatten option)
latexdiff --flatten "$TEX" "$ORIDIR/$TEX" > latexdiff.tex
# run latex, bibtex on latexdiff
latex -interaction=nonstopmode latexdiff.tex $OPTIONS
bibtex latexdiff.aux
# run latexdiff on bbl files
latexdiff --t CFONT --append-textcmd="abstract" $TEXNAME.bbl $ORIDIR/$TEXNAME.bbl > latexdiff.bbl
# run latex, pdflatex to get final product
latex -interaction=nonstopmode latexdiff.tex $OPTIONS
pdflatex -interaction=nonstopmode -shell-escape latexdiff.tex $OPTIONS
# move latexdiff to the original directory
mv latexdiff.pdf $ORIDIR/latexdiff-$2.pdf
cd $ORIDIR
# rm -rf $TMPDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment