The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

LaTeX::Table - Perl extension for the automatic generation of LaTeX tables.

VERSION

This document describes LaTeX::Table version 0.6.2

SYNOPSIS

  use LaTeX::Table;
  
  my $header
        = [ [ 'Name', 'Beers:2c' ], [ '', 'before 4pm', 'after 4pm' ] ];
  
  my $data = [
        [ 'Lisa',   '0', '0' ],
        [ 'Marge',  '0', '1' ],
        [ 'Homer',  '2', '6' ],
        [],  # horizontal line
        [ 'Wiggum', '0', '5' ],
        [ 'Otto',   '1', '3' ],
        [ 'Barney', '8', '16' ],
  ];
  
  my $table = LaTeX::Table->new(
        {   
        filename    => 'counter.tex',
        caption     => 'Number of beers before and after 4pm.',
        maincaption => 'Beer Counter',
        label       => 'table_beercounter',
        tablepos    => 'htb',
        header      => $header,
        data        => $data,
        }
  );
  
  # write LaTeX code in counter.tex
  $table->generate();

  # callback functions
  $table->set_callback(sub { 
       my ($row, $col, $value, $is_header ) = @_;
       if ($col == 0) {
           $value = uc $value;
       }
       return $value;
  });     
  
  print $table->generate_string();

Now in your LaTeX document:

    \documentclass{article}

    % for multipage tables
    \usepackage{xtab}
    % for publication quality tables
    \usepackage{booktabs}

    \begin{document}
    \input{counter}
    \end{document}
  

DESCRIPTION

LaTeX::Table provides functionality for an intuitive and easy generation of LaTeX tables for reports or theses. It ships with some predefined good looking table styles. Supports multipage tables via the xtab package.

INTERFACE

my $table = LaTeX::Table->new($arg_ref)

Constructs a LaTeX::Table object. The parameter is an hash reference with options (see below).

$table->generate()

Generates the LaTeX table code. The generated LaTeX table can be included in a LaTeX document with the \input command:

  % include counter.tex, generated by LaTeX::Table 
  \input{counter}
$table->generate_string()

Same as generate() but does not create a LaTeX file but returns the LaTeX code as string.

  my $latexcode = $table->generate_string();
$table->get_available_themes()

Returns an hash reference to all available (predefined and customs) themes. See "THEMES" for details.

        for my $theme ( keys %{ $table->get_available_themes } ) {
                ...
        }

OPTIONS

Options can be defined in the constructor hash reference or with the setter set_<optionname>. Additionally, getters of the form get_<optionname> are created.

BASIC OPTIONS

filename

The name of the LaTeX output file. Default is 'latextable.tex'.

type

Can be either std for the standard LaTeX table or xtab for a xtabular table for multipage tables. The later requires the xtab latex-package (\usepackage{xtab} in your LaTeX document). Default is std.

The header. It is a reference to an array (the rows) of array references (the columns).

  $table->set_header([ [ 'Name', 'Beers' ] ]);

will produce following header:

  +------+-------+
  | Name | Beers |
  +------+-------+

Here an example for a multirow header:

  $table->set_header([ [ 'Name', 'Beers' ], ['', '(roughly)' ] ]);

This code will produce this header:

  +------+-----------+
  | Name |   Beers   |
  |      | (roughly) |
  +------+-----------+
data

The data. Once again a reference to an array (rows) of array references (columns).

  $table->set_data([ [ 'Marge', '1' ], [ 'Homer', '4' ] ]);

And you will get a table like this:

 +-------+---------+
 | Marge |       1 |
 | Homer |       4 |
 +-------+---------+

An empty column array will produce a horizontal line:

  $table->set_data([ [ 'Marge', '1' ], [], [ 'Homer', '4' ] ]);

And you will get a table like this:

 +-------+---------+
 | Marge |       1 |
 +-------+---------+
 | Homer |       4 |
 +-------+---------+

TABLE ENVIRONMENT

table_environment

If 1, then a table environment will be generated. Default 1. This option only affects tables of type std.

    \begin{table}[htb]
        \center
        \begin{tabular}{|l||r|r|}
        ...
        \end{tabular}
        \caption{Number of beers}
        \label{table_beercounter}
    \end{table} 
caption

The caption of the table. Only generated if get_caption() returns a true value. Default is 0. Requires table_environment.

center

Defines whether the table is centered. Default 1 (centered). Requires table_environment.

label

The label of the table. Only generated if get_label() returns a true value. In Latex you can create a reference to the table with \ref{label}. Default is 0. Requires table_environment.

maincaption

If get_maincaption() returns a true value, then this value will be displayed in the Table Listing (\listoftables) and before the caption. Default 0. Requires table_environment.

size

Font size. Valid values are 'tiny', 'scriptsize', 'footnotesize', 'small', 'normal', 'large', 'Large', 'LARGE', 'huge', 'Huge' and 0. Default is 0 (does not define a font size). Requires table_environment.

tablepos

The position of the table, e.g. htb. Only generated if get_tablepos() returns a true value. Requires table_environment.

TABULAR ENVIRONMENT

callback

If get_callback() returns a true value and the return value is a code reference, then this callback function will be called for every column. The passed arguments are $row, $col (both starting with 0), $value and $is_header.

   use LaTeX::Encode;
   ...
   
   # use LaTeX::Encode to encode LaTeX special characters,
   # lowercase the third column (only the data)
   my $table = LaTeX::Table->new(
       {   header   => $header,
           data     => $data,
           callback => sub {
               my ( $row, $col, $value, $is_header ) = @_;
               if ( $col == 2 && !$is_header ) {
                   $value = lc $value;
               }
               return latex_encode($value);
           },
       }
   );
tabledef

The table definition, e.g. |l|r|c|r|. If unset, LaTeX::Table tries to guess a good definition. Columns containing only numbers are right-justified, others left-justified. Columns with cells longer than 50 characters are paragraph columns of size 5 cm. These rules can be changed with set_tabledef_strategy(). Default is 0 (guess good definition).

tabledef_strategy

Controls the behaviour of the tabledef calculation when get_tabledef() does not return a true value. Is a reference to a hash with following keys:

IS_A_NUMBER => $regex

Defines a column as NUMBER when all cells in this column match the specified regular expression. Default is qr{\A([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?\z}xms.

IS_LONG => $n

Defines a column as LONG when one cell is equal or larger than $n characters (default 50).

NUMBER_COL => $attribute

The tabledef attribute for NUMBER columns. Default 'r' (right-justified).

LONG_COL => $attribute

The tabledef attribute for LONG columns. Default 'p{5cm}' (paragraph column with text vertically aligned at the top, width 5cm). Note that this requires that get_text_wrap() returns 0.

DEFAULT => $attribute

The tabledef attribute for columns that are neither NUMBER nor LONG. Default 'l' (left-justified).

Example:

  $table->set_tabledef_strategy({
    IS_A_NUMBER => qr{\A \d+ \z}xms, # integers only;
    IS_LONG     => 60, # min. 60 characters
    LONG_COL    => 'm{7cm}', # vertically aligned at the middle, 7cm
  });
text_wrap

If get_text_wrap() returns a true value and if the return value is a reference to an array of integer values, then Text::Wrap is used to wrap the column after the specified number of characters. More precisely, Text::Wrap ensures that no column will have a length longer than $characters - 1.

  # wrap first and last column after 10 characters, second column after 60
  $table->set_text_wrap([ 10, 60, 10 ]);

Be aware that text_wrap wraps the content of the columns and does not try to filter out LaTeX commands - thus your formatted LaTeX document may display less characters than desired. It also turns off the automatic p attribute in the table definition.

width

If get_width() returns a true value, then {tabular*}{width}{@{\extracolsep{\fill}} ... } (or {xtabular*}{width}{ ... }, respectively) is used.

  # use 75% of textwidth 
  $table->set_width('0.75\textwidth');

MULTIPAGE TABLES

tabletailmsg

Message at the end of a multipage table. Default is Continued on next page.

tabletail

Custom table tail. Default is multicolumn with the tabletailmsg (see above) right-justified.

xentrystretch

Option for xtab. Play with this option if the number of rows per page is not optimal. Requires a number as parameter. Default is 0 (does not use this option).

    $table->set_xentrystretch(-0.1);

THEMES

theme

The name of the theme. Default is Zurich.

predef_themes

All predefined themes. Getter only.

custom_themes

All custom themes. See "CUSTOM THEMES".

MULTICOLUMNS

Multicolumns can be defined in LaTeX with \multicolumn{#cols}{definition}{text}. Because multicolumns are often needed and this is way too much code to type, a shortcut was implemented. Now, text:#colsdefinition is equivalent to original LaTeX code. For example, Beers:2|c| is equivalent to \multicolumn{2}{|c|}{Beers}. Note that |c| overrides the LINES settings in the theme (See "CUSTOM THEMES").

THEMES

The theme can be selected with $table->set_theme($themename). Currently, following predefined themes are available: Zurich, Dresden, Berlin, Miami and Houston. The script generate_examples.pl in the examples directory of this distributions generates some examples for all available themes.

ZURICH

The default theme. Requires \usepackage{booktabs} in your LaTeX document. The top and bottom lines are slightly heavier (ie thicker, or darker) than the other lines. No vertical lines.

  ------------------------
    Name     Beer   Wine  
  ------------------------
    Marge       1      0  
    Homer       4      0  
  ------------------------

DRESDEN

Nice and clean, with a centered header written in bold text. Header and first column are separated by a double line.

  +-------++------+------+
  | Name  || Beer | Wine |
  +-------++------+------+
  +-------++------+------+
  | Marge ||    1 |    0 |
  | Homer ||    4 |    0 |
  +-------++------+------+

BERLIN

First column separated by only one line.

  +-------+------+------+
  | Name  | Beer | Wine |
  +-------+------+------+
  +-------+------+------+
  | Marge |    1 |    0 |
  | Homer |    4 |    0 |
  +-------+------+------+
 

HOUSTON

Very similar to Dresden, but columns are separated (one inner line).

  +-------++------+------+
  | Name  || Beer | Wine |
  +-------++------+------+
  +-------++------+------+
  | Marge ||    1 |    0 |
  +-------++------+------+
  | Homer ||    4 |    0 |
  +-------++------+------+

MIAMI

A very simple theme. Header once again written in bold text.

    Name    Beer   Wine
  -----------------------
    Marge      1      0
    Homer      4      0

CUSTOM THEMES

Custom themes can be defined with an array reference containing all options (explained later):

    my $themes = { 
                'Leipzig' => {
                    'HEADER_FONT_STYLE'  => 'sc',
                    'HEADER_CENTERED'    => 1,
                    'CAPTION_FONT_STYLE' => 'sc',
                    'VERTICAL_LINES'     => [ 1, 2, 1 ],
                    'HORIZONTAL_LINES'   => [ 1, 2, 0 ],
                    'BOOKTABS'           => 0,
                },
            };

    $table->set_custom_themes($themes);
Fonts

HEADER_FONT_STYLE, CAPTION_FONT_STYLE. Valid values are bf (bold), it (italics), sc (caps) and tt (typewriter). When this option is undef, then header (or caption, respectively) is written in normal font.

Lines

VERTICAL_LINES, HORIZONTAL_LINES. A reference to an array with three integers, e.g. [ 1, 2, 0 ]. The first integer defines the number of outer lines. The second the number of lines after the header and after the first column. The third is the number of inner lines. For example "DRESDEN" is defined as:

            'Dresden' => {
                ...  
                'VERTICAL_LINES'     => [ 1, 2, 1 ],
                'HORIZONTAL_LINES'   => [ 1, 2, 0 ],
            }

The first integers define one outer line - vertical and horizontal. So a box is drawn around the table. The second integers define two lines between header and table and two vertical lines between first and second column. And finally the third integers define that columns are separated by a single vertical line whereas rows are not separated by horizontal lines.

Misc
HEADER_CENTERED

Valid values are 0 (not centered) or 1 (centered).

BOOKTABS

Use the Booktabs package for "Publication quality tables". 0 (don't use this package) or 1 (use it).

DIAGNOSTICS

callback is not a code reference

The return value of get_callback() is not a code reference. See "TABULAR ENVIRONMENT".

data/header is not an array reference

get_data() (or get_header(), respectively) does not return a reference to an array. See "BASIC OPTIONS".

data[$i]/header[$i] is not an array reference

The ith element of get_data() (or get_header()) is not an array reference. See "BASIC OPTIONS".

data[$i][$j]/header[$i][$j] is not a scalar

The jth column in the ith row is not a scalar. See "BASIC OPTIONS".

DEPRECATED. Use options header and data instead.

You have called either generate() or generate_string() with header and data as parameters. This is deprecated since LaTeX::Table 0.1.0. See "BASIC OPTIONS".

Family not known: ... . Valid families are: ...

You have set a font family to an invalid value. See "CUSTOM THEMES".

Size not known: ... . Valid sizes are: ...

You have set a font size to an invalid value. See "CUSTOM THEMES".

tabledef_strategy not a hash reference.

The return value of get_tabledef_strategy() is not a hash reference. See "TABULAR ENVIRONMENT".

text_wrap is not an array reference

The return value of get_text_wrap() is not an array reference. See "TABULAR ENVIRONMENT".

Theme not known: ...

You have set the option theme to an invalid value. See "THEMES".

Undefined value in data[$i][$j]/header[$i][$j]

The value in this cell is undef. See "BASIC OPTIONS".

Value in text_wrap not an integer: ...

All values in the text_wrap array reference must either be undef or must match the regular expression m{\A \d+ \z}xms. See "TABULAR ENVIRONMENT".

xentrystretch not a number

You have set the option xentrystretch to an invalid value. This option requires a number. See "MULTIPAGE TABLES".

CONFIGURATION AND ENVIRONMENT

LaTeX::Table requires no configuration files or environment variables.

DEPENDENCIES

Carp, Class::Std, English, Fatal, Scalar::Util, Text::Wrap

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-latex-table@rt.cpan.org, or through the web interface at http://rt.cpan.org.

SEE ALSO

Data::Table, LaTeX::Encode

AUTHOR

Markus Riester <mriester@gmx.de>

LICENSE AND COPYRIGHT

Copyright (c) 2006-2008, Markus Riester <mriester@gmx.de>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.