#!/usr/local/bin/perl # # tex_to_p.p David MacKay 4 June 94 # # Part of a suite of programs for `delatexing' a latex file. # # This perl program reads in a latex ".tex" file and # writes a file that is another perl program containing a list of # substitution commands to replace newcommands by their arguments. # # usage : # # tex_2_p.p [options] file.aux # # options: # perl=STRING change first line of perl program to STRING # (default #!/usr/local/bin/perl ) # $perl = "#!/usr/local/bin/perl" ; $this = $ARGV[0] ; # # process options on command line # eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # # add .tex to file name if it is not there already # if ( ! ( $ARGV[0] =~ /\.tex/ ) ) { $ARGV[0] .= ".tex" ; } print $perl, "\n" ; print "# perl program written automatically, based on ", $this , "\n" ; print "# feed this program a .tex file, and it will spit out a version \n" ; print "# with commands replaced according to newcommands \n" ; print "while( <> ) { \n" ; while( <> ) { # if there is a % character (not \% ) then delete what follows. s/\\\%/ /g; s/\%.*$//; if ( m#\\newcommand\{\\([^\}]*)\}\{(.*)\}# || m#\\renewcommand\{\\([^\}]*)\}\{(.*)\}# ) { $string1 = $1 ; $string2 = $2 ; $string1 = &protect ( $string1 ) ; $string2 = &protect ( $string2 ) ; print " s/\\\\$string1\\b/$string2/g;\n" ; # this mess means substitute the string if it is followed by word boundary } # } print " print ; \n" ; print "} \n" ; sub protect { local ( $s ) = @_ ; # throw away some things: # NB these lines only affect occurrences of the strings inside a newcommand $s =~ s/\$//g ; # this deletes all dollars $s =~ s/\\rm//g ; $s =~ s/\\em//g ; $s =~ s/\\bf//g ; $s =~ s/\\cal//g ; $s =~ s/\\scriptscriptstyle//g ; $s =~ s/\\scriptstyle//g ; $s =~ s/\\sc//g ; # protect symbols that might be worth protecting. $s =~ s/\\/\\\\/g ; $s =~ s/\^/\\\^/g ; $s =~ s/\./\\\./g ; $s =~ s/\//\\\//g ; return $s ; }