--- loncom/xml/lontable.pm 2008/12/23 11:49:32 1.6 +++ loncom/xml/lontable.pm 2008/12/29 11:57:37 1.7 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Generating TeX tables. # -# $Id: lontable.pm,v 1.6 2008/12/23 11:49:32 foxr Exp $ +# $Id: lontable.pm,v 1.7 2008/12/29 11:57:37 foxr Exp $ # # # Copyright Michigan State University Board of Trustees @@ -108,6 +108,7 @@ modified by this. These configuration i =over3 + =item alignment Table alignment. Some table styles support this but not all. @@ -132,6 +133,12 @@ to the LaTeX::Table package, and they wi not error checked. Any use of a non-existent theme is reported by the LaTeX::Table package when the table text is generated. +=item width + +The width of the table. This can be expressed as fractions of full width, or in any +TeX unit measure e.g. 0.75 for 75% of the width, or 10.8cm This forces the table to the +tabularx environment. + =back =head3 Member data @@ -399,6 +406,26 @@ sub theme { =pod +=head 2 width + +Gets and optionally sets the width of the table. + +=head 3 Examples: + + $table->width("0.8"); # 80% of the column width. + my $newwidth = $table->width("10cm"); # 10cm width returns "10cm". + +=cut +sub width { + my ($self, $new_value) = @_; + if (defined($new_value)) { + $self->{'width'} = $new_value; + } + return $self->{'width'}; # Could be undef. +} + +=pod + =head2 start_row Begins a new row in the table. If a row is already open, that row is @@ -657,6 +684,20 @@ sub generate { my $table = LaTeX::Table->new(); + # Add the caption if supplied. + + if ($this->{'caption'} ne "") { + $table->set_caption($this->caption); + } + + + # Set the width if defined: + + if (defined ($this->{'width'})) { + $table->set_width($this->{'width'}); + $table->set_width_environment('tabularx'); + } + # Build up the data: my @data; @@ -669,17 +710,46 @@ sub generate { for (my $row = 0; $row < $row_count; $row++) { my @row; my $cells = $rows->[$row]->{'cells'}; + my $def_halign = $rows->[$row]->{'default_halign'}; my $cell_count = scalar(@$cells); my $startcol = 1; my @underlines; # Array of \cline cells if cellborder on. for (my $cell = 0; $cell < $cell_count; $cell++) { my $contents = $cells->[$cell]->{'contents'}; + + # + # Cell alignment is the default alignment unless + # explicitly specified in the cell. + # NOTE: at this point I don't know how to do vert alignment. + # + + my $halign = $def_halign; + if (defined ($cells->[$cell]->{'halign'})) { + $halign = $cells->[$cell]->{'halign'}; + } + + # Create the horizontal alignment character: + + my $col_align = 'l'; + if ($halign eq 'right') { + $col_align = 'r'; + } + if ($halign eq 'center') { + $col_align = 'c'; + } + if ($inner_border || ($outer_border && ($cell == 0))) { + $col_align = '|'.$col_align; + } + if ($inner_border || ($outer_border && ($cell == ($cell_count -1)))) { + $col_align = $col_align.'|'; + } + + #factor in spans: + my $cspan = $cells->[$cell]->{'colspan'}; my $nextcol = $startcol + $cspan; - if ($cspan > 1) { - $contents = '\multicolumn{'.$cspan.'}{|l|}{'.$contents.'}'; - } + $contents = '\multicolumn{'.$cspan.'}{'.$col_align.'}{'.$contents.'}'; if ($inner_border && ($cells->[$cell]->{'rowspan'} == 1)) { my $lastcol = $nextcol -1; push(@underlines, "\\cline{$startcol-$lastcol}");