myTeX - Make Latex auto-compile on commandline

LaTeX is great. No not the kinky outfits you think about but the (scientific) writing software LaTeX. I love to write these documents in vim because first of all it is easy and second of all I feel like a proper computer scientist doing so. However, switching between vim and commandline to compile documents after every change can be draining. So here is a simple script that will watch your project directory for changing files and recompile after every change. This means that, everytime you type :w in vim, your document will be recompiled.

I have added this as an alias to my .zshrc but you can also make an independent script out of it.

#!/bin/bash
# Check for argument
if [[ $# -eq 0 ]] ; then
    echo "Error: No main latex file found"
    echo "Usage: mytex <LATEX_MAIN_FILE>"
else
    DIR=$(pwd)
    MAIN=$1
    echo "Monitoring directory: $DIR"
    echo "Main file: $MAIN"
    cd $DIR
    while inotifywait -e modify,create,delete -r $DIR
    do
        rubber --pdf $MAIN
        # xelatex $MAIN
    done
fi

I use rubber to compile my documents but you can also just do the plain

xelatex -interaction=nonstopmode $MAIN

Note that I use the nonstopmode to keep the compiler from failing on every error. So if your document fails to compile it will just try to get as far as it can and then waits for new changes.

Happy LaTeX-ing