-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtexflatten
More file actions
executable file
·65 lines (57 loc) · 1.84 KB
/
texflatten
File metadata and controls
executable file
·65 lines (57 loc) · 1.84 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
#!/bin/sh -f
#
# script for recursive flattening of tex/latex/bibtex into a single
# file (written to stdout)
#
# Usage: texflatten.sh <infile.tex>
#
# Important: for citations to work, there must be an up-to-date
# <infile.bbl>. I.e.: run bibtex first on the input files before
# flattening.
# bib file is main file basename .bbl
bibfile="${1%.*}.bbl"
# main gawk script
read -r -d '' awkflatten <<EOF
function insertFile(filename) {
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "%%% BEGIN "filename
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
while ((getline line < filename) > 0)
if( !match( line,/^%/ ) )
print line
close(filename)
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "%%% END "filename
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
}
{
if( match( \$0,/^[^%]*\\\\(include|input)\{([^\}]+)/,includes ) ) {
filename= includes[2]".tex"
insertFile(filename)
} else if( match( \$0,/^[^%]*\\\\bibliography\{/ ) ) {
insertFile("$bibfile")
} else if( !match( \$0,/^%/ ) ) {
# strip full line comments while we are at it
print
}
}
EOF
cd `dirname $1`
# iterative flattening until there are no more include, input and
# bibliography statements left (except those commented out)
infile=`mktemp flatten_input.XXXXXXX`
outfile=`mktemp flatten_output.XXXXXXX`
cp `basename $1` $outfile
while [ "$(egrep '^[^%]*\\(include|input|bibliography){' $outfile)" != "" ]; do
(>&2 echo "flattening iteration...")
cp $outfile $infile
gawk "$awkflatten" $infile > $outfile
done
# output result
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "%%% File generated by texflatten"
echo "%%% (you probably want to edit the source files instead)"
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
cat $outfile
# clean up temp files
rm $infile $outfile