#!/bin/bash # # Syntax: mktex [-h] [-s] [-m max_extra_runs] [-d dir1:dir2:...] [filename [...]] # # Purpose: Processes Latex files that use bibliography files. mktex first runs # by latex, followed by bibtex and/or makeindex, and followed by latex # as many times as necessary to resolve all references (with a maximum # of 5 times extra after the first latex-bibtex/makeindex-latex cycle; # use the -m flag to override this setting). # If no file is specified the file ~/bib/bib.tex is processed. # Use the -s option to reduce the intermediate output. # Use the -m option to set the maximum number of extra runs to a # different value than the default one. # Use the -d flag to specify extra directories to be prepended to # TEXINPUTS, BIBINPUTS, and BSTINPUTS. # The -h flag shows the help info for this command. # Created: Apr 22, 1993 by Bart De Schutter # Last revised: Apr 12, 2012 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: latex # # Process options. # latex_option="" bibtex_option="" batch_mode=0 max_extra_runs=5 extra_dirs="" while getopts :hsm:d: option do case "$option" in h) sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 ;; s) latex_option="-interaction=batchmode" bibtex_option="-terse" batch_mode=1 ;; m) max_extra_runs=${OPTARG} ;; d) extra_dirs=${OPTARG} ;; :) echo >&2 echo >&2 "Option -$OPTARG requires an argument." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 echo >&2 exit 1 ;; \?) echo >&2 echo >&2 "Unknown option: $OPTARG" echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 ;; esac done shift $(($OPTIND-1)) if [ -n "${extra_dirs}" ] then TEXINPUTS="${extra_dirs}:$TEXINPUTS" BSTINPUTS="${extra_dirs}:$BSTINPUTS" BIBINPUTS="${extra_dirs}:$BIBINPUTS" fi # # Determine files to be processed. # if [ $# -eq 0 ]; then latex $latex_option ~/bib/bib.tex if [ $? -eq 0 ]; then bibtex $bibtex_option bib latex $latex_option ~/bib/bib.tex fi else for fname in "$@" do # # Run latex and bibtex as many times as required # (subject to max_extra_runs setting). # n_extra_runs=$max_extra_runs file="${fname%.tex}" texfile="${file}.tex" file="${file##*/}" # strip leading path # to get filename in work dir latex $latex_option "$texfile" result=$? if [ $result -eq 0 ]; then grep '\\printindex' "$texfile" | grep -v '^[ \t]*%' >/dev/null 2>&1 # Note: We assume that \printindex command is included in # main file. # For complete generality we should recursively check # all tex files. # Quick fix: just put \printindex in the main # file after \end{document} if [ $? -eq 0 ]; then makeindex "$file" fi grep '^\\bibdata' "${file}.aux" >/dev/null 2>&1 # # Note: We assume that \bibliography command is included in # main file (so \include{references} would give problems). # Quick fix: use \input{references} instead. # For complete generality we should recursively check all # aux files. # if [ $? -eq 0 ]; then bibtex $bibtex_option "$file" fi latex $latex_option "$texfile" result=$? cont=1 while [[ ( $cont -eq 1 ) && ( $result -eq 0 ) ]]; do grep '^LaTeX Warning: Label(s) may have changed.' "${file}.log" \ >/dev/null 2>&1 if [ $? -eq 0 ]; then latex $latex_option "$texfile" else # # Extra test for natbib. # grep '^(natbib) *Rerun to get citations correct.' \ "${file}.log" >/dev/null 2>&1 if [ $? -eq 0 ]; then latex $latex_option "$texfile" else cont=0 fi fi result=$? let n_extra_runs-=1 if [[ $n_extra_runs -eq 0 ]] then cont=0 fi done fi if [[ $n_extra_runs -eq 0 ]] then echo >&2 echo >&2 echo >&2 "WARNING: (Default) number of latex re-runs exceeded for file $texfile" echo >&2 echo >&2 elif [ $result -ne 0 ]; then echo >&2 echo >&2 "ERROR processing $texfile" echo >&2 exit 1 fi done fi