Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Forked from ytsutano/LaTeX.sh
Last active August 29, 2015 14:26
Show Gist options
  • Save carlosdelfino/af68a9a66eb5f37bc88c to your computer and use it in GitHub Desktop.
Save carlosdelfino/af68a9a66eb5f37bc88c to your computer and use it in GitHub Desktop.
TextWrangler: automated LaTeX compilation.
#!/bin/bash
# Change the current directory to the document path.
cd "${BB_DOC_PATH%/*}"
# Update PATH.
PATH=$PATH:/usr/bin/:/usr/local/bin/
PATH=$PATH:/usr/local/bin:/usr/X11/bin:/usr/texbin:/opt/local/bin
# Pre-compute the file names used.
texfile=${BB_DOC_PATH##*/}
dvifile=`echo $texfile | sed -e 's/\.[^\.]*/.dvi/'`
pdffile=`echo $texfile | sed -e 's/\.[^\.]*/.pdf/'`
logfile=`echo $texfile | sed -e 's/\.[^\.]*/.log/'`
tocfile=`echo $texfile | sed -e 's/\.[^\.]*/.toc/'`
auxfile=`echo $texfile | sed -e 's/\.[^\.]*/.aux/'`
outfile=`echo $texfile | sed -e 's/\.[^\.]*/.out/'`
bbfile=`echo $texfile | sed -e 's/\.[^\.]*/.bb/'`
# Make sure the input file is a TeX document.
echo "$texfile" | grep "\.tex$" > /dev/null
if [ $? -eq 1 ]; then
say "Invalid file type." &
growlnotify -a 'TextWrangler' -n 'LaTeX' \
-t 'Invalid File Type' -m "$texfile"
exit 1
fi
# Check the document language.
grep 'documentclass.*\(jarticle\|jreport\|jbook\)' "$texfile" > /dev/null
if [ $? -eq 0 ]; then
# Japanese: use platex.
LATEX_CMD='platex --kanji=utf8 -shell-escape'
else
# English: use latex.
LATEX_CMD='latex -shell-escape'
fi
# Compile and generate a DVI file.
say "Compiling..." > /dev/null &
for ((i=0; i<=4; i+=1)); do
$LATEX_CMD "$texfile" > /dev/null
if [ $? -gt 0 ]; then
say "Error." &
cat "$logfile" | grep '^[!l]' | growlnotify -a 'TextWrangler' \
-n 'LaTeX' -t 'LaTeX Compilation Failed'
rm -f "$dvifile"
rm -f "$tocfile"
rm -f "$auxfile"
rm -f "$outfile"
rm -f "$bbfile"
exit 1
fi
done
# Generate PDF from DVI.
dvipdfmx -o "$pdffile" -O 4 -E "$dvifile" 2> "$logfile"
if [ $? -ne 0 ]; then
say "PDF Error." &
cat "$logfile" | growlnotify -a 'TextWrangler' \
-n 'LaTeX' -t 'dvipdfmx Failed'
rm -f "$dvifile"
rm -f "$tocfile"
rm -f "$auxfile"
rm -f "$outfile"
rm -f "$bbfile"
exit 1
fi
# Remove intermediate files.
rm -f "$dvifile"
rm -f "$tocfile"
rm -f "$logfile"
rm -f "$auxfile"
rm -f "$outfile"
rm -f "$bbfile"
# Open in Preview.app.
open -a Preview "$pdffile"
# Notify that everything is done.
say "Done." > /dev/null &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment