--- loncom/xml/londefdef.pm	2003/08/26 15:05:43	1.167
+++ loncom/xml/londefdef.pm	2004/04/01 15:18:06	1.205
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.167 2003/08/26 15:05:43 sakharuk Exp $
+# $Id: londefdef.pm,v 1.205 2004/04/01 15:18:06 albertel Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -50,8 +50,8 @@ use Apache::File();
 use Image::Magick;
 use Apache::lonmenu();
 use Apache::lonmeta();
+use Apache::Constants qw(:common);
 
-$Apache::londefdef::TD_redirection=0;
 
 BEGIN {
 
@@ -59,6 +59,15 @@ BEGIN {
 
 }
 
+sub initialize_londefdef {
+    $Apache::londefdef::TD_redirection=0;
+    @Apache::londefdef::table = ();
+    $Apache::londefdef::select=0;
+    @Apache::londefdef::description=();
+    $Apache::londefdef::DD_redirection=0;
+    $Apache::londefdef::DT_redirection=0;
+}
+
 #======================= TAG SUBROUTINES =====================
 #-- <output>
 sub start_output {
@@ -73,11 +82,10 @@ sub end_output {
 }
 #-- <m> tag
 sub start_m {
-    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
-	$Apache::lonxml::prevent_entity_encode++;
-	my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
+    my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
+    if ($target eq 'web' || $target eq 'analyze') {
 	$inside ='\\documentstyle{article}'.$inside;
 	&Apache::lonxml::debug("M is starting with:$inside:");
 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
@@ -92,11 +100,15 @@ sub start_m {
 	    $Apache::lontexconvert::errorstring='';
 	}
 	#&Apache::lonxml::debug("M is ends with:$currentstring:");
+	$Apache::lonxml::post_evaluate=0;
     } elsif ($target eq 'tex') {
-	$currentstring = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
+	$currentstring = $inside;
+	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
+	if ($eval eq 'on') {
+	    $currentstring=&Apache::run::evaluate($currentstring,$safeeval,$$parstack[-1]);
+	}
 	if ($currentstring=~/^(\s*\\\\\s*)*$/) {$currentstring = ' \vskip 0 mm ';}
-    } else {
-	my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
+	$Apache::lonxml::post_evaluate=0;
     }
     return $currentstring;
 }
@@ -104,11 +116,8 @@ sub start_m {
 sub end_m {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
-	$Apache::lonxml::prevent_entity_encode--;
-    } elsif ($target eq 'tex') {
+    if ($target eq 'tex') {
 	$currentstring = "";
-    } elsif ($target eq 'meta') {
     }
     return $currentstring;
 }
@@ -134,26 +143,13 @@ sub end_tthoption {
     return $result;
 }
 
-#-- <html> tag    
+#-- <html> tag (end tag optional)
 sub start_html {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($ENV{'browser.mathml'}) {
-	&tth::ttminit();
-	if ($ENV{'browser.unicode'}) {
-	    &tth::ttmoptions('-L -u1');
-	} else {
-	    &tth::ttmoptions('-L -u0');
-	}
-    } else {
-	&tth::tthinit();
-	if ($ENV{'browser.unicode'}) {
-	    &tth::tthoptions('-L -u1');
-	} else {
-	    &tth::tthoptions('-L -u0');
-	}
-    }
-    if ($target eq 'web') {
+    my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'};
+    &Apache::lontexconvert::init_tth();
+    if ($target eq 'web' || $target eq 'edit') {
 	$currentstring = &Apache::lonxml::xmlbegin().
 	    &Apache::lonxml::fontsettings();     
     } elsif ($target eq 'tex') {
@@ -182,7 +178,7 @@ sub end_html {
     return $currentstring;
 }
 
-#-- <head> tag
+#-- <head> tag (end tag optional)
 sub start_head {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -195,14 +191,14 @@ sub start_head {
 sub end_head {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
+    if ($target eq 'web' && $ENV{'request.state'} eq 'published') {
 	$currentstring = &Apache::lonmenu::registerurl(undef,$target).
 	    $token->[2];    
     } 
     return $currentstring;
 }
 
-#-- <map> tag
+#-- <map> tag (end tag required)
 sub start_map {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -221,13 +217,15 @@ sub end_map {
     return $currentstring;
 }
 
-#-- <select> tag
+#-- <select> tag (end tag required)
 sub start_select {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
-    } 
+    }  elsif ($target eq 'tex') {
+	$Apache::londefdef::select=0;
+    }
     return $currentstring;
 }
 
@@ -240,13 +238,20 @@ sub end_select {
     return $currentstring;
 }
 
-#-- <option> tag
+#-- <option> tag (end tag optional)
 sub start_option {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
-    } 
+    } elsif ($target eq 'tex') {
+	$Apache::londefdef::select++;
+	if ($Apache::londefdef::select == 1) {
+	    $currentstring='\noindent\fbox{'.&Apache::lonxml::get_param('value',$parstack,$safeeval).'}\keephidden{';
+	} else {
+	    $currentstring='\keephidden{';
+	}
+    }
     return $currentstring;
 }
 
@@ -255,11 +260,13 @@ sub end_option {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];    
-    } 
+    }  elsif ($target eq 'tex') {
+	$currentstring='}';
+    }
     return $currentstring;
 }
 
-#-- <input> tag
+#-- <input> tag (end tag forbidden)
 sub start_input {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -278,7 +285,7 @@ sub end_input {
     return $currentstring;
 }
 
-#-- <textarea> tag
+#-- <textarea> tag (end tag required)
 sub start_textarea {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -297,7 +304,7 @@ sub end_textarea {
     return $currentstring;
 }
 
-#-- <form> tag
+#-- <form> tag (end tag required)
 sub start_form {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -316,7 +323,7 @@ sub end_form {
     return $currentstring;
 }
 
-#-- <title> tag
+#-- <title> tag (end tag required)
 sub start_title {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -327,7 +334,7 @@ sub start_title {
     }
     if ($target eq 'meta') {
 	$currentstring='<title>';
-	&start_output();
+	&start_output($target);
     }
     return $currentstring;
 }
@@ -341,13 +348,13 @@ sub end_title {
 	$currentstring .= '}';
     }  
     if ($target eq 'meta') {
-	&end_output();
+	&end_output($target);
 	$currentstring='</title>';
     } 
     return $currentstring;
 }
 
-#-- <meta> tag
+#-- <meta> tag (end tag forbidden)
 sub start_meta {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -382,7 +389,7 @@ sub start_meta {
             my $display=&Apache::lonxml::get_param
 		('display',$parstack,$safeeval,undef,1);
             if ($display) {
-		$display=&HTML::Entities::encode($display);
+		$display=&HTML::Entities::encode($display,'<>&"');
 		$currentstring.='<'.$name.'.display>'.$display.
                                '</'.$name.'.display>';
             }
@@ -410,7 +417,7 @@ sub end_meta {
 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
 	if ((not defined $content) && (not defined $name)) {
-	    &Apache::lonxml::startredirection();
+	    &Apache::lonxml::endredirection();
 	}
     }
     return $currentstring;
@@ -461,12 +468,17 @@ sub end_accessrule {
     return $currentstring;
 }
 
-#-- <body> tag
+#-- <body> tag (end tag required)
 sub start_body {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
-	if (!$Apache::lonxml::registered) {
+	if ($Apache::lonhomework::parsing_a_problem) {
+	    &Apache::lonxml::warning("<body> tag found inside of <problem> tag this can cause problems.");
+	    return '';
+	}
+	if (!$Apache::lonxml::registered && 
+	    $ENV{'request.state'} eq 'published') {
 	    $currentstring.='<head>'.
 		&Apache::lonmenu::registerurl(undef,$target).'</head>';
 	}
@@ -519,12 +531,13 @@ sub start_body {
 	if ($ENV{'request.state'} ne 'published') {
 	    $currentstring.=(<<EDITBUTTON);
 		<form method="post">
-		<input type="submit" name="editmode" value="Edit" />
+		<input type="submit" name="editmode" accesskey="e" value="Edit" />
 		</form>
 EDITBUTTON
 	} else {
 	    $currentstring.=&Apache::lonmenu::menubuttons(undef,$target,1);
 	}
+	$currentstring.=&Apache::lonxml::message_location();
     } elsif ($target eq 'tex') {
 	$currentstring = '\begin{document}';  
     } 
@@ -537,12 +550,12 @@ sub end_body {
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
-	$currentstring = '\end{document}';  
+	$currentstring = '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';  
     } 
     return $currentstring;
 }
 
-#-- <center> tag
+#-- <center> tag (end tag required)
 sub start_center {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -565,7 +578,7 @@ sub end_center {
     return $currentstring;
 }
 
-#-- <b> tag
+#-- <b> tag (end tag required)
 sub start_b {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -588,7 +601,7 @@ sub end_b {
     return $currentstring;
 }
 
-#-- <strong> tag
+#-- <strong> tag (end tag required)
 sub start_strong {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -603,8 +616,7 @@ sub start_strong {
 sub end_strong {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
-	
+    if ($target eq 'web') {	
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
 	$currentstring = '}';  
@@ -612,7 +624,7 @@ sub end_strong {
     return $currentstring;
 }
 
-#-- <h1> tag
+#-- <h1> tag (end tag required)
 sub start_h1 {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -620,7 +632,7 @@ sub start_h1 {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	my $pre;
-	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
 	if (($align eq 'center') || (not defined $align)) {
 	    $pre='\begin{center}';
 	} elsif ($align eq 'left') {
@@ -633,7 +645,7 @@ sub start_h1 {
 	$currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
     } elsif ($target eq 'meta') {
 	$currentstring='<subject>';
-	&start_output();
+	&start_output($target);
     }
     return $currentstring;
 }
@@ -655,7 +667,7 @@ sub end_h1 {
 	}
 	$currentstring .= '}}'.$post;
     } elsif ($target eq 'meta') {
-	&end_output();
+	&end_output($target);
 	$currentstring='</subject>';
     } 
     return $currentstring;
@@ -876,14 +888,14 @@ sub end_h6 {
     return $currentstring;
 }
 
-#--- <cite> tag
+#--- <cite> tag (end tag required)
 sub start_cite {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\\textit{";
+	$currentstring .= '\textit{';
     }
     return $currentstring;
 }
@@ -894,12 +906,12 @@ sub end_cite {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= "}";
+	$currentstring .= '}';
     }
     return $currentstring;
 }
 
-#-- <i> tag
+#-- <i> tag (end tag required)
 sub start_i {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -922,14 +934,14 @@ sub end_i {
     return $currentstring;
 }
 
-#-- <address> tag
+#-- <address> tag (end tag required)
 sub start_address {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\\textit{";
+	$currentstring .= '\textit{';
     }
     return $currentstring;
 }
@@ -940,19 +952,19 @@ sub end_address {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= "}";
+	$currentstring .= '}';
     }
     return $currentstring;
 }
 
-#-- <dfn> tag
+#-- <dfn> tag (end tag required)
 sub start_dfn {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\\textit{";
+	$currentstring .= '\textit{';
     } 
     return $currentstring;
 }
@@ -963,12 +975,12 @@ sub end_dfn {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= "}";
+	$currentstring .= '}';
     }
     return $currentstring;
 }
 
-#-- <tt> tag
+#-- <tt> tag (end tag required)
 sub start_tt {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -991,14 +1003,14 @@ sub end_tt {
     return $currentstring;
 }
 
-#-- <kbd> tag
+#-- <kbd> tag (end tag required)
 sub start_kbd {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\\texttt";
+	$currentstring .= '\texttt{';
     }
     return $currentstring;
 }
@@ -1009,12 +1021,12 @@ sub end_kbd {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= "}";
+	$currentstring .= '}';
     }
     return $currentstring;
 }
 
-#-- <code> tag
+#-- <code> tag (end tag required)
 sub start_code {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1037,7 +1049,7 @@ sub end_code {
     return $currentstring;
 }
 
-#-- <em> tag
+#-- <em> tag (end tag required)
 sub start_em {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1060,14 +1072,14 @@ sub end_em {
     return $currentstring;
 }
 
-#-- <q> tag
+#-- <q> tag (end tag required)
 sub start_q {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\\emph{";
+	$currentstring .= '\emph{';
     }
     return $currentstring;
 }
@@ -1078,35 +1090,56 @@ sub end_q {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= "}";
+	$currentstring .= '}';
     } 
     return $currentstring;
 }
 
-#-- <p> tag
+#-- <p> tag (end tag optional)
+#optional attribute - align="center|left|right"
 sub start_p {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	my $signal=1;
-	foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}}
-	if ($signal) {$currentstring .= '\par ';}
+	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	if ($align eq 'center') {
+	    $currentstring='\begin{center}\par';
+	} elsif ($align eq 'right') {
+	    $currentstring='\makebox['.$ENV{'form.textwidth'}.']{\hfill\llap{';
+	} elsif ($align eq 'left') {
+	    $currentstring='\noindent\makebox['.$ENV{'form.textwidth'}.']{\rlap{';
+	}
+	my $signal=1;#<p> does not work inside <b>...</b> 
+	foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}
+	if (!$signal) {$currentstring = '';}
+	}
     }
     return $currentstring;
 }
 
 sub end_p {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
+    } elsif ($target eq 'tex') {
+	if ($$tagstack[-1] eq 'p') {
+	    my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	    if ($align eq 'center') {		
+		$currentstring .= '\end{center}';
+	    } elsif ($align eq 'right') {
+		$currentstring .= '}}';
+	    } elsif ($align eq 'left') {
+		$currentstring .= '}\hfill}';
+	    }
+	}
     }
     return $currentstring;
 }
 
-#-- <br> tag
+#-- <br> tag (end tag forbidden)
 sub start_br {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $currentstring = '';
@@ -1129,7 +1162,7 @@ sub end_br {
     return $currentstring;
 }
 
-#-- <big> tag
+#-- <big> tag (end tag required)
 sub start_big {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1152,7 +1185,7 @@ sub end_big {
     return $currentstring;
 }
 
-#-- <small> tag
+#-- <small> tag (end tag required)
 sub start_small {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1175,7 +1208,7 @@ sub end_small {
     return $currentstring;
 }
 
-#-- <basefont> tag
+#-- <basefont> tag (end tag forbidden)
 sub start_basefont {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $currentstring = '';
@@ -1204,15 +1237,13 @@ sub end_basefont {
     return $currentstring;
 }
 
-#-- <font> tag
+#-- <font> tag (end tag required)
 sub start_font {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $currentstring = '';
     if ($target eq 'web') {
 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
-	if ($face=~/symbol/i) {
-	    $Apache::lonxml::prevent_entity_encode++;
-	} else {
+	if ($face!~/symbol/i) {
 	    if (($ENV{'browser.fontenhance'} eq 'on') || 
 		($ENV{'browser.blackwhite'} eq 'on')) { return ''; }
 	}
@@ -1230,8 +1261,6 @@ sub end_font {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $currentstring = '';
     if ($target eq 'web') {
-	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
-	if ($face=~/symbol/i) {$Apache::lonxml::prevent_entity_encode--;}
 	$currentstring = $token->[2];    
     }  elsif ($target eq 'tex') {
 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
@@ -1242,7 +1271,7 @@ sub end_font {
     return $currentstring;
 }
  
-#-- <strike> tag
+#-- <strike> tag (end tag required)
 sub start_strike {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1268,7 +1297,7 @@ sub end_strike {
     return $currentstring;
 }
 
-#-- <s> tag
+#-- <s> tag (end tag required)
 sub start_s {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1294,14 +1323,14 @@ sub end_s {
     return $currentstring;
 }
 
-#-- <sub> tag
+#-- <sub> tag (end tag required)
 sub start_sub {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\$_{ ";
+	$currentstring .= '\ensuremath{_{';
     } 
     return $currentstring;
 }
@@ -1312,19 +1341,19 @@ sub end_sub {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= " }\$";
+	$currentstring .= '}}';
     }
     return $currentstring;
 }
 
-#-- <sup> tag
+#-- <sup> tag (end tag required)
 sub start_sup {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\$^{ ";
+	$currentstring .= '\ensuremath{^{';
     } 
     return $currentstring;
 }
@@ -1335,12 +1364,12 @@ sub end_sup {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= " }\$";
+	$currentstring .= '}}';
     }
     return $currentstring;
 }
 
-#-- <hr> tag
+#-- <hr> tag (end tag forbidden)
 sub start_hr {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -1380,7 +1409,7 @@ sub end_hr {
     return $currentstring;
 }
 
-#-- <div> tag
+#-- <div> tag (end tag required)
 sub start_div {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1399,7 +1428,7 @@ sub end_div {
     return $currentstring;
 }
 
-#-- <a> tag
+#-- <a> tag (end tag required)
 sub start_a {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -1421,7 +1450,7 @@ sub start_a {
 }
 
 sub end_a {
-    my ($target,$token,$tagstack,$parstack,$safeeval) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
@@ -1429,9 +1458,9 @@ sub end_a {
     return $currentstring;
 }
 
-#-- <li> tag
+#-- <li> tag (end tag optional)
 sub start_li {
-    my ($target,$token,$tagstack,$parstack,$safeeval) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
@@ -1459,7 +1488,7 @@ sub end_li {
     return $currentstring;
 }
 
-#-- <u> tag
+#-- <u> tag (end tag required)
 sub start_u {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1485,7 +1514,7 @@ sub end_u {
     return $currentstring;
 }
 
-#-- <ul> tag
+#-- <ul> tag (end tag required)
 sub start_ul {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -1528,7 +1557,7 @@ sub end_ul {
     return $currentstring;
 }
 
-#-- <menu> tag
+#-- <menu> tag (end tag required)
 sub start_menu {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1551,7 +1580,7 @@ sub end_menu {
     return $currentstring;
 }
 
-#-- <dir> tag
+#-- <dir> tag (end tag required)
 sub start_dir {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -1574,7 +1603,7 @@ sub end_dir {
     return $currentstring;
 }
 
-#-- <ol> tag
+#-- <ol> tag (end tag required)
 sub start_ol {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -1627,78 +1656,115 @@ sub end_ol {
     return $currentstring;
 }
 
-#-- <dl> tag
+#-- <dl> tag (end tag required)
 sub start_dl {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	$currentstring = '\begin{description}';  
+	$currentstring = '\begin{description}';
+	@Apache::londefdef::description=();
+	$Apache::londefdef::DD_redirection=0;
+	$Apache::londefdef::DT_redirection=0;
     } 
     return $currentstring;
 }
 
 sub end_dl {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
-	$currentstring = '\end{description}';  
+	if ($Apache::londefdef::DT_redirection) {
+	    my $data=&item_cleanup;
+	    push @Apache::londefdef::description,'\item['.$data.']';
+	    $Apache::londefdef::DT_redirection=0;
+	} elsif ($Apache::londefdef::DD_redirection) {
+	    $Apache::londefdef::description[-1].=&Apache::lonxml::endredirection();
+	}
+	foreach my $element (@Apache::londefdef::description) {
+	    $currentstring.=' '.$element.' ';
+	}
+	@Apache::londefdef::description=();
+	$currentstring.='\end{description}';  
     } 
     return $currentstring;
 }
 
-#-- <dt> tag
+#-- <dt> tag (end tag optional)
 sub start_dt {
-    my ($target,$token) = @_;
-    my $currentstring = '';
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
+    my $currentstring='';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	$currentstring = '\item[';  
+	if ($Apache::londefdef::DT_redirection) {
+	    my $data=&item_cleanup;
+	    push @Apache::londefdef::description,'\item['.$data.']';
+	    $Apache::londefdef::DT_redirection=0;
+	} elsif ($Apache::londefdef::DD_redirection) {
+	    $Apache::londefdef::description[-1].=&Apache::lonxml::endredirection();
+	    $Apache::londefdef::DD_redirection=0;
+	}
+	&Apache::lonxml::startredirection();
+	$Apache::londefdef::DT_redirection=1;
     } 
     return $currentstring;
 }
 
 sub end_dt {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];    
     } elsif ($target eq 'tex') {
-	$currentstring = ']';  
+	my $data=&item_cleanup;
+	push @Apache::londefdef::description,'\item['.$data.']';
+	$Apache::londefdef::DT_redirection=0;
     } 
     return $currentstring;
 }
 
-#-- <dd> tag
+sub item_cleanup {
+    my $item=&Apache::lonxml::endredirection();
+    $item=~s/\\begin{center}//g;
+    $item=~s/\\end{center}//g;
+    return $item;
+}
+
+#-- <dd> tag (end tag optional)
 sub start_dd {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	if ($$tagstack[-2] eq 'dl') {
-	    $currentstring = ' \item [] ';  
-	} elsif ($$tagstack[-2] eq 'dt') {
-	    $currentstring = ']'; 
+	if ($Apache::londefdef::DT_redirection) {
+	    my $data=&item_cleanup;
+	    push @Apache::londefdef::description,'\item['.$data.']';
+	    $Apache::londefdef::DT_redirection=0;
 	}
+	$Apache::londefdef::DD_redirection=1;
+	&Apache::lonxml::startredirection();
     } 
     return $currentstring;
 }
 
 sub end_dd {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];    
-    } 
+    }  elsif ($target eq 'tex') {
+	$Apache::londefdef::description[-1].=&Apache::lonxml::endredirection();
+	$Apache::londefdef::DD_redirection=0;
+    }
     return $currentstring;
 }
 
-#-- <table> tag
+#-- <table> tag (end tag required)
 sub start_table {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $textwidth='';
@@ -1721,13 +1787,14 @@ sub start_table {
 	if (not defined $TeXwidth) {
 	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,$safeeval,undef,1);
 	    if ($htmlwidth=~/%/) {
+                $Apache::londefdef::table[-1]{'percent'}=1;
 		$htmlwidth=~/(\d+)/;
-		my $value=$1*$textwidth/100;
-		$Apache::londefdef::table[-1]{'width'}=$value;
+		$Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;;
 	    } else {
 		$Apache::londefdef::table[-1]{'width'}=$textwidth;
 	    }
 	} elsif ($TeXwidth=~/%/) {
+	    $Apache::londefdef::table[-1]{'percent'}=1;
 	    $TeXwidth=~/(\d+)/;
 	    my $value=$1*$textwidth/100;
             $Apache::londefdef::table[-1]{'width'}=$value;
@@ -1807,6 +1874,17 @@ sub end_table {
 		$length_row_final[$jn]=0.9*$available_length/$needed;
 	    }
 	}
+        #recalculation for the use of all available width if width is defined in %
+        if ($Apache::londefdef::table[-1]{'percent'}==1) {
+	    my $current=0; 
+	    for (my $i=0;$i<=$#length_row_final;$i++) {  
+		$current+=$length_row_final[$i];
+	    }
+	    my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
+	    for (my $i=0;$i<=$#length_row_final;$i++) {  
+		$length_row_final[$i]*=$coef;
+	    }
+	}
 	#fill the table
 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 	    for (my $jn=0;$jn<=$#length_row_final;$jn++) {
@@ -1936,14 +2014,11 @@ sub end_td_tex {
 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 		foreach my $elementdata (@newdata) {
-		    $elementdata=~s/^\s+(\S.*)/$1/; 
-		    $elementdata=~s/(.*\S)\s+$/$1/;
-		    $elementdata=~s/(\s)+/$1/;
-		    my $lengthnewdata=1.8*length($elementdata);
+		    my $lengthnewdata=1.8*LATEX_length($elementdata);
 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 		}
 	    } else {
-		$current_length=1.8*length($data);
+		$current_length=1.8*LATEX_length($data);
 	    }
 	    $Apache::londefdef::table[-1]{'length'} .= $current_length.',';
 	    $Apache::londefdef::table[-1]{'TeXlength'} .= '0,';
@@ -2024,15 +2099,18 @@ sub end_th {
     }
     return $currentstring;
 }
-#-- <img> tag
+#-- <img> tag (end tag forbidden)
 sub start_img {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 					 undef,1);
+    if (not $src and ($target eq 'web' or $target eq 'tex')) { 
+	my $inside = &Apache::lonxml::get_all_text("/img",$parser);
+	&Apache::lonnet::logthis("inside was $inside");
+	return '';
+    }
     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$src;
     my $currentstring = '';
-    my $width_param = '';
-    my $height_param = '';
     my $scaling = .3;
     if ($target eq 'web') {
 	if ($ENV{'browser.imagesuppress'} ne 'on') {
@@ -2048,38 +2126,11 @@ sub start_img {
 	}
     } elsif ($target eq 'tex') {
 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
-	&image_replication($src);
 	#if original gif/jpg/png file exist do following:
-	if (-e $src) {          
-	    #defines the default size of image
-	    my $image = Image::Magick->new;
-	    my $current_figure = $image->Read($src);
-	    $width_param = $image->Get('width') * $scaling;;
-	    $height_param = $image->Get('height') * $scaling;;
-	    undef $image;
-	    #do we have any specified size of the picture?
-	    my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
-	    my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
-	    my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
-						   undef,1);
-	    if ($TeXwidth ne '') {  
-		if ($TeXwidth=~/(\d+)\s*\%/) {
-		    $width_param = $1*$ENV{'form.textwidth'}/100;
-		} else { 
-		    $width_param = $TeXwidth;
-		}
-	    } elsif ($TeXheight ne '') {
-		$width_param = $TeXheight/$height_param*$width_param;
-	    } elsif ($width ne '') {
-		$width_param = $width*$scaling;      
-	    }
-	    if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}
-	    my $file;
-	    my $path;	
-	    if ($src =~ m!(.*)/([^/]*)$!) {
-		$file = $2; 
-		$path = $1.'/'; 
-	    } 
+	if (-e $src) {
+	    #what is the image size?
+	    my $width_param=&image_size($src,$scaling,$parstack,$safeeval);
+            my ($file,$path)=&file_path($src); 
 	    my $newsrc = $src;
 	    $newsrc =~ s/\.(gif|jpg|png)$/.eps/i;
 	    $file=~s/\.(gif|jpg|png)$/.eps/i;
@@ -2099,12 +2150,8 @@ sub start_img {
 			$currentstring .= '\vskip 1 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 		    }
 		} else {
-		    #there aren't eps or ps - so create eps 
-		    my $temp_file;
-		    my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
-		    $temp_file = Apache::File->new('>>'.$filename); 
-		    print $temp_file "$src\n";
-		    $currentstring .= '\vskip 1 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
+		    #care about eps dynamical generation
+		    $currentstring.='\vskip 1 mm '.&eps_generation($src,$file,$width_param);
 		}
 	    }
 	} else {
@@ -2121,6 +2168,45 @@ sub start_img {
 		#<allow> tag will care about replication 
 	    }
 	}
+    } elsif ($target eq 'edit') {
+	$currentstring .=&Apache::edit::tag_start($target,$token);
+	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
+	    &Apache::edit::browse('src',undef,'alt').' '.
+	    &Apache::edit::search('src',undef,'alt').'<br />';
+	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
+	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
+	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
+	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
+	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
+	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
+	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval);
+	my $alt=&Apache::lonxml::get_param('alt',$parstack,$safeeval);
+	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
+	my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
+	$currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
+	if ($width) { $currentstring.=' width="'.$width.'" '; }
+	if ($height) { $currentstring.=' height="'.$height.'" '; }
+	$currentstring .= ' />';
+    } elsif ($target eq 'modified') {
+	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
+						     $safeeval,'src','alt',
+						     'TeXwidth','TeXheight',
+						     'width','height');
+	$src=$token->[2]{'src'};
+	if (!$token->[2]{'width'} && !$token->[2]{'height'}) {
+	    $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
+	    &image_replication($src);
+	    if (-e $src) {
+		my $image = Image::Magick->new;
+		my ($width, $height, $size, $format) = $image->Ping($src);
+		if ($width && $height) {
+		    $token->[2]{'width'} =$width;
+		    $token->[2]{'height'}=$height;
+		    $constructtag=1;
+		}
+	    }
+	}
+	if ($constructtag) {$currentstring=&Apache::edit::rebuild_tag($token);}
     }
     return $currentstring;
 }
@@ -2136,7 +2222,7 @@ sub end_img {
     return $currentstring;
 }
 
-#-- <applet> tag
+#-- <applet> tag (end tag required)
 sub start_applet {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     
@@ -2161,7 +2247,16 @@ sub start_applet {
 	    $currentstring='[APPLET: '.$alttag.']';
 	}
     } elsif ($target eq 'tex') {
-	$currentstring = " \\begin{figure} ";
+	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
+					       $safeeval,undef,1);
+	unless ($alttag) {
+	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
+						undef,1);
+	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
+					     $code);
+	}
+	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
+	    '.}\end{center}';
     } 
     return $currentstring;
 }
@@ -2172,12 +2267,11 @@ sub end_applet {
     if ($target eq 'web') {
 	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring = " \\end{figure}";
     } 
     return $currentstring;
 }
 
-#-- <embed> tag
+#-- <embed> tag (end tag optional/required)
 sub start_embed {    
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
@@ -2195,7 +2289,6 @@ sub start_embed {
 	    $currentstring='[EMBED: '.$alttag.']';
 	}
     } elsif ($target eq 'tex') {
-	$currentstring = " \\begin{figure} ";  
     } 
     return $currentstring;
 }
@@ -2205,13 +2298,12 @@ sub end_embed {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
-    } elsif ($target eq 'tex') {
-	$currentstring = " \\end{figure}";  
+    } elsif ($target eq 'tex') {  
     } 
     return $currentstring;
 }
 
-#-- <param> tag
+#-- <param> tag (end tag forbidden)
 sub start_param {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     if (&Apache::lonxml::get_param
@@ -2225,7 +2317,6 @@ sub start_param {
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	$currentstring = " \\begin{figure} ";  
     } 
     return $currentstring;
 }
@@ -2236,7 +2327,6 @@ sub end_param {
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
-	$currentstring = " \\end{figure}";  
     } 
     return $currentstring;
 }
@@ -2268,12 +2358,14 @@ sub end_allow {
     return '';
 }
 
-#-- Frames
+#-- Frames (end tag required)
+#-- <frameset>
 sub start_frameset {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') { 
-	if (!$Apache::lonxml::registered) {
+	if (!$Apache::lonxml::registered &&
+	    $ENV{'request.state'} eq 'published') {
 	    $currentstring.='<head>'.
 		&Apache::lonmenu::registerurl(undef,$target).'</head>';
 	}
@@ -2313,7 +2405,7 @@ sub end_frameset {
     return $currentstring;
 }
 
-#-- <xmp>
+#-- <xmp> (end tag required)
 sub start_xmp {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -2336,7 +2428,7 @@ sub end_xmp {
     return $currentstring;
 }
 
-#-- <pre>
+#-- <pre> (end tag required)
 sub start_pre {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -2419,7 +2511,7 @@ sub end_blankspace {
     return $currentstring;
 }
 
-#-- <abbr> tag
+#-- <abbr> tag (end tag required)
 sub start_abbr {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2438,7 +2530,7 @@ sub end_abbr {
     return $currentstring;
 }
 
-#-- <acronym> tag
+#-- <acronym> tag (end tag required)
 sub start_acronym {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2457,7 +2549,7 @@ sub end_acronym {
     return $currentstring;
 }
 
-#-- <area> tag
+#-- <area> tag (end tag forbidden)
 sub start_area {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2476,7 +2568,7 @@ sub end_area {
     return $currentstring;
 }
 
-#-- <base> tag
+#-- <base> tag (end tag forbidden)
 sub start_base {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2495,7 +2587,7 @@ sub end_base {
     return $currentstring;
 }
 
-#-- <bdo> tag
+#-- <bdo> tag (end tag required)
 sub start_bdo {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2514,7 +2606,7 @@ sub end_bdo {
     return $currentstring;
 }
 
-#-- <bgsound> tag
+#-- <bgsound> tag (end tag optional)
 sub start_bgsound {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2533,7 +2625,7 @@ sub end_bgsound {
     return $currentstring;
 }
 
-#-- <blink> tag
+#-- <blink> tag (end tag required)
 sub start_blink {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2552,7 +2644,7 @@ sub end_blink {
     return $currentstring;
 }
 
-#-- <blockquote> tag
+#-- <blockquote> tag (end tag required)
 sub start_blockquote {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2571,7 +2663,7 @@ sub end_blockquote {
     return $currentstring;
 }
 
-#-- <button> tag
+#-- <button> tag (end tag required)
 sub start_button {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2590,7 +2682,7 @@ sub end_button {
     return $currentstring;
 }
 
-#-- <caption> tag
+#-- <caption> tag (end tag required)
 sub start_caption {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2609,7 +2701,7 @@ sub end_caption {
     return $currentstring;
 }
 
-#-- <col> tag
+#-- <col> tag (end tag forbdden)
 sub start_col {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2628,7 +2720,7 @@ sub end_col {
     return $currentstring;
 }
 
-#-- <colgroup> tag
+#-- <colgroup> tag (end tag optional)
 sub start_colgroup {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2647,7 +2739,7 @@ sub end_colgroup {
     return $currentstring;
 }
 
-#-- <del> tag
+#-- <del> tag (end tag required)
 sub start_del {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2666,7 +2758,7 @@ sub end_del {
     return $currentstring;
 }
 
-#-- <fieldset> tag
+#-- <fieldset> tag (end tag required)
 sub start_fieldset {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2685,7 +2777,7 @@ sub end_fieldset {
     return $currentstring;
 }
 
-#-- <frame> tag
+#-- <frame> tag (end tag forbidden)
 sub start_frame {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2704,7 +2796,7 @@ sub end_frame {
     return $currentstring;
 }
 
-#-- <iframe> tag
+#-- <iframe> tag (end tag required)
 sub start_iframe {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2723,7 +2815,7 @@ sub end_iframe {
     return $currentstring;
 }
 
-#-- <ins> tag
+#-- <ins> tag (end tag required)
 sub start_ins {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2742,7 +2834,7 @@ sub end_ins {
     return $currentstring;
 }
 
-#-- <isindex> tag
+#-- <isindex> tag (end tag forbidden)
 sub start_isindex {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2761,7 +2853,7 @@ sub end_isindex {
     return $currentstring;
 }
 
-#-- <keygen> tag
+#-- <keygen> tag (end tag forbidden)
 sub start_keygen {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2799,7 +2891,7 @@ sub end_label {
     return $currentstring;
 }
 
-#-- <layer> tag
+#-- <layer> tag (end tag required)
 sub start_layer {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2818,7 +2910,7 @@ sub end_layer {
     return $currentstring;
 }
 
-#-- <legend> tag
+#-- <legend> tag (end tag required)
 sub start_legend {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2837,7 +2929,7 @@ sub end_legend {
     return $currentstring;
 }
 
-#-- <link> tag
+#-- <link> tag (end tag forbidden)
 sub start_link {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2856,7 +2948,7 @@ sub end_link {
     return $currentstring;
 }
 
-#-- <marquee> tag
+#-- <marquee> tag (end tag optional)
 sub start_marquee {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2875,8 +2967,8 @@ sub end_marquee {
     return $currentstring;
 }
 
-#-- <malticol> tag
-sub start_malticol {
+#-- <multicol> tag (end tag required)
+sub start_multicol {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
@@ -2885,7 +2977,7 @@ sub start_malticol {
     return $currentstring;
 }
 
-sub end_malticol {
+sub end_multicol {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
@@ -2894,13 +2986,15 @@ sub end_malticol {
     return $currentstring;
 }
 
-#-- <nobr> tag
+#-- <nobr> tag (end tag required)
 sub start_nobr {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
-    } 
+    }  elsif ($target eq 'tex') {
+	$currentstring='\mbox{';
+    }
     return $currentstring;
 }
 
@@ -2909,11 +3003,13 @@ sub end_nobr {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];    
-    } 
+    }   elsif ($target eq 'tex') {
+	$currentstring='}';
+    }
     return $currentstring;
 }
 
-#-- <noembed> tag
+#-- <noembed> tag (end tag required)
 sub start_noembed {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2932,7 +3028,7 @@ sub end_noembed {
     return $currentstring;
 }
 
-#-- <noframes> tag
+#-- <noframes> tag (end tag required)
 sub start_noframes {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2951,7 +3047,7 @@ sub end_noframes {
     return $currentstring;
 }
 
-#-- <nolayer> tag
+#-- <nolayer> tag (end tag required)
 sub start_nolayer {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2970,7 +3066,7 @@ sub end_nolayer {
     return $currentstring;
 }
 
-#-- <noscript> tag
+#-- <noscript> tag (end tag required)
 sub start_noscript {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -2989,7 +3085,7 @@ sub end_noscript {
     return $currentstring;
 }
 
-#-- <object> tag
+#-- <object> tag (end tag required)
 sub start_object {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3008,7 +3104,7 @@ sub end_object {
     return $currentstring;
 }
 
-#-- <optgroup> tag
+#-- <optgroup> tag (end tag required)
 sub start_optgroup {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3027,13 +3123,15 @@ sub end_optgroup {
     return $currentstring;
 }
 
-#-- <samp> tag
+#-- <samp> tag (end tag required)
 sub start_samp {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
-    } 
+    } elsif ($target eq 'tex') {
+	$currentstring='\texttt{';
+    }
     return $currentstring;
 }
 
@@ -3042,7 +3140,9 @@ sub end_samp {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];    
-    } 
+    } elsif ($target eq 'tex') {
+	$currentstring='}';
+    }
     return $currentstring;
 }
 
@@ -3065,7 +3165,7 @@ sub end_server {
     return $currentstring;
 }
 
-#-- <spacer> tag
+#-- <spacer> tag (end tag forbidden)
 sub start_spacer {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3084,7 +3184,7 @@ sub end_spacer {
     return $currentstring;
 }
 
-#-- <span> tag
+#-- <span> tag (end tag required)
 sub start_span {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3103,7 +3203,7 @@ sub end_span {
     return $currentstring;
 }
 
-#-- <tbody> tag
+#-- <tbody> tag (end tag optional)
 sub start_tbody {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3223,22 +3323,71 @@ sub end_hideweboutput {
 
 sub image_replication {
     my $src = shift;
-    if (not -e $src) {
-	#replicates image itself
-	&Apache::lonnet::repcopy($src);
-	#replicates eps or ps 
-	my $newsrc = $src;
-	$newsrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
-	if (not -e $newsrc) {
-	    if (&Apache::lonnet::repcopy($newsrc) ne 'OK') {
-		$newsrc =~ s/\.eps$/\.ps/;
-		&Apache::lonnet::repcopy($newsrc);
-	    }
-	}
+    if (not -e $src) { &Apache::lonnet::repcopy($src); }
+    #replicates eps or ps 
+    my $epssrc = my $pssrc = $src;
+    $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
+    $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
+    if (not -e $epssrc && not -e $pssrc) {
+	my $result=&Apache::lonnet::repcopy($epssrc);
+	if ($result ne OK) { &Apache::lonnet::repcopy($pssrc); }
     }
     return '';
 }
 
+sub image_size {
+    my ($src,$scaling,$parstack,$safeeval)=@_;
+    #size of image from gif/jpg/jpeg/png 
+    my $image = Image::Magick->new;
+    my $current_figure = $image->Read($src);
+    my $width_param = $image->Get('width') * $scaling;;
+    my $height_param = $image->Get('height') * $scaling;;
+    undef $image;
+    #do we have any specified LaTeX size of the picture?
+    my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
+    my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
+    #do we have any specified web size of the picture?
+    my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
+					   undef,1);
+    if ($TeXwidth ne '') {  
+	if ($TeXwidth=~/(\d+)\s*\%/) {
+	    $width_param = $1*$ENV{'form.textwidth'}/100;
+	} else { 
+	    $width_param = $TeXwidth;
+	}
+    } elsif ($TeXheight ne '') {
+	$width_param = $TeXheight/$height_param*$width_param;
+    } elsif ($width ne '') {
+	$width_param = $width*$scaling;      
+    }
+    if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}
+    return $width_param;
+}
+
+sub eps_generation {
+    my ($src,$file,$width_param) = @_;	     
+    my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
+    my $temp_file = Apache::File->new('>>'.$filename); 
+    print $temp_file "$src\n";
+    my $newsrc = $src;
+    $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
+    $newsrc=~s/\/home\/httpd\/html\/res//;
+    $newsrc=~s/\/home\/([^\/]*)\/public_html\//\/$1\//;
+    $newsrc=~s/\/\.\//\//;
+    $newsrc=~s/\/([^\/]+)\.(ps|eps)/\//;
+    return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
+}
+
+sub file_path {     
+    my $src=shift;
+    my ($file,$path); 
+    if ($src =~ m!(.*)/([^/]*)$!) {
+	$file = $2; 
+	$path = $1.'/'; 
+    } 
+    return $file,$path;
+}
+
 sub recalc {
     my $argument = shift;
     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
@@ -3257,5 +3406,26 @@ sub recalc {
     return $value.' mm';
 }
 
+sub LATEX_length {
+    my $garbage=shift;
+    $garbage=~s/^\s+(\S.*)/$1/; 
+    $garbage=~s/(.*\S)\s+$/$1/;
+    $garbage=~s/(\s)+/$1/;
+    $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;
+    $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$|\$)//g;
+    $garbage=~s/(\\alpha|\\beta|\\gamma|\\delta|\\epsilon|\\verepsilon|\\zeta|\\eta|\\theta|\\vartheta|\\iota|\\kappa|\\lambda|\\mu|\\nu|\\xi|\\pi|\\varpi|\\rho|\\varrho|\\sigma|\\varsigma|\\tau|\\upsilon|\\phi|\\varphi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega)/1/g;
+    $garbage=~s/(\\pm|\\mp|\\times|\\div|\\cdot|\\ast|\\star|\\dagger|\\ddagger|\\amalg|\\cap|\\cup|\\uplus|\\sqcap|\\sqcup|\\vee|\\wedge|\\oplus|\\ominus|\\otimes|\\circ|\\bullet|\\diamond|\\lhd|\\rhd|\\unlhd|\\unrhd|\\oslash|\\odot|\\bigcirc|\\Box|\\Diamond|\\bigtriangleup|\\bigtriangledown|\\triangleleft|\\triangleright|\\setminus|\\wr)/1/g;
+    $garbage=~s/(\\le|\\ll|\\leq|\\ge|\\geq|\\gg|\\neq|\\doreq|\\sim|\\simeq|\\subset|\\subseteq|\\sqsubset|\\sqsubseteq|\\in|\\vdash|\\models|\\supset|\\supseteq|\\sqsupset|\\sqsupseteq|\\ni|\\dash|\\perp|\\approx|\\cong|\\equiv|\\propto|\\prec|\\preceq|\\parallel|\\asymp|\\smile|\\frown|\\bowtie|\\succ|\\succeq|\\mid)/1/g;
+    $garbage=~s/(\\not<|\\\\not\\le|\\not\\prec|\\not\\preceq|\\not\\subset|\\not\\subseteq|\\not\\sqsubseteq|\\not\\in|\\not>|\\not\\ge|\\not\\succ|\\notsucceq|\\not\\supset|\\notsupseteq|\\not\\sqsupseteq|\\notin|\\not=|\\not\\equiv|\\not\\sim|\\not\\simeq|\\not\\approx|\\not\\cong|\\not\\asymp)/1/g;
+    $garbage=~s/(\\leftarrow|\\gets|\\Leftarrow|\\rightarrow|\\to|\\Rightarrow|\\leftrightarrow|\\Leftrightarrow|\\mapsto|\\hookleftarrow|\\leftharpoonup|\\leftkarpoondown|\\rightleftharpoons|\\longleftarrow|\\Longleftarrow|\\longrightarrow|\\Longrightarrow|\\longleftrightarrow|\\Longleftrightarrow|\\longmapsto|\\hookrightarrow|\\rightharpoonup|\\rightharpoondown|\\uparrow|\\Uparrow|\\downarrow|\\Downarrow|\\updownarrow|\\Updownarrow|\\nearrow|\\searrow|\\swarrow|\\nwarrow)/1/g;
+    $garbage=~s/(\\aleph|\\hbar|\\imath|\\jmath|\\ell|\\wp|\\Re|\\Im|\\mho|\\prime|\\emptyset|\\nabla|\\surd|\\partial|\\top|\\bot|\\vdash|\\dashv|\\forall|\\exists|\\neg|\\flat|\\natural|\\sharp|\\\||\\angle|\\backslash|\\Box|\\Diamond|\\triangle|\\clubsuit|\\diamondsuit|\\heartsuit|\\spadesuit|\\Join|\\infty)/ /g;
+    $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g; 
+    my  $value=length($garbage);
+    return $value;
+}
+
+
+
+
 1;
 __END__