-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathchapters.pl
More file actions
53 lines (45 loc) · 1.07 KB
/
chapters.pl
File metadata and controls
53 lines (45 loc) · 1.07 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
######################################################################
# Extract chapters from PDF.
#
# Call:
# perl chapters.pl <basename>
#
# Reads $basename.aux to access document structure
#
# Extracts pages by chapter from $basename.pdf
#
# Offset is such that the latex page beginning of the first chapter
# equals the corrrect PDF page
######################################################################
my $offset = 2;
my $basename = $ARGV[0];
$basename =~ s/\..*//;
my @begin, @end;
my $chap;
open (my $aux, "<", "$basename.aux");
while (<$aux>)
{
if (m/\{(\d+)\}\{chapter.(\d+)\}/)
{
$chap = $2;
$begin[$chap] = $1+$offset;
$end[$chap-1] = $1-1+$offset;
}
if (m/\{(\d+)\}\{appendix/)
{
++$chap;
$begin[$chap] = $1+$offset;
$end[$chap-1] = $1-1+$offset;
last;
}
}
for (my $i=1;$i<$chap;++$i)
{
print "Chapter $i: $begin[$i] - $end[$i]\n";
system ("qpdf", "$basename.pdf",
"--pages", ".", "$begin[$i]-$end[$i]",
"--", "chapter-$i.pdf");
}
system ("qpdf", "$basename.pdf",
"--pages", ".", "$begin[$chap]-z",
"--", "appendix.pdf");