lib/Tools/latex
author wenzelm
Tue Jul 19 17:21:45 2005 +0200 (2005-07-19)
changeset 16874 3057990d20e0
parent 16171 3c939bb52420
child 26576 fc76b7b79ba9
permissions -rwxr-xr-x
tuned;
     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, png, 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 #set by configure
    76 AUTO_PERL=perl
    77 
    78 function run_latex () { $ISABELLE_LATEX "\\nonstopmode\\input{$FILEBASE.tex}"; }
    79 function run_pdflatex () { $ISABELLE_PDFLATEX "\\nonstopmode\\input{$FILEBASE.tex}"; }
    80 function run_bibtex () { $ISABELLE_BIBTEX </dev/null "$FILEBASE"; }
    81 function run_makeindex () { $ISABELLE_MAKEINDEX </dev/null "$FILEBASE"; }
    82 function run_dvips () { $ISABELLE_DVIPS -q -o "$FILEBASE.ps" "$FILEBASE.dvi"; }
    83 function run_thumbpdf () { [ -n "$ISABELLE_THUMBPDF" ] && $ISABELLE_THUMBPDF "$FILEBASE"; }
    84 function copy_styles ()
    85 {
    86   for STYLEFILE in "$ISABELLE_HOME/lib/texinputs"/*.sty
    87   do
    88     TARGET="$DIR"/$(basename "$STYLEFILE")
    89     "$AUTO_PERL" -p -e 's/\$[I]d:?(?:\s)*([^\$]*)\$//g' "$STYLEFILE" > "$TARGET"
    90   done
    91 }
    92 
    93 function extract_syms ()
    94 {
    95   "$AUTO_PERL" -n \
    96     -e '(!m,%requires, || m,%requires latin1, || m,%requires amssymb, || m,%requires textcomp,) && m,\\newcommand\{\\isasym(\w+)\}, && print "$1\n";' \
    97     "$ISABELLE_HOME/lib/texinputs/isabellesym.sty" > "$DIR/syms.lst"
    98   "$AUTO_PERL" -n \
    99     -e 'm,\\newcommand\{\\isactrl(\w+)\}, && print "$1\n";' \
   100     "$ISABELLE_HOME/lib/texinputs/isabelle.sty" > "$DIR/ctrls.lst"
   101 }
   102 
   103 case "$OUTFORMAT" in
   104   dvi)
   105     check_root && \
   106     run_latex
   107     RC="$?"
   108     ;;
   109   dvi.gz)
   110     check_root && \
   111     run_latex && \
   112     gzip -f "$FILEBASE.dvi"
   113     RC="$?"
   114     ;;
   115   ps)
   116     check_root && \
   117     run_latex && \
   118     run_dvips &&
   119     RC="$?"
   120     ;;
   121   ps.gz)
   122     check_root && \
   123     run_latex && \
   124     run_dvips &&
   125     gzip -f "$FILEBASE.ps"
   126     RC="$?"
   127     ;;
   128   pdf)
   129     check_root && \
   130     run_pdflatex
   131     RC="$?"
   132     ;;
   133   bbl)
   134     check_root && \
   135     run_bibtex
   136     RC="$?"
   137     ;;
   138   idx)
   139     check_root && \
   140     run_makeindex
   141     RC="$?"
   142     ;;
   143   png)
   144     check_root && \
   145     run_thumbpdf
   146     RC="$?"
   147     ;;
   148   sty)
   149     copy_styles
   150     RC="$?"
   151     ;;
   152   syms)
   153     extract_syms
   154     RC="$?"
   155     ;;
   156   *)
   157     fail "Bad output format '$OUTFORMAT'"
   158     ;;
   159 esac
   160 
   161 exit "$RC"