Skip to content

Instantly share code, notes, and snippets.

@kenielf
Created March 13, 2024 14:54
Show Gist options
  • Save kenielf/7d319efc35654f2ae97612f1f5cfcc58 to your computer and use it in GitHub Desktop.
Save kenielf/7d319efc35654f2ae97612f1f5cfcc58 to your computer and use it in GitHub Desktop.
Compile Latex Documents On Change
#!/usr/bin/env sh
# vi: ft=sh
if ! (command -v lualatex >/dev/null 2>&1); then
printf "CLTEX: %s\n" "Please install lualatex!"
exit 1
fi
# Make sure the file exists
if ! [ -f "${1}" ]; then
printf "CLTEX: %s\n" "Target file does not exist!"
exit 1
else
printf "CLTEX: %s\n" "Target file found - proceeding."
fi
# Pre-run cleaning step
# Remove any existing auxiliary files, if they already exist
#for file in ./*.{aux,log}; do
# rm "${file}"
#done
# Start loop
last_change=""
current_change=""
while true; do
# Stat file contents
current_change="$(stat "${1}" | grep -oP '(?<=Change: )[0-9\.\-_:\s]+$')"
# Do something if change gets detected
if ! [ "${current_change}" = "${last_change}" ]; then
# Compile Document
lualatex --halt-on-error --shell-escape "${1}"
lualatex --halt-on-error --shell-escape "${1}"
# Remove all auxiliary files that were created when building
#for file in ./*.{aux,idx,ilg,ind,log,toc}; do
# rm -v "${file}"
#done
fi
# Update the last change
last_change="${current_change}"
# Wait one second before statting again
sleep 1;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment