public
Last active

Continuous build file for LaTeX

  • Download Gist
texit
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.