Scroll to navigation

Template::Latex(3pm) User Contributed Perl Documentation Template::Latex(3pm)

NAME

Template::Latex - Latex support for the Template Toolkit

SYNOPSIS

    use Template::Latex;
    
    my $tt = Template::Latex->new({
        INCLUDE_PATH  => '/path/to/templates',
        OUTPUT_PATH   => '/path/to/pdf/output',
        LATEX_FORMAT  => 'pdf',
    });
    my $vars = {
        title => 'Hello World',
    }
    $tt->process('example.tt2', $vars, 'example.pdf', binmode => 1)
        || die $tt->error();

DESCRIPTION

The Template::Latex module is a wrapper of convenience around the Template module, providing additional support for generating PDF, PostScript and DVI documents from LaTeX templates.

You use the Template::Latex module exactly as you would the Template module.

    my $tt = Template::Latex->new(\%config);
    $tt->process($input, \%vars, $output)
        || die $t->error();

It supports the "LATEX_FORMAT" option to specify the default output format. This can be set to "pdf", "ps" or "dvi".

    my $tt = Template::Latex->new({
        LATEX_FORMAT  => 'pdf',
    });

Previous versions of the module supported the "LATEX_PATH", "PDFLATEX_PATH", "DVIPS_PATH", "PS2PDF_PATH", "BIBTEX_PATH" and "MAKEINDEX_PATH" options. These are now deprecated and their use will result in a deprecation warning, as their use would result in modifying global state, disallowing different values for different simultaneous instances.

To change the paths of the various programs being called by the LaTeX::Driver module which this module wraps, the user is referred to the API of that module. This module provides a number of (wrapper) class methods around the LaTeX::Driver routine (latex_path() and friends).

The "latex" filter is automatically defined when you use the Template::Latex module. There's no need to load the Latex plugin in this case, although you can if you want (e.g. to set some configuration defaults). If you're using the regular Template module then you should first load the Latex plugin to define the "latex" filter.

    [% USE Latex %]
    [% FILTER latex('example.pdf') %]
    ...LaTeX doc...
    [% END %]

PUBLIC METHODS

The Template::Latex module is a subclass of the Template module and inherits all its methods. Please consult the documentation for the Template module for further information on using it for template processing. Wherever you see "Template" substitute it for "Template::Latex".

In addition to those inherted from the Template module, the following methods are also defined.

latex_paths()

Method to get or set the paths to the latex, pdflatex and dvips programs. These values are stored in the Template::Latex $LATEX, $PDFLATEX and $DVIPS package variables, respectively. It can be called as either a class or object method.

    Template::Latex->latex_paths({
        latex    => '/usr/bin/latex',
        pdflatex => '/usr/bin/pdflatex',
        dvips    => '/usr/bin/dvips',
    });
    my $paths = Template::Latex->latex_paths();
    print $paths->{ latex };    # /usr/bin/latex

latex_path()

Method to get or set the $Template::Latex::LATEX package variable which defines the location of the latex program on your system. It can be called as a class or object method.

    Template::Latex->latex_path('/usr/bin/latex');
    print Template::Latex->latex_path();   # '/usr/bin/latex'

pdflatex_path()

Method to get or set the $Template::Latex::PDFLATEX package variable which defines the location of the pdflatex program on your system. It can be called as a class or object method.

    Template::Latex->pdflatex_path('/usr/bin/pdflatex');
    print Template::Latex->pdflatex_path();   # '/usr/bin/pdflatex'

dvips_path()

Method to get or set the $Template::Latex::DVIPS package variable which defines the location of the dvips program on your system. It can be called as a class or object method.

    Template::Latex->dvips_path('/usr/bin/dvips');
    print Template::Latex->dvips_path();   # '/usr/bin/dvips'

bibtex_path()

Method to get or set the $Template::Latex::BIBTEX package variable which defines the location of the bibtex program on your system. It can be called as a class or object method.

    Template::Latex->bibtex_path('/usr/bin/bibtex');
    print Template::Latex->bibtex_path();   # '/usr/bin/bibtex'

makeindex_path()

Method to get or set the $Template::Latex::MAKEINDEX package variable which defines the location of the makeindex program on your system. It can be called as a class or object method.

    Template::Latex->makeindex_path('/usr/bin/makeindex');
    print Template::Latex->makeindex_path();   # '/usr/bin/makeindex'

INTERNALS

This section is aimed at a technical audience. It documents the internal methods and subroutines as a reference for the module's developers, maintainers and anyone interesting in understanding how it works. You don't need to know anything about them to use the module and can safely skip this section.

define_filter($context,\%config)

This class method installs the "latex" filter in the context passed as the first argument. The second argument is a hash reference containing any default filter parameters (e.g. those specified when the Template::Plugin::Latex plugin is loaded via a "USE" directive).

    Template::Latex->define_filter($context, { format => 'pdf' });

The filter is installed as a dynamic filter factory. This is just a fancy way of saying that the filter generates a new filter subroutine each time it is used to account for different invocation parameters. The filter subroutine it creates is effectively a wrapper (a "closure" in technical terms) around the "filter()" subroutine (see below) which does the real work. The closure keeps track of any configuration parameters specified when the filter is first defined and/or when the filter is invoked. It passes the merged configuration as the second argument to the "filter()" subroutine (see below).

See the Template::Filters module for further information on how filters work.

filter($text,\%config)

This is the main LaTeX filter subroutine which is called by the Template Toolkit to generate a LaTeX document from the text passed as the first argument. The second argument is a reference to a hash array of configuration parameters. These are usually provided by the filter subroutine that is generated by the filter factory.

    Template::Latex::filter($latex, { 
        latex    => '/usr/bin/latex',
        pdflatex => '/usr/bin/pdflatex',
        dvips    => '/usr/bin/dvips',
        output   => 'example.pdf',
    });

throw($message)

Subroutine which throws a Template::Exception error using "die". The exception type is set to "latex".

    Template::Latex::throw("I'm sorry Dave, I can't do that");

debug($message)

Debugging subroutine which print all argument to STDERR. Set the $DEBUG package variable to enable debugging messages.

    $Template::Latex::DEBUG = 1;

AUTHOR

Andrew Ford <a.ford@ford-mason.co.uk> (current maintainer)

Andy Wardley <abw@wardley.org> <http://wardley.org/>

The original Latex plugin on which this is based was written by Craig Barratt with additions for Win32 by Richard Tietjen.

COPYRIGHT

  Copyright (C) 1996-2006 Andy Wardley.  All Rights Reserved.
  Copyright (C) 2006-2014 Andrew Ford.  All Rights Reserved.
  Copyright (C) 2014-2016 Chris Travers. All Rights Reserved.

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

SEE ALSO

Template::Plugin::Latex

2022-11-19 perl v5.36.0