#!/usr/local/bin/perl # # aux_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 ".aux" file and # writes a file that is another perl program containing a list of # substitution commands to replace: # # \ref{} by the appropriate number # \eqref{} by the appropriate number # \figref{} by the appropriate number # \cite{} and \citeasnoun{} by the appropriate citation # # usage : # # aux_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 .aux to file name if it is not there already # if ( ! ( $ARGV[0] =~ /\.aux/ ) ) { $ARGV[0] .= ".aux" ; } print $perl, "\n" ; print "# perl program written automatically, based on ", $this_program , "\n" ; print "# feed this program a .tex file, and it will spit out a version \n" ; print "# with text replacing \\refs and \\cites and \\labels \n" ; print "while( <> ) { \n" ; while( <> ) { s/\\nobreakspace\s*\{\}/ /g; if ( m#\\newlabel\{(.*)\}\{\{(.*)\}\{.*\}\}# ) { &protect1 ( ) ; print " s/\\\\ref\\{$1\\}/$2/g;\n" ; print " s/\\\\label\\{$1\\}/($2)/g;\n" ; print " s/\\\\eqref\\{$1\\}/equation ($2)/g;\n" ; print " s/\\\\figref\\{$1\\}/figure $2/g;\n" ; print " s/\\\\Eqref\\{$1\\}/Equation ($2)/g;\n" ; print " s/\\\\Figref\\{$1\\}/Figure $2/g;\n" ; } # # elsif ( m#\\harvardcite\{(.*)\}\{(.*)\}\{(.*)\}\{(.*)\}# ) { # &protect1 ( ) ; # print ' if ( ! ( s/\\cite\\{$1\\}/($3, $4)/g ) && \n" ; # print ' ! ( s/\\citeasnoun\\{$1\\}/$3 ($4)/g ) ) { \n" ; # print ' s/[\\{\\,\\)]$1[\\(\\}\\,]/($3, $4)/g ; } \n" ; # } # elsif ( m#\\bibcite\{(.*)\}\{.citeauthoryear \{([^\}]*)\}\{([^\}]*)\}\}# ) { # lsalike bibcite &protect1 ( ) ; print " if ( \n" ; print ' ! ( s/\\\\citeasnoun\{'."$1".'\}/'."$2 ($3)/g ) &&\n" ; print ' ! ( s/\\\\quotecite\{'."$1".'\}/'."$2's ($3)/g ) &&\n" ; print ' ! ( s/\\\\cite\{'."$1".'\}/('."$2, $3)/g ) && \n" ; print ' ! ( s/\\\\cite\{'."$1".'\,/('."$2, $3),/g ) \n" ; print " ) { \n" ; print ' s/([\,\{\)])'."$1".'([\(\,\}])/'."\$1($2, $3)\$2/g ; } \n" ; } # elsif ( m#\\bibcite\{(.*)\}\{(.*)\}# ) { # plain bibcite &protect1 ( ) ; print ' if ( ! ( s/\\\\cite\{'."$1".'\}/('."$2)/g ) && \n" ; print ' ! ( s/\\\\cite\{'."$1".'\,/('."$2),/g ) && \n" ; print ' ! ( s/\\\\citeasnoun\{'."$1".'\}/'."$2/g ) ) { \n" ; print ' s/([\,\{\)])'."$1".'([\(\,\}])/'."\$1($2)\$2/g ; } \n" ; # print ' s/[\{\,\)]'."$1".'[\(\}\,]/'."($2)/g ; } \n" ; } # } # print " s/\\\\cite//g ; \n" ; # print " s/\\\\citeasnoun//g ; \n" ; print " print ; \n" ; print "} \n" ; sub protect1 { # Something should strictly be done here to protect special characters # in the string $1. In my latex documents the only special character is # ".", which causes no problems. # the protect function in tex_2_p.p could be used here. }