--- loncom/xml/lonxml.pm	2004/03/19 22:01:13	1.311
+++ loncom/xml/lonxml.pm	2004/03/30 07:16:24	1.314
@@ -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.314 2004/03/30 07:16:24 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;
@@ -966,7 +967,7 @@ sub get_all_text {
 	    #never found the end tag ran out of text, throw error send back blank
 	    &error('Never found end tag for &lt;'.$tag.
 		   '&gt; current string <pre>'.
-		   &HTML::Entities::encode($result).
+		   &HTML::Entities::encode($result,'<>&"').
 		   '</pre>');
 	    if ($gotfullstack) {
 		my $newstring='</'.$tag.'>'.$result;
@@ -1143,7 +1144,7 @@ SIMPLECONTENT
 
 sub inserteditinfo {
       my ($result,$filecontents,$filetype)=@_;
-      $filecontents = &HTML::Entities::encode($filecontents);
+      $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
 #      my $editheader='<a href="#editsection">Edit below</a><hr />';
       my $xml_help = '';
       if ($filetype eq 'html') {
@@ -1328,7 +1329,7 @@ sub debug {
 	$|=1;
 	my $request=$Apache::lonxml::request;
 	if (!$request) { $request=Apache->request; }
-	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0])."</pre></font>\n");
+	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
     }
 }