--- loncom/xml/lonxml.pm	2004/03/19 22:01:13	1.311
+++ loncom/xml/lonxml.pm	2004/03/23 00:47:48	1.313
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.311 2004/03/19 22:01:13 albertel Exp $
+# $Id: lonxml.pm,v 1.313 2004/03/23 00:47:48 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -408,16 +408,17 @@ sub latex_special_symbols {
         $string=~s/([^\\])\&/$1\\\&/g;
         $string=~s/([^\\])\#/$1\\\#/g;
     } else {
-	$string=~s/([^\\])\%/$1\\\%/g;
-	$string=~s/([^\\])(\$|_)/$1\\$2/g;
+	$string=~s/\\/\\ensuremath{\\backslash}/g;
+	$string=~s/([^\\]|^)\%/$1\\\%/g;
+	$string=~s/([^\\]|^)(\$|_)/$1\\$2/g;
 	$string=~s/\$\$/\$\\\$/g;
 	$string=~s/\#\#/\#\\\#/g;
-        $string=~s/([^\\])(\~|\^)/$1\\$2\\strut /g;
+        $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
 	$string=&Apache::lonprintout::character_chart($string);
 	# any & or # leftover should be safe to just escape
-        $string=~s/([^\\])\&/$1\\\&/g;
-        $string=~s/([^\\])\#/$1\\\#/g;
+        $string=~s/([^\\]|^)\&/$1\\\&/g;
+        $string=~s/([^\\]|^)\#/$1\\\#/g;
 #single { or } How to escape?
     }
     return $string;