Skip to content

Instantly share code, notes, and snippets.

@klmr
Created July 28, 2010 18:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klmr/495725 to your computer and use it in GitHub Desktop.
Save klmr/495725 to your computer and use it in GitHub Desktop.
Continuous build file for LaTeX
#!/bin/bash
# Author: Konrad Rudolph
# Original idea: Paul Biggar
#
# Usage: texit [--latex] target
#
# --latex: Use pdflatex instead of xelatex as the processor. Optional
# target: The name of the target (i.e. the source file without trailing `.tex`)
ulimit -t 10 # Sometimes pdflatex gets stuck.
# If the script is called as `texit --latex source` then use pdflatex as the
# processor instead of the default (xelatex).
case $1 in
--latex)
PROCESSOR=''
shift
;;
*)
PROCESSOR='$pdflatex=q/xelatex %O %S/'
;;
esac
# Make sure that a target was specified ...
if [ "$1" = "" ]; then
echo "No target name specified"
exit 1
fi
TARGET=$1
SOURCE=$TARGET.tex
SRCDIR=$(pwd)
TMPNAME=.$TARGET
# ... and that the source file of the target exists.
if [ ! -f $SOURCE ]; then
echo "$SOURCE not found."
exit 1
fi
while [ 1 ]; do
# Compile a different file so that the PDF doesn't reload mid-compile.
# Also, this prevents that the viewer crashes (Skim for one occasionally
# does this) when there were compilation errors.
cp $SOURCE $TMPNAME.tex
latexmk -e "$PROCESSOR" -pdf -silent $TMPNAME > /dev/null
# Preserve the log file for rubber-info, regardless of the compilation's
# success, so that the user can review the messages.
# The call to `sed` fixes the references to the source file, since we
# compiled a *different* file. Fixing the references is important so that
# editors (such as Vim) can map errors correctly to their locations in the
# source file.
sed "s/\.$TARGET/$TARGET/g" < $TMPNAME.log > $TARGET.log
# Only copy the rest if the compile was successful and there were changes.
if [ -e $TMPNAME.pdf ]; then
[ ! -e $TARGET.pdf ]
HASNOPDF=$?
if [ $HASNOPDF -eq 1 ]; then
diff $TARGET.pdf $TMPNAME.pdf
OUTPUTDIFFERS=$?
else
OUTPUTDIFFERS=0
fi
if [ $HASNOPDF -eq 0 -o $OUTPUTDIFFERS -ne 0 ]; then
# Do NOT `rm` the target, the viewer (Skim) cannot deal with this.
# This may have to be changed for other viewers.
#rm $TARGET.pdf
cp $TMPNAME.pdf $TARGET.pdf
# Forward search synchronization, if any.
# As for the log file, we once again need to fix the references to
# the source file.
if [ -e $TMPNAME.pdfsync ]; then
sed "s/\.$TARGET/$TARGET/g" < $TMPNAME.pdfsync > $TARGET.pdfsync
fi
if [ -e $TMPNAME.synctex.gz ]; then
gzcat $TMPNAME.synctex.gz | sed "s/\.$TARGET/$TARGET/g" | gzip - > $TARGET.synctex.gz
fi
fi
fi
sleep 1 # give it time to be killed by a CTRL-C
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment