#!/usr/local/bin/perl # # David MacKay 4 June 94 # # delatex0.p - reads a tex file and follows \input{} commands # and writes a flat file without those commands # # use # delatex0.p forbiddull=0 file.tex > flatfile.tex # # if you want to follow everything # $verbose = 0 ; $forbiddull = 1 ; # whether to strip out boring include files eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $filelist = "" ; $filenumber=0 ; $level = 1 ; WHLOOP: while( <>){ if (!( /^\%/ ) ) { if ( /\\end\{document\}/ ) { print ; last WHLOOP; } while ( s/\\(input|include)\{([^}]+)\}// ) { $f = $2 ; print STDERR "following input file $f \n" if ( $verbose ); &processfile ( $f ) ; } } print ; } print $filelist ; sub processfile { ($file,$rest) = @_ ; if ( !($file =~ /\.((la|)tex|ind)$/ ) ) { print STDERR "adding tex to $file : " if ( $verbose ); $file .= ".tex" ; # } # if ( $forbiddull && $file =~ /newcommands|psfig|itprnncha/ ) { print STDERR "skipping $file, it's boring\n" if ( $verbose ); print "%%%%%%%%%% skipping $file, it's boring\n" ; # put it back into the file......... print "\\input\{$file\}\n" ; } else { $filenumber ++ ; local($opencommand) = "open( O$filenumber , \"< $file\" ) " ; local($readcommand) = "\$_ = " ; print STDERR $opencommand , " " , $readcommand , "\n" ; if ( eval($opencommand) ) { $level ++ ; # print "%%%%%%%%%%%%%%%%%% reading file $file %%%%%%%%%%%%%%%%\n" ; $filelist .= "%%"." "x$level."$file\n" ; while ( eval($readcommand) ) { # if (!( /^\%/ ) ) { # if ( /\\end\{document\}/ ) { print ; last WHLOOP; } while ( s/\\(input|include)\{([^\}]+)\}// ) { $f = $2 ; # print STDERR "following input file $f \n" if ( $verbose ); &processfile ( $f ) ; } } print ; } $level -- ; # } else { print STDERR "couldn't open $file\n" ; # } } }