#!/usr/bin/perl -w use strict; use Getopt::Std; our $opt_a; #includes abstract (my $basename = $0) =~ s!.*/!!; if (@ARGV < 2 || $ARGV[0] eq '--help') { warn < [...] Takes a .tex file which \\inputs chapters (such as a thesis.tex file), and a list of selected chapters (referred to by (file)name or number, or letter for appendices). A .tex file is then produced containing only the title page, table of contents, the selected chapters, and the references for those chapters. Chapters can appear in any order at the command line, but will be reordered as they appear in the original master.tex file. Output is sent to file.chaps.tex. The -a option causes the abstract, if any, to be included. (The original .tex file is assumed to be formed for the easychithesis class.) USAGE exit (0); } getopts('a'); my $In_texfile = shift @ARGV; (my $Out_texfile = $In_texfile) =~ s/(\.tex)?$/.chaps$1/; open (TEX, "<$In_texfile") or die "$basename: Could not open $In_texfile for read, $!"; my $Header; my $Found_maketitle; while () { s/([^\\]?(?:\\\\)*%).*$/$1/; next if (/^\s*%?\s*$/); $Header .= $_; if (/[^\\]?(?:\\\\)*\\maketitle\W/) { $Found_maketitle = 1; last; } die "$basename: no \\maketitle found in $In_texfile, aborting.\n" if /[^\\]?(?:\\\\)*\\end{document}/; } die "$basename: no \\maketitle found in $In_texfile, aborting.\n" unless $Found_maketitle; my $Abstract = ''; my $Found_toc; my $Found_top; if ($opt_a) { while () { s/([^\\]?(?:\\\\)*%).*$/$1/; next if /^\s*%?\s*$/; if (/[^\\]?(?:\\\\)*\\topmatter{(?i:abstract)}/) { $Abstract = $_; last; } die "$basename: no abstract found in $In_texfile, aborting.\n" if /[^\\]?(?:\\\\)*\\end{document}/; } while () { s/([^\\]?(?:\\\\)*%).*$/$1/; next if /^\s*%?\s*$/; if (/[^\\]?(?:\\\\)*\\tableofcontents\W/) { $Found_toc = 1; last; } if (/[^\\]?(?:\\\\)*\\topmatter\W/) { $Found_top = 1; last; } die "$basename: \\end{document} while reading abstract, aborting.\n" if /[^\\]?(?:\\\\)*\\end{document}/; $Abstract .= $_; } die "$basename: end of file while reading abstract, aborting.\n" unless ($Found_toc || $Found_top); } unless ($Found_toc) { while () { s/([^\\]?(?:\\\\)*%).*$/$1/; next if /^\s*%?\s*$/; if (/[^\\]?(?:\\\\)*\\tableofcontents\W/) { $Found_toc = 1; last; } die "$basename: no \\tableofcontents found in $In_texfile, aborting.\n" if /[^\\]?(?:\\\\)*\\end{document}/; } die "$basename: no \\tableofcontents found in $In_texfile, aborting.\n" unless $Found_toc; } my $Main = $_; #This SHOULD be the line containing tableofcontents my $Found_mainmatter; while () { s/([^\\]?(?:\\\\)*%).*$/$1/; next if /^\s*%?\s*$/; $Main .= $_; if (/[^\\]?(?:\\\\)*\\mainmatter\W/) { $Found_mainmatter = 1; last; } die "$basename: no \\mainmatter found in $In_texfile, aborting.\n" if /[^\\]?(?:\\\\)*\\end{document}/; } die "$basename: no \\mainmatter found in $In_texfile, aborting.\n" unless $Found_mainmatter; my @Chapters; my $Found_appendix; my @Appendices; my $Found_references; while () { s/([^\\]?(?:\\\\)*%).*$/$1/; next if /^\s*%?\s*$/; if (/[^\\]?(?:\\\\)*\\addcontentsline{toc}{chapter}{References}/) { $Found_references = 1; last; } if (/[^\\]?(?:\\\\)*\\appendix\W/) { $Found_appendix = 1; } die "$basename: no References line found before \\end{document}, aborting.\n" if /[^\\]?(?:\\\\)*\\end{document}/; next unless /[^\\]?(?:\\\\)*\\input{(\w+)}/; if ($Found_appendix) { push @Appendices, $1; } else { push @Chapters, $1; } } die "$basename: no References line found before end of file, aborting.\n" unless $Found_references; my $Trailer = $_; my $Found_end; while() { s/([^\\]?(?:\\\\)*%).*$/$1/; next if /^\s*%?\s*$/; $Trailer .= $_; if (/[^\\]?(?:\\\\)*\\end{document}/) { $Found_end = 1; last; } } die "$basename: no \\end{document} found, aborting.\n" unless $Found_end; close (TEX) or die "$basename: Could not close $In_texfile, $!"; my %Chapter_num; my %Appendix_num; @Chapter_num{@Chapters} = (0 .. $#Chapters); @Appendix_num{@Appendices} = (0 .. $#Appendices); my @Chosen_chapters; my @Chosen_appendices; foreach (@ARGV) { if (/^(\d+)$/) { my $Chapter = $1 - 1; if ($Chapter <= $#Chapters) { push @Chosen_chapters, $Chapters[$Chapter]; } else { die "$basename: requested chapter $1 not found in $In_texfile, aborting.\n"; } } elsif (/^([A-Za-z])$/) { my $Appendix = (ord(uc($1)) - ord('A')); if ($Appendix <= $#Appendices) { push @Chosen_appendices, $Appendices[$Appendix]; } else { die "$basename: requested appendix $1 not found in $In_texfile, aborting.\n"; } } elsif (exists $Chapter_num{$_}) { push @Chosen_chapters, $_; } elsif (exists $Appendix_num{$_}) { push @Chosen_appendices, $_; } else { die "$basename: requested chapter $_ not found in $In_texfile, aborting.\n"; } } my %Chosen_chapters; my %Chosen_appendices; @Chosen_chapters{@Chosen_chapters} = (); @Chosen_appendices{@Chosen_appendices} = (); @Chosen_chapters = sort { $Chapter_num{$a} <=> $Chapter_num{$b}} keys(%Chosen_chapters); @Chosen_appendices = sort {$Appendix_num{$a} <=> $Appendix_num{$b}} keys(%Chosen_appendices); open (TEX, ">$Out_texfile") or die "$basename: Could not open $Out_texfile for write, $!"; print TEX <