Skip to content

Instantly share code, notes, and snippets.

@whophil
Last active March 14, 2016 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whophil/7752874fa5f2f7f9abf0 to your computer and use it in GitHub Desktop.
Save whophil/7752874fa5f2f7f9abf0 to your computer and use it in GitHub Desktop.
Shell script to run latexdiff against a TeX file (with specified commit hash) from the current repository.
#!/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 CTRADITIONAL --append-context2cmd="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
@whophil
Copy link
Author

whophil commented Mar 11, 2016

Seems to have some issues with \abstract{} the environment (abstract text is currently not marked up).
There is some discussion of this here https://www.researchgate.net/post/How_can_I_track_changes_in_LaTeX_especially_abstractenvironment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment