--- loncom/xml/londefdef.pm	2005/12/20 23:12:03	1.304
+++ loncom/xml/londefdef.pm	2006/01/09 22:53:25	1.311
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.304 2005/12/20 23:12:03 foxr Exp $
+# $Id: londefdef.pm,v 1.311 2006/01/09 22:53:25 albertel Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -68,7 +68,6 @@ BEGIN {
 #    for ($row =0; $row <= $lastrow; $row++ ) {
 #	my $text = Dumper($Apache::londefdef::table[$row]);
 #	&Apache::lonnet::logthis("table [ $row ]".$text);
-#
 #    }
 #}
 sub initialize_londefdef {
@@ -174,11 +173,12 @@ sub start_html {
     if ($target eq 'web' || $target eq 'edit' || $target eq 'webgrade' ) {
 	$currentstring = &Apache::lonxml::xmlbegin();
     } elsif ($target eq 'tex') {
-	$currentstring .= '\documentclass[letterpaper]{article}';
+	$currentstring .= '\documentclass[letterpaper,twoside]{article}';
 	if (($env{'form.latex_type'}=~'batchmode') ||
             (!$env{'request.role.adv'})) {$currentstring .='\batchmode';} 
 	$currentstring .= '\newcommand{\keephidden}[1]{}'.
                           '\renewcommand{\deg}{$^{\circ}$}'.
+			  '\usepackage{multirow}'.
                           '\usepackage{longtable}'.
                           '\usepackage{textcomp}'.
                           '\usepackage{makeidx}'.
@@ -594,25 +594,34 @@ sub end_body {
     return $currentstring;
 }
 
+# \begin{center} causes a new paragprah spacing that looks odd inside 
+# of a table cell
+sub center_correction { return '\vspace*{-6 mm}'; }
 #-- <center> tag (end tag required)
 sub start_center {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack) = @_;
     my $currentstring = &end_p();	# Close off any prior para.
     if ($target eq 'web') {
 	$currentstring .= $token->[4];     
     } elsif ($target eq 'tex') {
+	if (&is_inside_of($tagstack, "table")) {
+	    $currentstring .= &center_correction();
+	}
 	$currentstring .= '\begin{center}';  
     }
     return $currentstring;
 }
 
 sub end_center {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring = $token->[2];     
     } elsif ($target eq 'tex') {
 	$currentstring = '\end{center}';  
+	if (&is_inside_of($tagstack, "table")) {
+	    $currentstring .= &center_correction();
+	}
     }
     return $currentstring;
 }
@@ -1183,6 +1192,10 @@ sub start_p {
 	if ($align eq 'center') {
 	    $currentstring .='\begin{center}\par';
 	    $closing_string = '\end{center}';
+	    if (&is_inside_of($tagstack, "table")) {
+		$currentstring = &center_correction().$currentstring;
+		$closing_string .= &center_correction();
+	    }
 	} elsif ($align eq 'right') {
 	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
 	    $closing_string= '}}';
@@ -1524,6 +1537,10 @@ sub start_div {
 	if ($align eq 'center') {
 	    $currentstring .= '\begin{center}';
 	    $endstring      = '\end{center}';
+	    if (&is_inside_of($tagstack, "table")) {
+		$currentstring = &center_correction().$currentstring;
+		$endstring .= &center_correction();
+	    }
 	}
 	elsif ($align eq 'right') {
 	    $currentstring .= '\begin{flushright}';
@@ -1944,11 +1961,16 @@ sub start_table {
 
 	# width either comes forced from the TeXwidth or the width parameters.
 	# in either case it can be a percentage or absolute width.
-
+	# in the width case we ignore absolute width 
 	my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
-	if (not defined $TeXwidth) {
-	    $TeXwidth = &Apache::lonxml::get_param('width',$parstack,$safeeval,undef,1);
-
+	if (!defined($TeXwidth)) {
+	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,
+						       $safeeval,undef,1);
+	    if ($htmlwidth =~ /%/) {
+		$TeXwidth = $htmlwidth;
+	    } else { 
+		$TeXwidth = $textwidth;
+	    }
 	} else {
 	    $Apache::londefdef::table[-1]{'forcedtablewidth'} = 1;
 	}
@@ -2002,6 +2024,7 @@ sub end_table {
 	my $inmemory = '';
 	my $output = '';
 	my $WARNING='';
+	# &debug_dump_table($Apache::londefdef::table[-1]);
         #width of columns from TeXwidth attributes
 
 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
@@ -2186,6 +2209,7 @@ sub end_table {
 	$header_of_table .= '}';
 	#fill the table
 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
+	    my $have_rowspan = 0;
 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
 		#
 		#  Do the appropriate magic if this has a colspan
@@ -2196,19 +2220,34 @@ sub end_table {
 			$colspan
 			.'}{|l|}{';
 		}
+		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
+
+		# Start a rowspan if necessary:
+
+		if ($rowspan > 1) {
+		    $have_rowspan++;
+		    $output .= '\multirow{'.$rowspan.'}[0]{'.$fwidth[$jn].'mm}{';
+		}
+		if (($rowspan eq '^') || ($rowspan eq '_')) {
+		    $have_rowspan++;
+		}
+
 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
-		    # $output.='\vspace*{-6 mm}\begin{center}';
-		    $output.='\begin{center}';
+		    $output.=&center_correction().'\begin{center}';
 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 		    $output.=' \hfill \llap{'
 		}
 		$output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
-		    # $output.='\end{center}\vspace*{-6 mm}';
-		    $output.='\end{center}';
+		    $output.='\end{center}'.&center_correction();
 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 		    $output.='} ';
 		}
+		# Close off any open multirow:
+		
+		if ($rowspan > 1) {
+		    $output .= '}';
+		}
 		#  Close off the colspan...
 		#
 		if ($colspan > 1)  {
@@ -2217,7 +2256,24 @@ sub end_table {
 		}
                 if ($jn!=$#fwidth) {$output.=' '.$Apache::londefdef::table[-1]{'vinc'};}
 	    }
-	    $output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
+	    #  If have_rowspan > 0, and borders are on, then 
+	    #  we need to do more than put an \hline at the bottom of row.
+	    #  we need to do the appropriate \cline to ensure that
+	    #  the spanned rows don't have \hlines through them.
+
+	    if (($Apache::londefdef::table[-1]{'hinc'} =~ /\\hline/) && $have_rowspan) {
+		$output .= ' \\\\ ';
+		for (my $jn=0; $jn<=$#fwidth;$jn++) {
+		    my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
+		    if (($rowspan <= 1) || ($rowspan eq '_')) {
+			my $column = $jn+1;
+			$output .= '\cline{'.$column.'-'.$column.'} ';
+		    }
+		}
+
+	    } else {
+		$output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
+	    }
 	}
 	# Note that \newline destroys alignment env's produced  by e.g. <div>
 	# $Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut\newline\strut ';
@@ -2365,8 +2421,37 @@ sub end_td_tex {
     my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
     my $data = &Apache::lonxml::endredirection();
 
+    #  The rowspan array of the table indicates which cells are part of a span.
+    #  n indicates the start of a span set of n rows.
+    #  ^ indicates a cell that continues a span set.
+    #  _ indicates the cell is at the bottom of a span set.
+    #  If this and subsequent cells are part of a rowspan, we must
+    #  push along the row until we find one that is not.
+
+    while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
+	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
+	# Part of a span.
+	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
+	$current_column++;
+    }
+    $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
+   
 
-    
+
+    my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
+    if (!$rowspan) {
+	$rowspan = 1;
+    }
+
+
+
+    $Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] = $rowspan;
+    for (my $i = 1; $i < $rowspan; $i++) {
+	$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column] = '^';
+	if ($i == ($rowspan-1)) {
+	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column] = '_';
+	}
+    }
 
     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
     if (defined $TeXwidth) {		
@@ -2456,16 +2541,16 @@ sub end_td_tex {
 
     }
     # Should be be killing off the 'include' elements as they're used up?
+
     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
+
     # Get the column and row spans.
     # Colspan can be done via \multicolumn if I can figure out the data structs.
-    # Rowspan, can be done using the multirow package which adds similar stuff to rowspanning.
 
     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
     if (!$colspan) {
 	$colspan = 1;
     }
-    my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
 
 
     #  the colspan array will indicate how many columns will be spanned by this
@@ -2680,7 +2765,8 @@ sub start_img {
 						  $safeeval,
 						  undef,1));
 	if(!$align) {
-	    if (&is_inside_of($tagstack, "table")) {
+	    # disabled for now see BUG#4535
+	    if (0 && &is_inside_of($tagstack, "table")) {
 		$align = "right";      # Force wraptext use. 
 	    } else {
 		$align = "bottom";	# This is html's default so it's ours too.