--- loncom/xml/lonlatextable.pm 2010/08/18 12:04:31 1.1 +++ loncom/xml/lonlatextable.pm 2011/04/13 10:44:26 1.4 @@ -1,6 +1,6 @@ # Generating TeX tables. # -# $Id: lonlatextable.pm,v 1.1 2010/08/18 12:04:31 foxr Exp $ +# $Id: lonlatextable.pm,v 1.4 2011/04/13 10:44:26 foxr Exp $ # # # Copyright Michigan State University Board of Trustees @@ -140,12 +140,14 @@ array element is a table row. However: These are treated as LaTeX/TeX commands and, when the table is generated, passed without interpretation. -=item Empty cells +=item Lines with one cell that is empty. These produce a horizontal rule that spans the width of the table. =back +Calling this multiple times will continue to append cell data. + =head3 Example $table->set_data([ ['Gnu', '92.52'], [], ['Emu', '33.33'] ]); @@ -157,11 +159,15 @@ Produces | Emu | 33.33 | +--------------+--------------+ + =cut sub set_data { my ($self, $data) = @_; - $self->{'data'} = $data; + my $current_data = $self->{'data'}; + push (@$current_data, @$data); + $self->{'data'} = $current_data; + } @@ -222,7 +228,7 @@ sub generate_string { sub table_header() { my ($self) = @_; - my $result = 'begin{tabular}['; + my $result = '\begin{tabular}{'; my $coldef = $self->{'coldef'}; if ($coldef eq '') { @@ -252,7 +258,37 @@ sub table_header() # sub table_body() { - return ''; + my ($self) = @_; + my $result = ''; + foreach my $row (@{$self->{'data'}}) { + # + # If a row has only a single cell we need to deal with the two special cases + # Pass LaTeX uninterpreted or \hline. + # + if ((scalar @{$row}) == 1) { + my $cell = $row->[0]; + if ($cell eq '') { + $result .= '\hline' . "\n"; + } elsif (substr($cell, 0, 1) eq "\\") { + $result .= $cell . "\n"; + } else { + # could just be a table with one col... + + $result .= $cell . ' \\\\ ' ."\n"; + } + } else { + my $line = ''; + foreach my $cell (@{$row}) { + $line .= $cell . ' & '; + } + # Replace the last ' & ' with \\ and a line terminator.. + # and append the line to the result. + + $line =~ s/ & $/ \\\\\n/; + $result .= $line; + } + } + return $result; } #