# Customization

The transformation process (and thus the output rendering) can be heavily customized by:

- using some configuration parameters either in a configuration stylesheet or directly from the command line,

- using Processing Instructions to create some instructions very specific to dblatex,

- using some customized stylesheets,

- using a customized LaTeX style package.

- using a LaTeX post process script or plugin.

All these customization methods can be used independently and in exceptional cases, but it can also be combined and registered in a master configuration file, called a specification file (cf. ) to create a new tool dedicated to your needs.

## Using XSL Parameters

The PDF rendering can be customised by using some XSL configuration parameters. contains the reference documentation of the available user-configurable parameters.

## Setting Command line Parameters

You can set some XSL parameters directly from the command line without creating a configuration parameter stylesheet, with the `-P parameter=value` option.

The following example set the latex.hyperparam parameter value:

      dblatex -P latex.hyperparam=colorlinks,linkcolor=blue myfile.xml

## Using Processing Instructions

Dblatex has Processing Instructions (PI) which can modify the default document formatting. Usually these instructions alter the output formatting of specific DocBook elements, like table cells.

To alter formatting globally it is better to set an appropriate stylesheet parameter.

Processing instructions are written `<?`name content `?>`. Most often content will have the form of one or more XML attributes with a value, as follows: `<?`name attribute`="`value`" ?>`.

The list of the available Processing Instructions are given in .

Here is an example of a PI used in a table:

Table width specified with a Processing Instruction

## XSL User Stylesheet

You can provide your own XSL stylesheet to set some of the XSL parameters, and/or to override some of the dblatex XSL templates. The user stylesheet is specified by using the option `-p custom.xsl`.

### Changing the XSL parameter values

The parameters can be stored in a user defined XSL stylesheet. An example of configuration stylesheet is given with this manual:

### Overriding some templates

You can directly put the overriding templates in your XSL stylesheet, but do not try to import the default dblatex stylesheets in it: it is automatically done by the tool. So, just focus on the template to override and dblatex will ensure that your definitions will get precedence over the dblatex ones.

You can of course split your templates in several files, and import each of them in the main user stylesheet by calling `xsl:import`.

Overriding templates

    <?xml version='1.0' encoding="iso-8859-1"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>

    <!-- Let's import our own XSL to override the default behaviour. -->
    <xsl:import href="mystyle.xsl"/>

    <!-- Let's patch directly a template here -->
    <xsl:template match="article" mode="docinfo">
      <xsl:apply-imports/>
      <xsl:text>\let\mymacro=\DBKrelease</xsl:text>
    </xsl:template>

    </xsl:stylesheet>

## Customized LaTeX style

The actual output rendering is done by the latex style package used, and not by the XSL stylesheets whose role is only to translate to latex. Users can provide their own LaTeX style file, in respect of some rules:

- The LaTeX style package preamble must support all the options that the XSL stylesheets can pass to the package.

- Some packages must be used to make all the thing work.

- The docbook interface must be defined: the XSL stylesheets register some elements information in LaTeX commands. These commands or macro are the only ones specific to DocBook that are explicitely used by the XSL stylesheets. Other specific macros are used but are not intended to be changed by the user. These hidden macros are defined in the dbk_core latex package.

The latex style file to use is specified by using the option `--texstyle latex_style`. An example of a simple LaTeX DocBook style is provided in the package.

The `--texstyle latex_style` option accepts a package name (no path and no `.sty` extension) or a full style file path. If a full path is used, the filename must ends with `.sty`.

    # Give a package name and assume its path is already in TEXINPUTS
    dblatex --texstyle=mystyle file.xml

    # Give the full package path. The TEXINPUTS is then updated by dblatex
    dblatex --texstyle=./mystyle.sty file.xml

### Reusing an existing LaTeX style

You can either create your latex style from scratch, in respect of the interfaces described in the following sections, or you can simply reuse an already existing style and override what you want. The latter method is easier for small things to change.

Here is an example of a style package reusing the default docbook style:

Reused LaTeX style

### Package options

A compliant LaTeX style package supports the following options. The options are provided by the XSL stylesheets according to the document attributes.

### Needed packages

A LaTeX style package must at least include the following packages.

### DocBook interface

All the latex commands beginning with DBK are related to elements under bookinfo or articleinfo.

## Latex post process script

Extra user actions can be processed on the latex file produced by the XSL stylesheets or on its temporary working files produced by the latex compilation.

For instance, in the documents I write the cover page must display the number of pages of the document, but written in full letters (e.g. 23 is written “twenty three”). The latex post process script is then helpfull, and in this particular case it patches the .aux file.

The post process script is called just before the last latex compilation, and takes one parameter, the latex file compiled by the tool.

### Post latex compilations

The latex compilations done once the script is called depend on the return code of the script:

- When the return code is 0, `dblatex` continues the compilation as many times as necessary.

- When the return code is 1, no more compilation is done by dblatex. This case is useful if the script needs to control precisely the number of compilation to apply. It is up to the script to perform the expected compilations.

  To do so, the script can retrieve in the `LATEX` environment variable the actual compiler used by `dblatex`.

- When the return code is another value, an error is raised to signal a failed post process script execution.

### Post processing with a Python Plugin

You can use a python plugin instead of a script by prefixing the plugin name with the string `"plugin:"`. When using a plugin you must not put the python suffix in the plugin name. If the plugin is in one of the Python system directories, or in the current directory where you call dblatex, or in one of the directories of the `PYTHONPATH` environment variable, you don't need to specify a directory location. Otherwise put the plugin directory path before the plugin name.

Here are several plugin call examples:

    # The texpost.py module is in one of the python paths
    dblatex -r plugin:texpost file.xml

    # The texpost.py module location is specified with an absolute path
    dblatex -r plugin:/path/to/texpost file.xml

    # The texpost.py module is specified through a relative path from current dir
    dblatex -r plugin:relative/path/from/current/dir/texpost file.xml

The plugin must contain a `main` entry point. `Dblatex` will pass the following parameters to the entry point: latex_file to specify the latex file to post process, and stdout to specify the output stream to use to be consistent with the dblatex verbosity.

Texpost Python Plugin Example

    import sys
    import os

    def main(latex_file, stdout):
        """
        Texpost Plugin Entry point
        """
        # Let's find out the backend used
        tex_engine = os.environ["LATEX"]

        # Let's log something
        print >>stdout, "Plugin called on '%s' file" % (latex_file)

        # Open the latex file and parse it
        texfile = open(latex_file)
        ...

        # Now decide if a new compilation must occur
        if has_changed:
          sys.exit(0)
        else:
          sys.exit(1)

## Dblatex Configuration File

A configuration file, also called a specification file, can be used to list all the customizations and options to apply. Such a file is passed by using the option `-S config_file`. Several configuration files can be specified if needed; it can be usefull to have a default setup and additional setup for specific needs that override or complete the default configuration.

### XML Configuration File Format

You should use the XML format to configure dblatex. It contains more features than the text format used up to the 0.3.7 release that is now deprecated. The principle remains the same, that is, most of the configuration parameters correspond to a command line option.

Here is a full example of a configuration file. In the next sections the meaning and use of the configuration tags are detailed.

Another example, much simpler, is the configuration file used for this manual.

User Manual Configuration File

The following sections detail the meaning of each tag. Note that dblatex provides some schema that you can use to validate your configuration file.

#### <config>

It is the root element of a configuration file. A valid configuration file must have a config root element.

Attributes:

xmlns

  The namespace to use for a dblatex configuration file is: `http://dblatex.sourceforge.net/config`.

The following elements occur in config: , , , .

#### <latex>

The latex element contains the configuration data related to the latex processing.

Attributes: none

The following elements occur in latex:

backend

  It defines the latex engine to use, like the option `--backend` does. The backend name is specified through the use attribute.

texstyle

  It defines the docbook latex style to use, like the option `--texstyle` does. The latex package is specified through the use attribute.

texpost

  It defines the latex post-processing script to use, like the option `--texpost` does. The script name is specified through the use attribute or the fileref attribute.

indexstyle

  It defines index style file (.ist) to use, like the option `--indexstyle` does. The filename is specified through the fileref attribute.

texinputs

  The element text defines the extra paths to add to TEXINPUTS, like the option `--texinputs` does.

bibinputs

  The element text defines the lookup paths of BIBINPUTS, like you would do by successive use of the option `--bib-path`.

bstinputs

  The element text defines the lookup paths of BSTINPUTS, like you would do by successive use of the option `--bst-path`.

#### <xslt>

The xslt element contains the configuration data related to the XSL processing.

Attributes: none

The following elements occur in xslt:

stylesheet

  It defines a user stylesheet to use, like the option `--xsl-user` does. The stylesheet name is specified through the fileref attribute. If several stylesheet elements are set, the precedence is the same like using the option `--xsl-user` several times.

engine

  See .

#### <engine>

It defines the XSLT processor to use; it can be either a predefined engine, or a user-defined command to run.

When the attribute use is used, it works like the option `--xslt`. The engine name is specified through the use attribute.

When the element contains the command or commandchain child element it defines the command(s) to run to perform the XSLT processing. The core keywords defined in apply. The additional keywords of an XSLT engine command are the following:

`%(xmlfile)s`

  Replaced by the XML source file to process.

`%(stylesheet)s`

  Replaced by the stylesheet to use to process the XML file `xmlfile`.

`%(param_list)s`

  Replaced by the list of the XSL parameters to set. The parameter pair formatting (the parameter name and its value) is specified through the param-format attribute given to the engine element.

Attributes:

param-format

  This attribute is mandatory when the engine is specified through a command. It tells how a parameter name and value pair shall be formatted when added to the XSLT engine command. The formatting is given by a string containing the keywords replaced by the name or value of the parameter to set:

  `%(param_name)s`

    Replaced by the name of the parameter to set.

  `%(param_value)s`

    Replaced by the value to set to the parameter.

The following example re-writes with a user-defined command the Saxon engine with an additional `-T` option:

    <xslt>
      <engine param-format="%(param_name)s=%(param_value)s">
        <command>
        saxon-xslt -T -o %(output)s %(xmlfile)s
                                    %(stylesheet)s %(param_list)s</command>
      </engine>
    </xslt>

#### <imagedata>

The imagedata element contains the rules and commands to convert on the fly the images included in the docbook document.

Attributes: none

The following elements occur in imagedata:

figpath

  It defines a path where to find the images, like the option `--fig-path` does.

figformat

  It defines the default image format when no suffix is available to deduce the actual format, like `--fig-format` does.

formatrule

  It specifies in which format an image must be converted, depending on the context, given by some attributes:

  docformat

    The output document for which the rule applies. When not set the rule applies for any format

  backend

    The rule applies only when this backend is used

  dst

    When the conditions are met the image shall be converted to this format.

  The following example:

      <formatrule docformat="pdf" backend="xetex" dst="pdf"/>

  tells that when a PDF document is built with the xetex backend, the graphics not natively supported shall be converted to PDF.
converter

  It defines an image transformation rule. See for more details.

#### <converter>

The converter element defines a rule about how to convert on the fly an image in a docbook document to the corresponding image used in the built latex file. The goal is to have an image format compatible with the latex engine, whatever the original format is. `Dblatex` has a default set of rules, but one can overwrite or add some rules.

Attributes:

src

  Format of the original image referenced in the XML document. The converter only applies to the images matching this format.

dst

  Target format once converted. The converter only applies to the original images having the format `src` and that need to be converted to the format `dst`. `dst` can take the special value "`*`" meaning that the rule applies for all the possible target formats (e.g. eps, pdf, png).

docformat

  Specify for which document output format the converter applies. For example, if `docformat` is set to "pdf", it means that the converter applies only if the document output format is PDF. The converter will not apply if you want to build a PostScript or DVI document.

backend

  The converter applies only if it is this latex engine that is used. For example, it is usefull if you want to convert an image in a specific way only when xetex is used.

The following elements occur in converter:

commandchain

  List of the commands to run. A commandchain contains one or more command elements.

command

  command to execute to perform the conversion, or a part of the conversion if several commands are chained. See .

#### <command>

The command element contains the arguments of the command to run. The arguments can contain some predefined keywords that are replaced by the actual values when the command is executed. The known core keywords are:

`%(src)s`

  Replaced by the `src` value (original image format).

`%(dst)s`

  Replaced by the `dst` value (target image format).

`%(input)s`

  Replaced by the input file required by the command. In the case of an image conversion, it is the filename of the original image to convert.

`%(output)s`

  Replaced by the name of the ouput file required by the command. In the case of an image conversion, it is the filename of the output image once converted.

Attributes:

input

  Standard input of the command to run. It can specify a file or it can specify to use the output of the preceding command if the keyword "PIPE" is used.

output

  Standard output of the command to run. It can specify a file or it can specify that the output shall be redirected to the next command if the keyword "PIPE" is used.

shell

  When set to "1" or "true", the command is run in a shell environment.

#### <options>

The options element lists the extra arguments to pass to `dblatex`.

### Deprecated Text Configuration File Format

The format of the file is the following:

- Every comment starts with a “#”, and is ignored.

- The file must contain one parameter by line.

- The format of a parameter is the following:

      <keyword>: <value>

- Every parameter is mapped to an option that can be passed to `dblatex`.

- An unknown parameter is silently ignored (the whole line is dropped).

- The parameters defining a path (a file or a directory) can take absolute or relative paths. A relative path must be defined from the specification file itself. For instance, a specification file under `/the/spec/directory/` with a parameter describing the file `../where/this/file/is/myfile` points to `/the/spec/where/this/file/is/myfile`.

The following table lists the supported parameters and the corresponding command line option.

### Style Paths

By default `dblatex` tries to find the configuration files related to a style (specified with option `-T`) in the following paths, in respect of the order:

1. The current directory

2. `$HOME/.dblatex`

3. `/etc/dblatex`

4. The dblatex package configuration directories.

You can add some extra paths where to look for by setting the `DBLATEX_CONFIG_FILES` environment variable. The paths are separated by ":" in Unix like systems, and by ";" on Windows. These paths are used only when nothing is found in the default paths.

## Customization Precedence

All the customization queries are translated to the corresponding command line options. Thus, using several customization methods can be unconsistent because each of them override the same option with another value.

For instance, you can specify the use of a specification file in which it is said to use a latex style (parameter TexStyle) and explicitely use the `--texstyle` command line option. So, what is the behaviour?

The options order is the following:

- If a specification file is used (`-S` option), the options are set to the specification file parameters.

- If several specification files are used (`-S` option), the precedence is given to the last specified files.

- The options explicitely passed override the specification file setting, whatever is the position of the options (i.e. before or after the `-S` option).

- If an option is passed several times, this is the last occurence that is used.

Customization Precedence

Let's consider the specification file containing the following parameters:

    <?xml version="1.0" ?>
    <config xmlns="http://dblatex.sourceforge.net/config">
      <xslt><stylesheet fileref="file3.xsl"/></xslt>
      <options>-b pdftex</options>
      <latex><texstyle use="mystyle1"/></latex>
    </config>

And now the command line:

    dblatex -b dvips -p file1.xsl -p file2.xsl -S file.specs -s mystyle2 mydoc.xml

The setting used is the following:

- “-b dvips” overrides “-b pdftex” set by the spec file.

- “-p file2.xsl” overrides “-p file1.xsl” since it is defined after, and overrides “file3.xsl” set by the spec file.

- “-s mystyle2” override “mystyle1” set by the spec file.