#!/usr/bin/perl # # txt2latex -- by Tristan Miller (psychonaut@nothingisreal.com) # This program is in the public domain. It comes with absolutely NO # WARRANTY -- use at your own risk. # # Revision history # ---------------- # 1.0 -- 2002/02/20 -- initial release # # Description # ----------- # txt2latex assists in translation from ASCII text to LaTeX by escaping # special characters and "fixing" quote marks (using English conventions). # Reads from standard input and writes to standard output. # Any "unfixed" double quotes are reported on standard error. # # If there is a demand, I will add command-line options allowing # customization or suppression of the document header/footer, and use of # non-English quotation marks (e.g., "` and "'). $count = 0; # Header print STDOUT "\\documentclass{article}\n\\begin{document}\n"; foreach $line (<>) { $count++; # Escape special characters $line =~ s/\\/{\\textbackslash}/g; $line =~ s/{/\\{/g; $line =~ s/}/\\}/g; $line =~ s/\\{\\textbackslash\\}/{\\textbackslash}/g; $line =~ s/\$/\\\$/g; $line =~ s/%/\\%/g; $line =~ s/_/\\_/g; $line =~ s/&/\\&/g; $line =~ s/\#/\\\#/g; # Ellipses $line =~ s/(^|[^.])\.\.\.([^.])/\1\\ldots\2/g; # Fix double quotes $line =~ s/(^|\s)\"/\1``/g; $line =~ s/\"(\W|$)/''\1/g; # Fix single quotes $line =~ s/(^|\s)'/\1`/g; if ($line =~ /\"/) { print STDERR "txt2latex: unfixed quote mark on line $count\n"; } print STDOUT $line; } # Footer print STDOUT "\\end{document}\n";