-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_latex.sh
executable file
·157 lines (133 loc) · 3.95 KB
/
build_latex.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
#!/bin/bash
# Description: Compile PDF from Jupyter notebook with BibLaTeX references
### END INIT INFO
#
# Author: Bjoern Kasper
# Copyright: GPL v3.0 and later
# Date: 2022-08-29
# Changed: 2023-01-13
#
# Short-Description: Compile PDF from Jupyter notebook with BibLaTeX references
#
# Changelog:
# - 2022-08-30: Changed handling of file names
# - 2023-01-13: Added the possibility to choose the LaTeX compiler
#
###
CMD=$1
NOTEBOOK_FILE=$2
# LATEX_COMPILER="pdflatex"
LATEX_COMPILER="xelatex"
# funtion to run LaTeX command stored in a variable
mylatex() {
$LATEX_COMPILER $1
}
USED_PROGRAMS_ARRAY=(
"basename"
"jupyter"
$LATEX_COMPILER
"biber"
)
LATEX_FILE_EXTENSIONS_TO_CLEAN_ARRAY=(
".aux"
".bbl"
".bcf"
".blg"
".log"
".out"
".run.xml"
".toc"
".synctex.gz"
)
#function that checks if dependencies are installed
function check_software {
printf "Checking if dependencies are installed ...\n";
for index in ${!USED_PROGRAMS_ARRAY[*]}
do
if !( type "${USED_PROGRAMS_ARRAY[$index]}" > /dev/null 2>&1 ); then
printf "Error: Please install %s first!\n" "${USED_PROGRAMS_ARRAY[$index]}";
exit 1;
fi
done
printf "Success: All dependencies are installed\n";
}
#function that cleans temporary build files and folders
function clean_build_artefacts {
printf "Cleaning temporary build artefacts ...\n";
for index in ${!LATEX_FILE_EXTENSIONS_TO_CLEAN_ARRAY[*]}
do
FILE_TO_CLEAN="$NOTEBOOK_FILE_BASE${LATEX_FILE_EXTENSIONS_TO_CLEAN_ARRAY[$index]}"
if [ -f $FILE_TO_CLEAN ]; then
printf "File to remove: %s\n" "$FILE_TO_CLEAN";
rm -rf $FILE_TO_CLEAN
else
printf "Error: file for cleaning not found: %s\n" "$FILE_TO_CLEAN";
fi
done
if [ -d $LATEX_TMP_DIRECTORY_TO_CLEAN ]; then
printf "Directory to remove: %s\n" "$LATEX_TMP_DIRECTORY_TO_CLEAN";
rm -rf $LATEX_TMP_DIRECTORY_TO_CLEAN
else
printf "Error: directory for cleaning not found: %s\n" "$LATEX_TMP_DIRECTORY_TO_CLEAN"
fi
}
function usage {
printf "***\n"
printf "Usage:\n"
printf "Compile PDF from Juypter notebook: %s compile <notebook file>\n" "$0";
printf "Cleanup temporary build artefacts: %s clean <notebook file>\n" "$0";
printf "***\n"
exit 0;
}
check_software;
case "$CMD" in
compile)
if ! [ -z $NOTEBOOK_FILE ]; then
NOTEBOOK_FILE_BASE=`basename ${NOTEBOOK_FILE%.*}`
if ! [ -z $NOTEBOOK_FILE_BASE.ipynb ]; then
jupyter nbconvert $NOTEBOOK_FILE_BASE.ipynb --to latex
mylatex $NOTEBOOK_FILE_BASE.tex
biber $NOTEBOOK_FILE_BASE.bcf
mylatex $NOTEBOOK_FILE_BASE.tex
mylatex $NOTEBOOK_FILE_BASE.tex
printf "Compiling was successful.\n";
else
printf "Error: Please provide a valid notebook filename for compiling!\n";
usage;
exit 1;
fi
else
printf "Error: Please provide a valid filename for compiling!\n";
usage;
exit 1;
fi
exit 0
;;
clean)
if ! [ -z $NOTEBOOK_FILE ]; then
NOTEBOOK_FILE_BASE=`basename ${NOTEBOOK_FILE%.*}`
LATEX_TMP_DIRECTORY_TO_CLEAN=${NOTEBOOK_FILE_BASE}_files
if [ -f $NOTEBOOK_FILE_BASE.ipynb ]; then
clean_build_artefacts;
else
printf "Error: Please provide a valid notebook filename for cleaning!\n";
usage;
exit 1;
fi
else
printf "Error: Please provide a valid filename for cleaning!\n";
usage;
exit 1;
fi
exit 0
;;
help)
usage;
exit 0
;;
*)
usage;
exit 1
;;
esac
exit 0