3 # This script takes a LaTeX file and generates PPM files, one per page.
4 # The idea is to use it with preview.sty to create small bitmap previews of
5 # things like math equations.
7 # The script takes two arguments, the name of the file to be converted and
8 # the resolution of the generated image, to be passed to gs.
13 # A couple of helper functions
16 which ${FIND_EXECUTABLE} > /dev/null
18 if [ ${STATUS} -ne 0 ]; then
19 echo "Unable to find \"${FIND_EXECUTABLE}\". Please install."
25 if [ ${STATUS} -ne 0 ]; then
26 echo "${EXECUTABLE} failed."
27 # Remove everything except the original .tex file.
28 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
34 # We use latex, dvips and gs, so check that they're all there.
35 FIND_EXECUTABLE=latex; FIND_IT
36 FIND_EXECUTABLE=dvips; FIND_IT
37 FIND_EXECUTABLE=gs; FIND_IT
39 # Initialise some variables.
43 DIR=`dirname ${TEXFILE}`
44 BASE=`basename ${TEXFILE} .tex`
47 METRICS=${BASE}.metrics
49 # Perform the conversion.
51 latex -interaction=batchmode ${TEXFILE}
54 EXECUTABLE="latex ${TEXFILE}"; CHECK_STATUS
56 dvips -o ${PSFILE} ${DVIFILE}
59 EXECUTABLE="dvips ${DVIFILE}"; CHECK_STATUS
61 # Older versions of gs have problems with a large degree of anti-aliasing
64 if [ ${RESOLUTION} -gt 150 ]; then
68 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnm -sOutputFile=${BASE}%03d.ppm \
69 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
73 EXECUTABLE="gs ${PSFILE}"; CHECK_STATUS
75 # Attempt to generate a file ${METRICS} that contains only the tightpage
76 # bounding box info, extract from ${PSFILE}
78 # 1. Create a file containing the sed instructions
80 cat - > ${SEDSCRIPT} <<EOF
81 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
82 /^\%\%BeginDocument/,/^\%\%EndDocument/d
84 # Extract the tightpage bounding box info.
88 # -32890 -32890 32890 32890 492688 0 744653
89 # The sed command gives this:
90 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
93 s/\: \(.*\) .*$/ \1: /;N;N
97 # Delete everything (so only the stuff that's printed, above, goes into the
103 sed -f ${SEDSCRIPT} < ${PSFILE} > ${METRICS}
106 EXECUTABLE="extracting metrics"; CHECK_STATUS
108 # All was successful, so remove everything except the ppm files and the
110 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}.*.ppm/d"`