--- loncom/xml/londefdef.pm	2003/06/19 18:45:57	1.141
+++ loncom/xml/londefdef.pm	2003/09/30 19:39:00	1.175
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.141 2003/06/19 18:45:57 sakharuk Exp $
+# $Id: londefdef.pm,v 1.175 2003/09/30 19:39:00 sakharuk Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -25,8 +25,7 @@
 # /home/httpd/html/adm/gpl.txt
 #
 # http://www.lon-capa.org/
-#
-# Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
+## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
 # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
 # binary executable programs or libraries distributed by the 
 # Michigan State University (the "Licensee"), but any binaries so 
@@ -52,6 +51,8 @@ use Image::Magick;
 use Apache::lonmenu();
 use Apache::lonmeta();
 
+$Apache::londefdef::TD_redirection=0;
+
 BEGIN {
 
     &Apache::lonxml::register('Apache::londefdef',('a','abbr','acronym','accessrule','address','allow','applet','area','b','base','basefont','bgo','bgsound','big','blink','blockquote','blankspace','body','br','button','caption','center','cite','code','col','colgroup','dd','del','dfn','dir','div','dl','dt','em','embed','externallink','fieldset','font','form','frame','frameset','h1','h2','h3','h4','h5','h6','head','hr','html','i','iframe','img','input','ins','insert','isindex','kbd','keygen','label','layer','legend','li','link','m','map','marquee','menu','meta','multicol','nobr','noembed','noframes','nolayer','noscript','object','ol','optgroup','option','output','p','param','pre','q','s','samp','select','server','small','spacer','span','strike','strong','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','tthoption','u','ul','var','wbr','hideweboutput'));
@@ -157,14 +158,17 @@ sub start_html {
 	    &Apache::lonxml::fontsettings();     
     } elsif ($target eq 'tex') {
 	@Apache::londefdef::table = ();
-	$currentstring .= '\documentclass[letterpaper]{article}
-                           \batchmode
-                           \newcommand{\keephidden}[1]{}
+	$currentstring .= '\documentclass[letterpaper]{article}';
+	if ($ENV{'form.latex_type'}=~'batchmode') {$currentstring .='\batchmode';} 
+	$currentstring .= '\newcommand{\keephidden}[1]{}
                            \renewcommand{\deg}{$^{\circ}$}
+                           \usepackage{longtable}
                            \usepackage{textcomp}
+                           \usepackage{makeidx}
                            \usepackage[dvips]{graphicx}
                            \usepackage{epsfig}\usepackage{calc}
-\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}';
+\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}
+\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large\textbf{Index}} \newline \setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}';
     }
     return $currentstring;
 }
@@ -319,7 +323,7 @@ sub start_title {
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	$currentstring .= '\keephidden{' 
+	$currentstring .= '\keephidden{Title of the document:  ' 
     }
     if ($target eq 'meta') {
 	$currentstring='<title>';
@@ -363,20 +367,38 @@ sub start_meta {
 	    $name=~s/\s/\_/gs;
 	    $name=~s/\W//gs;
 	    if ($name) {
-		$currentstring='<'.$name.'>'.
+		$currentstring='<'.$name;
+                 my $display=&Apache::lonxml::get_param
+		('display',$parstack,$safeeval,undef,1);
+                if ($display) {
+                    $display=~s/\"/\'/g;
+		    $currentstring.=' display="'.$display.'"';
+                }
+		$currentstring.='>'.
 		    &Apache::lonxml::get_param
 			('content',$parstack,$safeeval,undef,1).
 			'</'.$name.'>';
 	    }
+            my $display=&Apache::lonxml::get_param
+		('display',$parstack,$safeeval,undef,1);
+            if ($display) {
+		$display=&HTML::Entities::encode($display);
+		$currentstring.='<'.$name.'.display>'.$display.
+                               '</'.$name.'.display>';
+            }
 	}
     } elsif ($target eq 'tex') {
-	&Apache::lonxml::startredirection();
+	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();
+	}
     }
     return $currentstring;
 }
 
 sub end_meta {
-    my ($target,$token,$tagstack,$parstack,$parser) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	my $args='';
@@ -385,8 +407,11 @@ sub end_meta {
 	    $currentstring = $token->[4];
 	}
     } elsif ($target eq 'tex') {
-	$currentstring=&Apache::lonxml::endredirection();
-	$currentstring='';
+	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::endredirection();
+	}
     }
     return $currentstring;
 }
@@ -441,10 +466,37 @@ sub start_body {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
+	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) {
 	    $currentstring.='<head>'.
 		&Apache::lonmenu::registerurl(undef,$target).'</head>';
 	}
+# Accessibility
+	if ($ENV{'browser.imagesuppress'} eq 'on') {
+	    delete($token->[2]->{'background'});
+	}
+	if ($ENV{'browser.fontenhance'} eq 'on') {
+	    my $style='';
+	    foreach my $key (keys(%{$token->[2]})) {
+		if ($key =~ /^style$/i) {
+		    $style.=$token->[2]->{$key}.';';
+		    delete($token->[2]->{$key});
+		}
+	    }
+	    $token->[2]->{'style'}=$style.'; font-size: x-large;';
+	}
+	if ($ENV{'browser.blackwhite'} eq 'on') {
+	    delete($token->[2]->{'font'});
+	    delete($token->[2]->{'link'});
+	    delete($token->[2]->{'alink'});
+	    delete($token->[2]->{'vlink'});
+	    delete($token->[2]->{'bgcolor'});
+	    delete($token->[2]->{'background'});
+	}
+# Overload loads
 	my $onLoad='';
 	foreach my $key (keys(%{$token->[2]})) {
 	    if ($key =~ /^onload$/i) {
@@ -489,7 +541,7 @@ 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;
 }
@@ -502,9 +554,7 @@ sub start_center {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
 	$currentstring = '\begin{center}';  
-    }  elsif ($target eq 'latexsource') {
-	$currentstring = '\begin{center}';  
-    } 
+    }
     return $currentstring;
 }
 
@@ -515,9 +565,7 @@ sub end_center {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
 	$currentstring = '\end{center}';  
-    }  elsif ($target eq 'latexsource') {
-	$currentstring = '\end{center}';  
-    } 
+    }
     return $currentstring;
 }
 
@@ -529,8 +577,6 @@ sub start_b {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
 	$currentstring = '\textbf{';  
-    }  elsif ($target eq 'latexsource') {
-	$currentstring = '\textbf{';  
     } 
     return $currentstring;
 }
@@ -542,8 +588,6 @@ sub end_b {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
 	$currentstring = '}';  
-    } elsif ($target eq 'latexsource') {
-	$currentstring = '}';  
     } 
     return $currentstring;
 }
@@ -556,8 +600,6 @@ sub start_strong {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
 	$currentstring = '\textbf{';  
-    } elsif ($target eq 'latexsource') {
-	$currentstring = '\textbf{';  
     } 
     return $currentstring;
 }
@@ -570,9 +612,7 @@ sub end_strong {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
 	$currentstring = '}';  
-    }  elsif ($target eq 'latexsource') {
-	$currentstring = '}';  
-    } 
+    }
     return $currentstring;
 }
 
@@ -848,9 +888,7 @@ sub start_cite {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= "\\textit{";
-    }  elsif ($target eq 'latexsource') {
-	$currentstring .= "\\textit{";
-    } 
+    }
     return $currentstring;
 }
 
@@ -861,9 +899,7 @@ sub end_cite {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= "}";
-    }  elsif ($target eq 'latexsource') {
-	$currentstring .= "}";
-    } 
+    }
     return $currentstring;
 }
 
@@ -875,9 +911,7 @@ sub start_i {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\textit{';
-    }  elsif ($target eq 'latexsource') {
-	$currentstring .= '\textit{';
-    } 
+    }
     return $currentstring;
 }
 
@@ -888,8 +922,6 @@ sub end_i {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '}';
     } 
     return $currentstring;
 }
@@ -902,9 +934,7 @@ sub start_address {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= "\\textit{";
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= "\\textit{";
-    } 
+    }
     return $currentstring;
 }
 
@@ -915,8 +945,6 @@ sub end_address {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= "}";
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= "}";
     }
     return $currentstring;
 }
@@ -929,8 +957,6 @@ sub start_dfn {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= "\\textit{";
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= "\\textit{";
     } 
     return $currentstring;
 }
@@ -942,9 +968,7 @@ sub end_dfn {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= "}";
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= "}";
-    } 
+    }
     return $currentstring;
 }
 
@@ -956,9 +980,7 @@ sub start_tt {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\texttt{';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '\texttt{';
-    } 
+    }
     return $currentstring;
 }
 
@@ -969,8 +991,6 @@ sub end_tt {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '}';
     }
     return $currentstring;
 }
@@ -983,9 +1003,7 @@ sub start_kbd {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= "\\texttt";
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= "\\texttt{";
-    } 
+    }
     return $currentstring;
 }
 
@@ -996,9 +1014,7 @@ sub end_kbd {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= "}";
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= "}";
-    } 
+    }
     return $currentstring;
 }
 
@@ -1033,9 +1049,7 @@ sub start_em {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\emph{';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '\emph{';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1046,9 +1060,7 @@ sub end_em {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '}';
-    }  
+    } 
     return $currentstring;
 }
 
@@ -1060,8 +1072,6 @@ sub start_q {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= "\\emph{";
-    }  elsif ($target eq 'latexsource') {
-	$currentstring .= "\\emph{";
     }
     return $currentstring;
 }
@@ -1073,23 +1083,21 @@ sub end_q {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= "}";
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= "}";
-    }  
+    } 
     return $currentstring;
 }
 
 #-- <p> tag
 sub start_p {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= '{\par ';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '{\par ';
-    } 
+	my $signal=1;
+	foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}}
+	if ($signal) {$currentstring .= '\par ';}
+    }
     return $currentstring;
 }
 
@@ -1098,10 +1106,6 @@ sub end_p {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
-    } elsif ($target eq 'tex') {
-	$currentstring .= '}';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '}';
     }
     return $currentstring;
 }
@@ -1114,11 +1118,9 @@ sub start_br {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	if ($$tagstack[-2] ne 'sub' && $$tagstack[-2] ne 'sup') {
-	    $currentstring .= '\vskip 0.2 mm';
+	    $currentstring .= '\vskip 0.2 mm ';
 	}
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '\\';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1139,9 +1141,7 @@ sub start_big {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '{\large ';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '{\Large ';
-    }  
+    } 
     return $currentstring;
 }
 
@@ -1152,8 +1152,6 @@ sub end_big {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '}';
     }
     return $currentstring;
 }
@@ -1166,9 +1164,7 @@ sub start_small {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '{\footnotesize ';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '{\footnotesize ';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1179,8 +1175,6 @@ sub end_small {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } elsif ($target eq 'latexsource') {
-	$currentstring .= '}';
     }
     return $currentstring;
 }
@@ -1220,7 +1214,12 @@ sub start_font {
     my $currentstring = '';
     if ($target eq 'web') {
 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
-	if ($face=~/symbol/i) {$Apache::lonxml::prevent_entity_encode++;}
+	if ($face=~/symbol/i) {
+	    $Apache::lonxml::prevent_entity_encode++;
+	} else {
+	    if (($ENV{'browser.fontenhance'} eq 'on') || 
+		($ENV{'browser.blackwhite'} eq 'on')) { return ''; }
+	}
 	$currentstring = $token->[4];     
     }  elsif ($target eq 'tex') {
 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
@@ -1352,14 +1351,14 @@ sub start_hr {
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,0);
+	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 	if (defined $LaTeXwidth) {
 	    if ($LaTeXwidth=~/^%/) {
 		substr($LaTeXwidth,0,1)='';
 		$LaTeXwidth=($LaTeXwidth/100).'\textwidth';
 	    }
 	} else {
-	    $LaTeXwidth ='0.95\textwidth';
+	    $LaTeXwidth ='0.9\textwidth';
 	}
 	my ($pre,$post);
 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
@@ -1370,7 +1369,7 @@ sub start_hr {
 	} elsif ($align eq 'right') {
 	    $pre=' \hfill \llap{'; $post='}';
 	}
-	$currentstring .= ' \vskip 0 mm \noindent\makebox[\textwidth]{'.$pre.'\makebox['.
+	$currentstring .= ' \vskip 0 mm \noindent\makebox['.$LaTeXwidth.']{'.$pre.'\makebox['.
                                     $LaTeXwidth.'][b]{\hrulefill}'.$post.'}\vskip 0 mm ';
     } 
     return $currentstring;
@@ -1381,8 +1380,7 @@ sub end_hr {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
-    } elsif ($target eq 'tex') {
-    } 
+    }
     return $currentstring;
 }
 
@@ -1407,50 +1405,50 @@ sub end_div {
 
 #-- <a> tag
 sub start_a {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
+	my $a=&Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
+	my $b=&Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
+	if ($a=~/\S/) {
+	    $a=~s/([^\\])%/$1\\\%/g;
+	    $currentstring .= '\ref{URI: '.$a.'}';
+	} elsif ($b=~/\S/) {
+	    $currentstring .= '\ref{Anchor: '.$b.'}';
+	} else {
+	    $currentstring.='';
+	}
     }
     return $currentstring;
 }
 
 sub end_a {
-    my ($target,$token,$tagstack,$stackref) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
-    } elsif ($target eq 'tex') {
-	my  $tempor_var = $stackref->[$#$stackref];
-	if (index($tempor_var,'name') != -1 ) {
-	    $tempor_var =~ s/name=([^,]*),/$1/g;
-	} elsif (index($tempor_var,'href') != -1 ) {
-	    $tempor_var =~ s/href=([^,]*),/$1/g;
-	    $currentstring .= " \\ref{$tempor_var}";
-	}
     }
     return $currentstring;
 }
 
 #-- <li> tag
 sub start_li {
-    my ($target,$token,$tagstack,$stackref) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	my  $tempor_var = $stackref->[$#$stackref];
-	if (index($tempor_var,'circle') != -1 ) {
-	    $currentstring .= " \\item[o] ";
-	} elsif (index($tempor_var,'square') != -1 ) {
-	    $currentstring .= " \\item[$\Box$] ";
-	} elsif ($tempor_var ne '') { 
-	    $_ = $tempor_var;
-	    m/my\s*([^=]*)=/;
-	    $currentstring .= " \\item[$1] ";
+	my  $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,1);
+	if ($type=~/circle/) {
+	    $currentstring .= ' \item[o] ';
+	} elsif ($type=~/square/) {
+	    $currentstring .= ' \item[$\Box$] ';
+	} elsif ($type ne '') { 
+	    $currentstring .= ' \item['.$type.'] ';
 	} else {
-	    $currentstring .= " \\item ";
+	    $currentstring .= ' \item ';
 	}  
     } 
     return $currentstring;
@@ -1640,61 +1638,104 @@ sub start_dl {
     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;
 }
 
+sub item_cleanup {
+    my $item=&Apache::lonxml::endredirection();
+    $item=~s/\\begin{center}//g;
+    $item=~s/\\end{center}//g;
+    return $item;
+}
+
 #-- <dd> tag
 sub start_dd {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
+    } elsif ($target eq 'tex') {
+	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;
 }
 
@@ -1747,7 +1788,7 @@ sub start_table {
 	    $Apache::londefdef::table[-1]{'vvinc'} = '';
 	}
 	$Apache::londefdef::table[-1]{'output'} = ' \noindent \begin{tabular} ';
-	$currentstring = '\keephidden{NEW TABLE ENTRY '.$textwidth.' }';
+	$currentstring = '\keephidden{NEW TABLE ENTRY}';
     }
     return $currentstring;
 }
@@ -1795,7 +1836,7 @@ sub end_table {
 	$needed=$#length_row_final-$needed+1;
 	for (my $jn=0;$jn<=$#length_row_final;$jn++) {
 	    if ($length_row_final[$jn]==0) {
-		if ($length_raw_row[$jn]<$available_length/3) {
+		if ($length_raw_row[$jn]<$available_length) {
 		    $length_row_final[$jn]=$length_raw_row[$jn];
 		    $available_length=$available_length-$length_raw_row[$jn];
 		    $needed--;
@@ -1825,13 +1866,13 @@ sub end_table {
 	} else {
 	    $currentstring .= $Apache::londefdef::table[-1]{'output'};
 	    pop @Apache::londefdef::table;
-            undef @Apache::londefdef::table;
+	    undef @Apache::londefdef::table;
 	}
     }
     return $currentstring;
 }
 
-#-- <tr> tag
+#-- <tr> tag (end tag optional)
 sub start_tr {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -1854,74 +1895,121 @@ sub start_tr {
 }
         
 sub end_tr {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
+	if ($Apache::londefdef::TD_redirection) {
+	    &end_td_tex($parstack,$parser,$safeeval);    
+	}
 	push @{ $Apache::londefdef::table[-1]{'TeXlengthrow'} },$Apache::londefdef::table[-1]{'TeXlength'};
 	push @{ $Apache::londefdef::table[-1]{'lengthrow'} },$Apache::londefdef::table[-1]{'length'};
     }
     return $currentstring;
 }
 
-#-- <td> tag
+#-- <td> tag (end tag optional)
 sub start_td {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	my $what_to_push = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
-	if ($what_to_push eq '') {
-	    $what_to_push = substr($Apache::londefdef::table[-1]{'rows'}[0],0,1);;
-	}
-	push @{ $Apache::londefdef::table[-1]{'columns'} }, $what_to_push;
-	$Apache::londefdef::table[-1]{'counter_columns'}++;
-	&Apache::lonxml::startredirection();
+	$Apache::londefdef::TD_redirection = 1;
+	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
     } 
     return $currentstring;
 }   
-     
+    
+sub tag_check {
+    my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
+    my @ar=@$parstack; 
+    for (my $i=$#ar-1;$i>=0;$i--) {
+	if (lc($$tagstack[$i]) eq $good_tag) {
+	    &start_td_tex($parstack,$parser,$safeeval);
+	    last;
+	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
+	    splice @ar, $i+1;
+	    &end_td_tex(\@ar,$parser,$safeeval);
+	    &start_td_tex($parstack,$parser,$safeeval);
+	    last;
+	}
+    }
+    return '';
+}
+ 
+sub start_td_tex {
+    my ($parstack,$parser,$safeeval) = @_;
+    my $what_to_push = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
+    if ($what_to_push eq '') {
+	$what_to_push = substr($Apache::londefdef::table[-1]{'rows'}[0],0,1);;
+    }
+    push @{ $Apache::londefdef::table[-1]{'columns'} }, $what_to_push;
+    $Apache::londefdef::table[-1]{'counter_columns'}++;
+    &Apache::lonxml::startredirection();
+    return '';
+}
+
+sub end_td_tex {
+    my ($parstack,$parser,$safeeval) = @_;
+    my $current_row = $Apache::londefdef::table[-1]{'row_number'};
+    my $data=&Apache::lonxml::endredirection();
+    my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
+    if (defined $TeXwidth) {		
+	my $current_length=&recalc($TeXwidth);
+	$current_length=~/(\d+)/;
+	$Apache::londefdef::table[-1]{'TeXlength'} .= $1.',';
+	$Apache::londefdef::table[-1]{'length'} .= '0,';
+    } else {
+	if ($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
+	    my $current_length=&recalc($1);
+	    $current_length=~/(\d+\.?\d*)/;
+	    $Apache::londefdef::table[-1]{'TeXlength'} .= $1.',';
+	    $Apache::londefdef::table[-1]{'length'} .= '0,';
+	} else {  
+	    $data=~s/^\s+(\S.*)/$1/; 
+	    $data=~s/(.*\S)\s+$/$1/;
+	    $data=~s/(\s)+/$1/;
+	    my $current_length=0;
+	    if ($data=~/\\vskip/) {
+                my $newdata=$data;
+		$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);
+		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
+		}
+	    } else {
+		$current_length=1.8*length($data);
+	    }
+	    $Apache::londefdef::table[-1]{'length'} .= $current_length.',';
+	    $Apache::londefdef::table[-1]{'TeXlength'} .= '0,';
+	}        
+    }
+	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
+	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
+	}
+    @{ $Apache::londefdef::table[-1]{'rowdata'} }[$current_row] .= '\parbox{TOBECHANGEDONNUMBER}{'.$data.'} '.$Apache::londefdef::table[-1]{'vinc'};
+    return'';
+}
+
 sub end_td {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
-	my $current_row = $Apache::londefdef::table[-1]{'row_number'};
-	my $data=&Apache::lonxml::endredirection();
-	my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
-	if (defined $TeXwidth) {		
-	    my $current_length=&recalc($TeXwidth);
-	    $current_length=~/(\d+)/;
-	    $Apache::londefdef::table[-1]{'TeXlength'} .= $1.',';
-	    $Apache::londefdef::table[-1]{'length'} .= '0,';
-	} else {
-	    if ($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
-		my $current_length=&recalc($1);
-		$current_length=~/(\d+\.?\d*)/;
-		$Apache::londefdef::table[-1]{'TeXlength'} .= $1.',';
-		$Apache::londefdef::table[-1]{'length'} .= '0,';
-	    } else {     
-		$data=~/^\s*(\S.*)/;
-		$data=$1;
-		$data=~/(.*\S)\s*$/;
-		$data=$1;
-		my $current_length=2*length($data);
-		$Apache::londefdef::table[-1]{'length'} .= $current_length.',';
-		$Apache::londefdef::table[-1]{'TeXlength'} .= '0,';
-	    }        
-	}
-	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {                                
-	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
-	}
-	@{ $Apache::londefdef::table[-1]{'rowdata'} }[$current_row] .= '\parbox{TOBECHANGEDONNUMBER}{'.$data.'} '.$Apache::londefdef::table[-1]{'vinc'};
+        $Apache::londefdef::TD_redirection =0;
+	&end_td_tex($parstack,$parser,$safeeval);
     }
     return $currentstring;
 }
 
-#-- <th> tag
+#-- <th> tag (end tag optional)
 sub start_th {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
@@ -1987,10 +2075,9 @@ sub start_img {
     my $width_param = '';
     my $height_param = '';
     my $scaling = .3;
-    
     if ($target eq 'web') {
 	if ($ENV{'browser.imagesuppress'} ne 'on') {
-	    $currentstring = $token->[4];
+	    $currentstring.= $token->[4];
 	} else {
 	    my $alttag= &Apache::lonxml::get_param
 		('alt',$parstack,$safeeval,undef,1);
@@ -1998,12 +2085,11 @@ sub start_img {
 		$alttag=&Apache::lonmeta::alttag
 		    ($Apache::lonxml::pwd[-1],$src);
 	    }
-	    $currentstring='[IMAGE: '.$alttag.']';
+	    $currentstring.='[IMAGE: '.$alttag.']';
 	}
     } 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
@@ -2028,6 +2114,7 @@ sub start_img {
 	    } 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!(.*)/([^/]*)$!) {
@@ -2115,7 +2202,11 @@ sub start_applet {
 	    $currentstring='[APPLET: '.$alttag.']';
 	}
     } elsif ($target eq 'tex') {
-	$currentstring = " \\begin{figure} ";
+	my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
+					       undef,1);
+	if ($code=~/TrueFalse\.class/) {
+	    $currentstring.='\begin{center} $\bigcirc$ True \hskip 10 mm  $\bigcirc$ False\end{center}';
+	}
     } 
     return $currentstring;
 }
@@ -2126,7 +2217,6 @@ sub end_applet {
     if ($target eq 'web') {
 	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring = " \\end{figure}";
     } 
     return $currentstring;
 }
@@ -2149,7 +2239,6 @@ sub start_embed {
 	    $currentstring='[EMBED: '.$alttag.']';
 	}
     } elsif ($target eq 'tex') {
-	$currentstring = " \\begin{figure} ";  
     } 
     return $currentstring;
 }
@@ -2159,8 +2248,7 @@ 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;
 }
@@ -2179,7 +2267,6 @@ sub start_param {
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
-	$currentstring = " \\begin{figure} ";  
     } 
     return $currentstring;
 }
@@ -2190,7 +2277,6 @@ sub end_param {
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
-	$currentstring = " \\end{figure}";  
     } 
     return $currentstring;
 }
@@ -2267,6 +2353,29 @@ sub end_frameset {
     return $currentstring;
 }
 
+#-- <xmp>
+sub start_xmp {
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
+    my $currentstring = '';
+    if ($target eq 'web') {
+	$currentstring .= $token->[4];
+    } elsif ($target eq 'tex') {
+	$currentstring .= '\begin{verbatim}';
+    } 
+    return $currentstring;
+}
+
+sub end_xmp {
+    my ($target,$token) = @_;
+    my $currentstring = '';
+    if ($target eq 'web') {
+	$currentstring .= $token->[2];
+    } elsif ($target eq 'tex') {
+	$currentstring .= '\end{verbatim}';
+    }
+    return $currentstring;
+}
+
 #-- <pre>
 sub start_pre {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
@@ -2413,7 +2522,7 @@ sub start_base {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
-    } 
+    }
     return $currentstring;
 }
 
@@ -3053,7 +3162,7 @@ sub end_tbody {
     return $currentstring;
 }
 
-#-- <tfoot> tag
+#-- <tfoot> tag (end tag optional)
 sub start_tfoot {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3072,7 +3181,7 @@ sub end_tfoot {
     return $currentstring;
 }
 
-#-- <thead> tag
+#-- <thead> tag (end tag optional)
 sub start_thead {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3097,7 +3206,9 @@ sub start_var {
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[4];     
-    } 
+    } elsif ($target eq 'tex') {
+	$currentstring = '\textit{'; 
+    }
     return $currentstring;
 }
 
@@ -3105,12 +3216,14 @@ sub end_var {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
-	$currentstring = $token->[2];    
+	$currentstring = $token->[2];
+    } elsif ($target eq 'tex') {
+	$currentstring = '}'; 
     } 
     return $currentstring;
 }
 
-#-- <wbr> tag
+#-- <wbr> tag (end tag forbidden)
 sub start_wbr {
     my ($target,$token) = @_;
     my $currentstring = '';
@@ -3129,7 +3242,6 @@ sub end_wbr {
     return $currentstring;
 }
 
-
 #-- <hideweboutput> tag
 sub start_hideweboutput {
     my ($target,$token) = @_;