forked from mohuangrui/ucasthesis
-
Notifications
You must be signed in to change notification settings - Fork 2
/
artratex.sh
132 lines (129 loc) · 4.93 KB
/
artratex.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
#!/bin/bash
set -eu
#---------------------------------------------------------------------------#
#- LaTeX Automated Compiler -#
#- <By e71828> -#
#- Copyright (C) e71828 <[email protected]> -#
#- This is free software: you can redistribute it and/or modify it -#
#- under the terms of the GNU General Public License as published by -#
#- the Free Software Foundation, either version 3 of the License, or -#
#- (at your option) any later version. -#
#---------------------------------------------------------------------------#
VERSION=0.1
parser_definition() {
setup REST error:error plus:true abbr:true help:usage width:18 -- \
"Usage: ${2##*/} [options...] [arguments...]" ''
msg -- 'LaTeX Automated Compiler' ''
msg -- 'Options:'
flag OPEN -o --open init:=0 -- "open the pdf"
flag TEX -x +x on:xelatex no:lualatex init:=pdflatex \
-- "specify tex engine: default:pdflatex; -x:xelatex; +x:lualatex"
flag BIB -a +a on:bibtex no:biber init:= \
-- "specify bib engine: default:none; -a:bibtex; +a:biber"
disp :usage -h --help
disp VERSION -v --version
}
eval "$(getoptions parser_definition - "$0") exit 1"
echo "---------------------------------------------------------------------------"
echo " INFORMATION "
echo "OPEN: $OPEN"
echo "TEX: $TEX"
echo "BIB: $BIB"
#---------------------------------------------------------------------------#
#->> Preprocessing
#---------------------------------------------------------------------------#
#-
#-> Get absolute path
#-
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "$SCRIPT_DIR"
echo "---------------------------------------------------------------------------"
echo "Change directory to $SCRIPT_DIR"
echo "---------------------------------------------------------------------------"
#-
#-> Get source filename
#-
if [[ "$#" -eq 0 ]]; then
# shellcheck disable=SC2006
# shellcheck disable=SC2035
FileName=`echo *.tex`
elif [[ "$#" -eq 1 ]]; then
FileName="$1"
else
echo " TOO MANY ARGUMENTS TO DEAL WITH, TRY LESS AGAIN "
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
exit
fi
FileName=${FileName/.tex}
#-
#-> Set compilation out directory resembling the inclusion hierarchy
#-
Tmp="Tmp"
if [[ ! -d $Tmp ]]; then
mkdir $Tmp
fi
#-
#-> Set LaTeX environmental variables to add subdirs into search path
#-
export TEXINPUTS=".//:" # paths to locate .tex
export BIBINPUTS=".//:" # paths to locate .bib
export BSTINPUTS=".//:" # paths to locate .bst
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
# shellcheck disable=SC2001
TEXINPUTS=$(sed 's|:|;|' <<< $TEXINPUTS)
# shellcheck disable=SC2001
BIBINPUTS=$(sed 's|:|;|' <<< $BIBINPUTS)
# shellcheck disable=SC2001
BSTINPUTS=$(sed 's|:|;|' <<< $BSTINPUTS)
fi
#---------------------------------------------------------------------------#
#->> Compiling
#---------------------------------------------------------------------------#
#-
#-> Build textual content and auxiliary files
#-
$TEX -output-directory=$Tmp -interaction=nonstopmode $FileName || exit
#-
#-> Build references and links
#-
if [[ -n $BIB ]]; then
#- fix the inclusion path for hierarchical auxiliary files
sed -i -e "s|\@input{|\@input{$Tmp/|g" $Tmp/"$FileName".aux
#- extract and format bibliography database via auxiliary files
$BIB $Tmp/$FileName
#- insert reference indicators into textual content
$TEX -output-directory=$Tmp -interaction=nonstopmode $FileName || exit
#- refine citation references and links
$TEX -output-directory=$Tmp -interaction=nonstopmode $FileName || exit
fi
#---------------------------------------------------------------------------#
#->> Postprocessing
#---------------------------------------------------------------------------#
#-
#-> Set PDF viewer
#-
# shellcheck disable=SC2006
if [[ $OSTYPE == "linux"* ]]; then
PDFviewer="xdg-open"
elif [[ $OSTYPE == "darwin"* ]]; then
PDFviewer="open"
elif [[ "$OSTYPE" == "msys" ]]; then
PDFviewer="start"
elif [[ "$OSTYPE" == "cygwin" ]]; then
PDFviewer="cygstart"
else
PDFviewer="open"
fi
#-
#-> Open the compiled file
#-
if [[ $OPEN -eq 1 ]]; then
$PDFviewer ./$Tmp/"$FileName".pdf || exit
else
echo "---------------------------------------------------------------------------"
echo "Do not Open the compiled PDF"
fi
echo "---------------------------------------------------------------------------"
# shellcheck disable=SC2027
echo "$TEX $BIB "$FileName".tex finished..."
echo "---------------------------------------------------------------------------"