-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_tex.sh
163 lines (153 loc) · 5.31 KB
/
compile_tex.sh
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/bash
# for each lecture .tex file, compiles the .tex file with pdflatex and writes
# the output to the same directory the source .tex file is located in.
# pdflatex compile command. for some reason, -output-directory is ignored.
PDF_TEX="pdflatex -interaction=nonstopmode -halt-on-error -shell-escape"
# directory this file is located in, the repo root (relative path)
REPO_ROOT=$(dirname $0)
# base lesson directory
LESSON_ROOT=$REPO_ROOT/lessons
# error message when there are too many arguments
TOO_MANY_ARGS="$0: too many arguments. try $0 --help for usage"
# script usage
USAGE="
usage: $0 [TEXFILE] [-v]
For each .tex file in the top-level package directory and for each
lecture_[0-9]+ directory located in the top-level package directory, compiles
the .tex file to PDF with pdflatex + bibtex and writes the output to the same
directory the respective .tex file is located in. The pdflatex command used is
$PDF_TEX
The typical pdflatex -> bibtex -> pdflatex -> pdflatex chain of commands is run
to produce the PDF, which will be in the same directory as the .tex file.
If an argument is passed to this script, assumed to be a .tex file, then only
that specified file will be compiled into a PDF.
optional arguments:
-h, --help show this usage
TEXFILE specified .tex file to compile, output written to the same
directory as TEXFILE. if omitted, then all .tex files in the
top-level package directory and each lecture_[0-9]+ directory
within in the top-level package directory are compiled.
-v, --verbose pass to show the full output of the pdflatex/bibtex commands.
by default, the output of pdflatex/bibtex is suppressed."
##
# Single-file compilation function.
#
# Arguments:
# Path to .tex file
# 0 for silence (direct output to /dev/null), 1 for no output suppression
# Outputs:
# Message on which PDF file is being built and any TeX output if verbose
#
compile_tex() {
# save current directory so we can cd back
BASE_DIR=$(pwd)
# save file name, without extension
LONG_PROJ_NAME=$1
LONG_PROJ_NAME=${LONG_PROJ_NAME%%.tex}
# save pathless file name, without extension (delete from back of string)
PROJ_NAME=$(basename $1)
PROJ_NAME=${PROJ_NAME%%.tex}
# so that pdflatex doesn't screw up looking for images, cd to dir of arg.
# note sure why pdflatex doesn't respect the \graphicspath command.
cd $(dirname $1)
## build project (pdflatex -> bibtex -> pdflatex -> pdflatex) ##
# if verbose, allow pdflatex and bibtex output to be printed to screen
if [[ $2 == 1 ]]
then
echo "building $LONG_PROJ_NAME.pdf..."
$PDF_TEX $PROJ_NAME
# BibTeX returns nonzero exit code if it is called on project that has
# no citation, bibdata, or bibstyle commands. ignore so set -e works
bibtex $PROJ_NAME || true
$PDF_TEX $PROJ_NAME
$PDF_TEX $PROJ_NAME
echo "done"
# else suppress all stdout from pdflatex and bibtex
else
echo -n "building $LONG_PROJ_NAME.pdf... "
$PDF_TEX $PROJ_NAME > /dev/null
# see lines 66-67. ignore so set -e works
bibtex $PROJ_NAME > /dev/null || true
$PDF_TEX $PROJ_NAME > /dev/null
$PDF_TEX $PROJ_NAME > /dev/null
echo "done"
fi
# return to base directory
cd $BASE_DIR
}
##
# Main compilation loop.
#
# Arguments:
# 0 for silence (direct output to /dev/null), 1 for no output suppression
# Outputs:
# Outputs from compile_tex
#
compile_loop() {
# compile all .tex files in the top-level lessons directory
for INFILE in $LESSON_ROOT/*.tex
do
compile_tex $INFILE $1
done
# for each lecture directory in the lessons directory
for LEC_DIR in $LESSON_ROOT/lecture_{00..15}
do
# compile each .tex file in the directory
for INFILE in $LEC_DIR/*.tex
do
# don't feed nonexistent names to compilation function
if [ -e $INFILE ]
then
compile_tex $INFILE $1
fi
done
done
}
##
# Main function.
#
# If no arguments, all TeX files in repo are compiled to PDF non-verbosely.
#
# Arguments:
# -h, --help or list of individual TeX files to compile.
#
main() {
if [[ $# == 0 ]]
then
# compile all .tex files in repo, suppressing pdflatex/bibtex output
compile_loop 0
# else if 1 argument
elif [[ $# == 1 ]]
then
# if help argument, then print usage
if [ $1 = "-h" ] || [ $1 = "--help" ]
then
# need double quote to preserve the spacing
echo "$USAGE"
# else if -v or --verbose, don't suppress pdflatex/bibtex output when
# compiling all the .tex files to pdf
elif [ $1 = "-v" ] || [ $1 = "--verbose" ]
then
compile_loop 1
# else treat as .tex and send to pdflatex. output written in same dir.
else
compile_tex $1 0
fi
# else if two 2 arguments
elif [[ $# == 2 ]]
then
# if the second argument is -v or --verbose, compile file verbosely
if [ $2 = "-v" ] || [ $2 = "--verbose" ]
then
compile_tex $1 1
# else too many arguments
else
echo $TOO_MANY_ARGS
fi
# else too many arguments
else
echo $TOO_MANY_ARGS
fi
}
set -e
main "$@"