-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.sh
More file actions
executable file
·73 lines (58 loc) · 2.62 KB
/
preprocess.sh
File metadata and controls
executable file
·73 lines (58 loc) · 2.62 KB
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
#!/usr/bin/env bash
# Runs main.tex through pandoc
set -e
# Convert tabularx tables to longtables so pandoc can understand
sed 's/\\end{tabularx}/\\end{longtable}/g' "${1:-main.tex}" > main.tex.tmp.pandoc
sed -i 's/\\begin{tabularx}{\\linewidth}{/\\begin{longtable}[]{/g' main.tex.tmp.pandoc
sed -i 's/\\begin{longtable}.*/\L&/g' main.tex.tmp.pandoc
# Fix texttt
sed -i 's/}\\texttt{//g' main.tex.tmp.pandoc
# Process with pandoc
pandoc main.tex.tmp.pandoc -f latex -t latex -o main.tex.tmp.pandoc
# Copy over head of main.tex
begin=$(grep -n '\tableofcontents' main.tex | head -1 | cut -d ':' -f 1)
begin=$((begin + 1))
head -n "$begin" main.tex > main.tex.tmp
# Copy over pandoc processed file
cat main.tex.tmp.pandoc >> main.tex.tmp
rm main.tex.tmp.pandoc
# Remove messed up title page pandoc creates
start=$(grep -n '\tableofcontents' main.tex.tmp | head -1 | cut -d ':' -f 1)
end=$(grep -n '\hypertarget' main.tex.tmp | head -1 | cut -d ':' -f 1)
start=$((start + 1))
end=$((end - 2))
sed -i "${start},${end}d" main.tex.tmp
# Copy over end of main.tex
total_lines=$(wc -l main.tex | cut -d ' ' -f 1)
end=$(grep -n '\end{sloppypar}' main.tex | head -1 | cut -d ':' -f 1)
end=$((total_lines - end + 1))
echo "" >> main.tex.tmp
tail -n "$end" main.tex >> main.tex.tmp
# Format tables
sed -i 's/\\tabularnewline/\n\\tabularnewline\n\\midrule/g' main.tex.tmp
sed -i -e '1h;2,$H;$!d;g' -e 's/\\midrule\n\\midrule/\\midrule/g' main.tex.tmp
sed -i -e '1h;2,$H;$!d;g' -e 's/\\midrule\n\\toprule/\\toprule/g' main.tex.tmp
sed -i -e '1h;2,$H;$!d;g' -e 's/\\midrule\n\\bottomrule/\\bottomrule/g' main.tex.tmp
sed -i 's/\\strut//g' main.tex.tmp
sed -i '/^\\begin{minipage}/d' main.tex.tmp
sed -i 's/^\\end{minipage}.*/\&/g' main.tex.tmp
sed -i -e '1h;2,$H;$!d;g' -e 's/\&\n\\tabularnewline/\\tabularnewline/g' main.tex.tmp
sed -i '/\\endfirsthead/,/\\midrule/d' main.tex.tmp
sed -i -e '1h;2,$H;$!d;g' -e 's/\\\midrule\n\\midrule/\\midrule/g' main.tex.tmp
sed -i 's/\\ldots{}/\\ldots/g' main.tex.tmp
# Convert longtables back to tabularx
sed -i 's/\\end{longtable}/\\end{tabularx}/g' main.tex.tmp
tabularx_headers=($(grep '\\begin{tabularx}' main.tex))
longtable_headers=($(grep -n '\\begin{longtable}' main.tex.tmp | cut -d ':' -f 1))
for ((i = 0; i < ${#longtable_headers[@]}; ++i)); do
line="${longtable_headers[$i]}"
replace=${tabularx_headers[$i]//\\/\\\\}
gawk -i inplace "NR==${line} {\$0=\"${replace}\"}1" main.tex.tmp
done
# Process figures
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
python3 $script_dir/figures.py
# Remove height from images
sed -i 's/\,height=\\textheight//g' main.tex.tmp
# Done
mv main.tex.tmp main.tex