summary refs log tree commit diff
path: root/pkgs/misc/tex/nix/run-latex.sh
blob: ecce893950ba10bcbf89d29184e9df6f8d3a87c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
source $stdenv/setup

ensureDir $out

mkdir root
cd root

startDir=$(perl $copyIncludes $includes)
cd $startDir

for i in $extraFiles; do
    if test -d $i; then
        ln -s $i/* .
    else
        ln -s $i $(stripHash $i; echo $strippedName)
    fi
done

rootName=$(basename $(stripHash "$rootFile"; echo $strippedName))

rootNameBase=$(echo "$rootName" | sed 's/\..*//')

if test -n "$generatePDF"; then
    latex=pdflatex
else
    latex=latex
fi

latexFlags="-file-line-error"
tmpFile=$out/log

showError() {
    echo
    echo "LATEX ERROR (LAST LOG LINES SHOWN):"
    tail -n 20 $tmpFile
    bzip2 $tmpFile
    exit 1
}

runLaTeX() {
    if ! $latex $latexFlags $rootName >$tmpFile 2>&1; then showError; fi
    runNeeded=
    if fgrep -q \
        -e "LaTeX Warning: Label(s) may have changed." \
        -e "Rerun to get citations correct." \
        "$tmpFile"; then
        runNeeded=1
    fi
}

echo


if test -n "$copySources"; then
    cp -prd $TMPDIR/root $out/tex-srcs
fi


echo "PASS 1..."
runLaTeX
echo


# Run bibtex to process all bibliographies.  There may be several when
# we're using the multibib package.
for auxFile in $(find . -name "*.aux"); do
    if grep -q '\\citation' $auxFile; then
        echo "RUNNING BIBTEX ON $auxFile..."
        auxBase=$(basename $auxFile .aux)
        bibtex --terse $auxBase
        cp $auxBase.bbl $out
        runNeeded=1
        echo
    fi
done


if test "$runNeeded"; then
    echo "PASS 2..."
    runLaTeX
    echo
fi


if test -f $rootNameBase.idx; then
    echo "MAKING INDEX..."
    if test -n "$compressBlanksInIndex"; then
        makeindexFlags="$makeindexFlags -c"
    fi
    makeindex $makeindexFlags $rootNameBase.idx
    runNeeded=1
    echo
fi    


if test "$runNeeded"; then
    echo "PASS 3..."
    runLaTeX
    echo
fi


if test "$runNeeded"; then
    echo "PASS 4..."
    runLaTeX
    echo
fi


if test "$runNeeded"; then
    echo "Hm, still not done :-("
    echo
fi


if test -n "$generatePDF"; then
    cp $rootNameBase.pdf $out
else
    cp $rootNameBase.dvi $out
    if test -n "$generatePS"; then
        echo "CONVERTING TO POSTSCRIPT..."
        dvips $rootNameBase.dvi -o $out/$rootNameBase.ps
        echo
    fi
fi


echo "OVERFULL/UNDERFULL:"
cat $tmpFile | egrep "Overfull|Underfull" || true

echo
echo "UNDEFINED REFERENCES:"
cat $tmpFile | grep "Reference.*undefined" || true

echo
echo "UNDEFINED CITATIONS:"
cat $tmpFile | grep "Citation.*undefined" || true

echo
echo "STATS:"
printf "%5d overfull/underfull h/vboxes\n" $(cat $tmpFile | egrep -c "Overfull|Underfull" || true)
printf "%5d undefined references\n" $(cat $tmpFile | grep -c "Reference.*undefined" || true)
printf "%5d undefined citations\n" $(cat $tmpFile | grep -c "Citation.*undefined" || true)
printf "%5d pages\n" \
    $(cat $tmpFile | grep "Output written.*(.*pages" | sed "s/.*(\([0-9]*\) pages.*/\1/" || true)
echo

bzip2 $tmpFile