# 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
PROCESSOR='$pdflatex=q/xelatex %O %S/'
# Make sure that a target was specified ...
if [ "$1" = "" ]; then
echo "No target name specified"
# ... and that the source file of the target exists.
if [ ! -f $SOURCE ]; then
echo "$SOURCE not found."
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 ]
if [ $HASNOPDF -eq 1 ]; then
diff $TARGET.pdf $TMPNAME.pdf
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.
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
if [ -e $TMPNAME.synctex.gz ]; then
gzcat $TMPNAME.synctex.gz | sed "s/\.$TARGET/$TARGET/g" | gzip - > $TARGET.synctex.gz
sleep 1 # give it time to be killed by a CTRL-C