-
Notifications
You must be signed in to change notification settings - Fork 14
/
detex.sh
40 lines (30 loc) · 791 Bytes
/
detex.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
#!/bin/bash
input=$1
if [ -z "$input" ]; then
echo "SYNAPSIS: $0 mylatex.tex"
echo
echo "Primarily runs detex mylatex.tex > mylatex.txt"
echo "Additionally, extracts texts from figure and table captions."
exit 1
fi
output=${input/.tex/.txt}
if [[ "$input" == "$output" ]]; then
output="$input".txt
fi
#if [ -e "$output" ]; then
#echo "Output file $output already exists, not overwriting. Please delete it first."
#exit 1
#fi
echo "detexing main text ..."
detex $input > $output || exit 1
echo "extracting figure captions ..."
< $input sed 's/%.*//g' |
grep -Pzo '(?s)\\caption\{.*?\\end\{' |
sed 's,\label{[^}]*},,g' |
sed 's,\ref{[^}]*},REF,g' |
tr '\n' ' ' |
sed 's,\\caption{,\n,g' |
sed 's, *\\end{.*,,g'|
sed 's,$,\n,g' |
detex >> $output || exit 2
echo "$output created."