lib/Tools/latex
author urbanc
Sat May 24 02:19:09 2008 +0200 (2008-05-24)
changeset 26979 c58778bdf146
parent 26954 3a3816ca44bb
child 28650 a7ba12e0d3b7
permissions -rwxr-xr-x
fixed improper handling of return code (pdf and ps.gz formats)
     1 #!/usr/bin/env bash
     2 #
     3 # $Id$
     4 # Author: Markus Wenzel, TU Muenchen
     5 #
     6 # DESCRIPTION: run LaTeX (and related tools)
     7 
     8 
     9 PRG="$(basename "$0")"
    10 
    11 function usage()
    12 {
    13   echo
    14   echo "Usage: $PRG [OPTIONS] [FILE]"
    15   echo
    16   echo "  Options are:"
    17   echo "    -o FORMAT    specify output format: dvi (default), dvi.gz, ps, ps.gz,"
    18   echo "                 pdf, bbl, idx, sty, syms"
    19   echo
    20   echo "  Run LaTeX (and related tools) on FILE (default root.tex),"
    21   echo "  producing the specified output format."
    22   echo
    23   exit 1
    24 }
    25 
    26 function fail()
    27 {
    28   echo "$1" >&2
    29   exit 2
    30 }
    31 
    32 
    33 ## process command line
    34 
    35 # options
    36 
    37 OUTFORMAT=dvi
    38 
    39 while getopts "o:" OPT
    40 do
    41   case "$OPT" in
    42     o)
    43       OUTFORMAT="$OPTARG"
    44       ;;
    45     \?)
    46       usage
    47       ;;
    48   esac
    49 done
    50 
    51 shift $(($OPTIND - 1))
    52 
    53 
    54 # args
    55 
    56 FILE="root.tex"
    57 [ "$#" -ge 1 ] && { FILE="$1"; shift; }
    58 
    59 [ "$#" -ne 0 ] && usage
    60 
    61 
    62 ## main
    63 
    64 # root file
    65 
    66 DIR=$(dirname "$FILE")
    67 FILEBASE=$(basename "$FILE" .tex)
    68 [ "$DIR" = . ] || FILEBASE="$DIR/$FILEBASE"
    69 
    70 function check_root () { [ -f "$FILEBASE.tex" ] || fail "Bad file '$FILE'"; }
    71 
    72 
    73 # operations
    74 
    75 function run_latex () { $ISABELLE_LATEX "\\nonstopmode\\input{$FILEBASE.tex}"; }
    76 function run_pdflatex () { $ISABELLE_PDFLATEX "\\nonstopmode\\input{$FILEBASE.tex}"; }
    77 function run_bibtex () { $ISABELLE_BIBTEX </dev/null "$FILEBASE"; }
    78 function run_makeindex () { $ISABELLE_MAKEINDEX </dev/null "$FILEBASE"; }
    79 function run_dvips () { $ISABELLE_DVIPS -q -o "$FILEBASE.ps" "$FILEBASE.dvi"; }
    80 function copy_styles ()
    81 {
    82   for STYLEFILE in "$ISABELLE_HOME/lib/texinputs"/*.sty
    83   do
    84     TARGET="$DIR"/$(basename "$STYLEFILE")
    85     perl -p -e 's/\$[I]d:?(?:\s)*([^\$]*)\$//g' "$STYLEFILE" > "$TARGET"
    86   done
    87 }
    88 
    89 function extract_syms ()
    90 {
    91   perl -n \
    92     -e '(!m,%requires, || m,%requires latin1, || m,%requires amssymb, || m,%requires textcomp,) && m,\\newcommand\{\\isasym(\w+)\}, && print "$1\n";' \
    93     "$ISABELLE_HOME/lib/texinputs/isabellesym.sty" > "$DIR/syms.lst"
    94   perl -n \
    95     -e 'm,\\newcommand\{\\isactrl(\w+)\}, && print "$1\n";' \
    96     "$ISABELLE_HOME/lib/texinputs/isabelle.sty" > "$DIR/ctrls.lst"
    97 }
    98 
    99 case "$OUTFORMAT" in
   100   dvi)
   101     check_root && \
   102     run_latex
   103     RC="$?"
   104     ;;
   105   dvi.gz)
   106     check_root && \
   107     run_latex && \
   108     gzip -f "$FILEBASE.dvi"
   109     RC="$?"
   110     ;;
   111   ps)
   112     check_root && \
   113     run_latex && \
   114     run_dvips
   115     RC="$?"
   116     ;;
   117   ps.gz)
   118     check_root && \
   119     run_latex && \
   120     run_dvips && \
   121     gzip -f "$FILEBASE.ps"
   122     RC="$?"
   123     ;;
   124   pdf)
   125     check_root && \
   126     run_pdflatex
   127     RC="$?"
   128     ;;
   129   bbl)
   130     check_root && \
   131     run_bibtex
   132     RC="$?"
   133     ;;
   134   idx)
   135     check_root && \
   136     run_makeindex
   137     RC="$?"
   138     ;;
   139   sty)
   140     copy_styles
   141     RC="$?"
   142     ;;
   143   syms)
   144     extract_syms
   145     RC="$?"
   146     ;;
   147   *)
   148     fail "Bad output format '$OUTFORMAT'"
   149     ;;
   150 esac
   151 
   152 exit "$RC"