Skip to content

Instantly share code, notes, and snippets.

@ph00lt0
Forked from alexnederlof/watch.sh
Last active March 9, 2022 13:17
Show Gist options
  • Save ph00lt0/ec3b8153c10524b9c21e6a42f306efc9 to your computer and use it in GitHub Desktop.
Save ph00lt0/ec3b8153c10524b9c21e6a42f306efc9 to your computer and use it in GitHub Desktop.
Auto recompile Latex in the background on OS X
#!/bin/bash
###
# A Script that automatically recompiles your
# Latex in the background on Mac OS X.
###
# Options
BIN_PATH=/usr/texbin
FILE=main
WATCH_FILE=chapters
# The script
PREVIOUS=""
while [ true ] ; do
# Calculate any difference using crc32. CRC is nice and quick.
NOW=`crc32 $WATCH_FILE.tex`
if [ ! "$NOW" == "$PREVIOUS" ]; then
echo "Diff!"
PREVIOUS=$NOW
# Run first pdf comile
$BIN_PATH/pdflatex -halt-on-error $FILE.tex
if [ $? -eq 1 ]; then # If error code
echo -e "\033[31m Compilation error\033[0m"
# Stop and wait until changed again
continue
fi;
# Compile the bibtech.
for aux in `ls *.aux`; do
$BIN_PATH/bibtex $aux
done
# Compile latex twice. Just because its a stupid language.
$BIN_PATH/pdflatex -halt-on-error $FILE.tex > /dev/null
$BIN_PATH/pdflatex -halt-on-error $FILE.tex > /dev/null
#Open with Preview in the background
open -g $FILE.pdf
fi
sleep 1;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment