#!/bin/bash # BASH script for the compilation of LaTeX documents # Carlos Maziero, August 21, 2008 # TODO: # - test presence of commands latex, bibtex, dvips, ps2pdf, psnup # - test options for beamer slides # default options PAPER="a4" # paper size OFFSET="0pt,0pt" # sheet margin offsets for dvips PAGES=1 # how many pages per sheet? REMOVE=1 # remove temporary files OPTIMIZE=1 # optimize PDF output RUNBIBTEX=1 # run BibTeX # location of BibTeX reference files export BIBINPUTS=$HOME/unispace/software/bibtex:. # command line parameters while [ $# -gt 0 ]; do case "$1" in -c) ALL=1; REMOVE=1;; -p) REMOVE=0;; -l) PAPER="letter";; -no) OPTIMIZE=0;; -nb) RUNBIBTEX=0;; -ob) RUNBIBTEX=-1;; -s) SLIDE=1;; -1) PAGES=1;; -2) PAGES=2;; -4) PAGES=4;; -8) PAGES=8;; -*) echo >&2 "This script helps the compilation of LaTeX sources"; echo >&2 "usage: $0 [-l] [-a] [-c] file" ; echo >&2 " -l : use letter paper (default is A4)" ; echo >&2 " -c : complete compilation (latex, bibtex, dvips, ps2pdf)" ; echo >&2 " -p : preserve (do not remove) temporary files" ; echo >&2 " -no : do not optimize the output PDF file" ; echo >&2 " -nb : do not run BibTeX" ; echo >&2 " -ob : only BibTeX (stop just after it)" ; echo >&2 " -s : slides (using the Beamer class)" ; echo >&2 " -1 : 1 page/sheet (default)" ; echo >&2 " -2 : 2 pages/sheet" ; echo >&2 " -4 : 4 pages/sheet" ; echo >&2 " -8 : 8 pages/sheet" ; exit 1;; *) break;; # terminate while loop esac shift done # choosing sheet size for slides if (( $SLIDE )); then DVIPSPAPER="" PS2PDFPAPER="" else DVIPSPAPER="-t$PAPER" PS2PDFPAPER="-sPAPERSIZE=$PAPER" fi # dvips options DVIPSCMD="dvips" DVIPSOPT="-Ppdf -G0 $DVIPSPAPER -O $OFFSET" # sheet size and offset # ps2pdf options (to ensure embedded fonts and PDF 1.4 compatibility) PS2PDFCMD="ps2pdfwr" PS2PDFOPT="-dSAFER -dDELAYSAFER -dCompatibilityLevel=1.4 \ -dPDFSETTINGS=/prepress -dEmbedAllFonts=true \ -dSubsetFonts=true -dMAxSubsetPct=100 $PS2PDFPAPER" # separator line SEPLINE="============================================================" # main input file FILE=`basename $1 .tex` if [ ! -f "$FILE.tex" ]; then echo "$FILE: file not found or not a .tex file" exit 1 fi # first LaTeX pass (can produce fatal errors) echo "Running LaTeX (1st pass) ..." if ! latex $FILE < /dev/null; then echo "ERROR in $FILE: LaTeX compilation failed, aborting..." exit 1 fi echo $SEPLINE if (( $ALL )); then # should run BibTeX? if [ $RUNBIBTEX != 0 ]; then # does the source files have undefined citations? if egrep 'Citation .+ undefined' $FILE.log > /dev/null; then # references processing by BibTex echo "Running BibTeX ..." if ! bibtex $FILE; then echo "ERROR in $FILE: BibTeX processing failed, aborting..." exit 1 fi # stop here if running BibTeX only if [ $RUNBIBTEX == -1 ]; then exit 0 fi echo $SEPLINE # second LaTeX pass, to read bibtex references echo "Running LaTeX (2nd pass) ..." latex $FILE echo $SEPLINE fi fi # third LaTeX pass, to solve backward bibtex references echo "Running LaTeX (3rd pass) ..." latex $FILE echo $SEPLINE fi # conversion DVI -> PS echo "Converting DVI to PS ..." $DVIPSCMD $DVIPSOPT $FILE 1>/dev/null 2>&1 #echo $SEPLINE # psnup processing (to get multiple pages/sheet) if [ $PAGES != "1" ]; then echo "Putting $PAGES pages per sheet ..." # beamer: psnup -$PAGES -W128mm -H96mm -p$PAPER -d -m 1cm input.ps output.ps psnup -$PAGES -p$PAPER -d -m 0cm $FILE.ps $FILE.tmp-buildfile mv $FILE.tmp-buildfile $FILE.ps echo $SEPLINE fi # conversion PS -> PDF echo "Converting PS to PDF ..." $PS2PDFCMD $PS2PDFOPT $FILE.ps # PDF optimization if (( $OPTIMIZE )); then echo "Optimizing the PDF file ..." pdfopt $FILE.pdf $FILE.pdf.tmp mv $FILE.pdf.tmp $FILE.pdf fi # remove temporary files if (( $REMOVE )); then echo "Cleaning temporary files ..." # find . -name \*~ -exec rm {} \; find . -name \*.aux -exec rm {} \; rm -f $FILE.brf $FILE.toc $FILE.bbl $FILE.blg $FILE.log $FILE.ps rm -f $FILE.dvi $FILE.lof $FILE.lot $FILE.nav $FILE.snm $FILE.out texput.log rm -f *.log *.bbl *.blg fi