--- loncom/xml/lonxml.pm	2002/07/29 22:06:38	1.186
+++ loncom/xml/lonxml.pm	2002/09/04 21:01:33	1.193
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.186 2002/07/29 22:06:38 www Exp $
+# $Id: lonxml.pm,v 1.193 2002/09/04 21:01:33 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -333,7 +333,8 @@ sub registerurl {
     my $target = shift;
     my $result = '';
     if ($target eq 'edit') {
-        $result .="<script>\n    menu.currentURL=null;\n".
+        $result .="<script>\n".
+            "if (typeof menu != 'undefined') {menu.currentURL=null;}\n".
             &Apache::loncommon::browser_and_searcher_javascript().
                 "\n</script>\n";
     }
@@ -538,6 +539,22 @@ sub htmlclean {
     return $output;
 }
 
+sub latex_special_symbols {
+    my ($current_token,$stack,$parstack)=@_;
+    $current_token=~s/\\/\\char92 /g;
+    $current_token=~s/\^/\\char94 /g;
+    $current_token=~s/\~/\\char126 /g;
+    $current_token=~s/(&[^a-z#])/\\$1/g;
+    $current_token=~s/([^&]\#)/\\$1/g;
+    $current_token=~s/(\$|_|{|})/\\$1/g;
+    $current_token=~s/\\char92 /\\texttt{\\char92}/g;
+    $current_token=~s/>/\$>\$/g; #more
+    $current_token=~s/</\$<\$/g; #less
+    if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit
+    if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space
+    return $current_token;
+}
+
 sub inner_xmlparse {
   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
   my $finaloutput = '';
@@ -547,19 +564,12 @@ sub inner_xmlparse {
     while ($token = $$pars['-1']->get_token) {
       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
 	if ($metamode<1) {
-	    if ($target eq 'tex') {
-		my @temp_array = @$stack;
-		if ($temp_array[-1] ne 'm') {
-		    if ($temp_array[-1] ne 'tt') {
-			if ($token->[1]=~m/\^/) {$token->[1]=~s/\^/\\verb|\^|/g;}
-		    } else {
-			if ($token->[1]=~m/\^/) {$token->[1]=~s/\^/}\\verb|\^|{/g;}
-		    }
-		    if ($token->[1]=~m/>/) {$token->[1]=~s/>/\$>\$/g;}
-		    if ($token->[1]=~m/</) {$token->[1]=~s/</\$<\$/g;}
-		}
+	    my $text=$token->[1];
+	    if ($token->[0] eq 'C' && $target eq 'tex') {
+		$text = '%'.$text;
+		$text =~ s/[\n\r]//g;
 	    }
-	  $result.=$token->[1];
+	    $result.=$text;
 	}
       } elsif ($token->[0] eq 'PI') {
 	if ($metamode<1) {
@@ -622,6 +632,12 @@ sub inner_xmlparse {
 	  $result= &Apache::run::evaluate($result,$safeeval,'');
 	}
       }
+      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
+	if ($target eq 'tex') {
+	    $result=&latex_special_symbols($result,$stack,$parstack);
+	}
+      }
+
       # Encode any high ASCII characters
       if (!$Apache::lonxml::prevent_entity_encode) {
 	$result=&HTML::Entities::encode($result,"\200-\377");
@@ -748,13 +764,13 @@ sub callsub {
     }
     if (!$deleted) {
       if ($space) {
-	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
+	&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
 	$sub1="$space\:\:$sub";
 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
 					     $parstack,$parser,$safeeval,
 					     $style);
       } else {
-	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
+	&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
 	if ($metamode <1) {
 	  if (defined($token->[4]) && ($metamode < 1)) {
 	    $currentstring = $token->[4];
@@ -947,6 +963,7 @@ sub decreasedepth {
 }
 
 sub get_all_text_unbalanced {
+#there is a copy of this in lonpublisher.pm
  my($tag,$pars)= @_;
  my $token;
  my $result='';