-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex-compile
executable file
·163 lines (146 loc) · 4.11 KB
/
latex-compile
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/env bash
# Latex command template where %s is filename
LATEX_COMMAND="latexmk"
LATEX_ARGS="-f -shell-escape --synctex=1 --pdf"
LATEX_PIPE=""
flag_auto=''
flag_supress=''
flag_debug=''
watch_files=''
compile_count='1'
compile_files=''
latex_recipe='pdflatex' # unused
latex_extra_args='' # unused
print_usage() {
printf "latex-compile: Helper tool to autocompile latex files.\n"
printf "\nUsage: latex-compile [-a|--auto] [-s|--supress]"
printf " [-d|--debug] [-w|--watch <files>]"
printf " [-r|--recipe <recipe>] [-c|--commands <args>]"
printf " [-h|--help] [<files>]\n"
printf "\nOptions:\n"
printf " [-a|--auto]\n"
printf " Automatically compile latex each time file is modified.\n"
printf " [-s|--supress]\n"
printf " Supress as much output as possible.\n"
printf " [-d|--debug]\n"
printf " Debug mode; print variables and exit.\n"
printf " [-w|--watch] <files>\n"
printf " Add more files to watch list which trigger auto compile.\n"
printf " [-r|--recipe] <recipe>\n"
printf " Recipe to use for compilation\n"
printf " [-c|--commands] <args>\n"
printf " Arguments to pass to latex compile program.\n"
printf " [-C|--clean]\n"
printf " Clean the extra files generated by pdflatex (keep .tex and .pdf).\n"
printf " [-h|--help]\n"
printf " Display this and exit.\n"
printf " [<files>]\n"
printf " Files to compile, defaults to all .tex in PWD.\n"
}
compile_(){
for i in $(seq $compile_count); do
for arg in $compile_files; do
printf "$LATEX_COMMAND $LATEX_ARGS $PWD/%s $LATEX_PIPE\n" "$arg"
$(printf "$LATEX_COMMAND $LATEX_ARGS $PWD/%s $LATEX_PIPE\n" "$arg")
done
done
}
TEMP=$(getopt -o 'asdt:w:r:c:Ch' --long 'auto,supress,debug,times:,watch:,recipe:,commands:,clean,help' -n 'latex-compile' -- "$@")
if [ $? -ne 0 ]; then
echo 'Erro in parsing Arguments...' >&2
print_usage
exit 1
fi
eval set -- "$TEMP"
unset TEMP
while true; do
case "$1" in
'-a'|'--auto')
flag_auto='true'
shift
continue
;;
'-s'|'--supress')
flag_supress='true'
LATEX_ARGS="$LATEX_ARGS --interaction=batchmode"
LATEX_PIPE=" 2>&1 > /dev/null"
shift
continue
;;
'-d'|'--debug')
flag_debug='true'
shift
continue
;;
'-w'|'--watch')
watch_files="$PWD/$2 $watch_files"
shift 2
continue
;;
'-C'|'--clean')
rm $PWD/*.{aux,log,out}
exit 0
;;
'-r'|'--recipe')
recipe="$2"
shift 2
continue
;;
'-t'|'--times')
compile_count="$2"
shift 2
continue
;;
'-h'|'--help')
print_usage
shift
exit
;;
'--')
shift
break
;;
*) printf "Internal Error!\n"
exit 1
;;
esac
done
compile_files="$@"
# if none given defaults to all the tex files in PWD
if [[ -z $compile_files ]]; then
compile_files="*.tex"
fi
# outputs for debug mode
if [[ -n $flag_debug ]]; then
echo "---------Settings---------"
printf "LaTeX Command = \t {%s}\n" "$LATEX_COMMAND"
printf "LaTeX Args = \t {%s}\n" "$LATEX_ARGS"
printf "LaTeX PIPE = \t {%s}\n" "$LATEX_PIPE"
echo "------User Arguments------"
printf "flag_auto = \t {%s}\n" "$flag_auto"
printf "flag_supress = \t {%s}\n" "$flag_supress"
printf "flag_debug = \t {%s}\n" "$watch_files"
printf "recipe = \t {%s}\n" "$latex_recipe"
printf "args = \t {%s}\n" "$latex_extra_args"
printf "compile_count = \t {%s}\n" "$compile_count"
echo "-----------Files----------"
printf "watch_files: \n\t {%s}\n" "$watch_files"
printf "compile_files: \n\t {%s}\n" "$compile_files"
exit
fi
# just compile and exit if it's not in auto mode
if [[ -z $flag_auto ]]; then
compile_
exit
fi
for file in $compile_files; do
watch_files="$watch_files $PWD/$file"
done
printf "Waiting for changes in the watch files.\n$watch_files\n"
# listen to events and compile when one of the files is modified
inotifywait -q -m -e modify $watch_files |
while read -r filename event; do
printf "Changes detected in: %s\n" "$filename"
compile_
printf "Waiting for changes in the watch files.\n$watch_files\n"
done