Template Script Bindings

Last modified by Vincent Massol on 2026/08/30 20:43

Reference

A LaTeX template is a Velocity script, so it has the standard bindings any wiki script has, $services and the Velocity tools among them. On top of those the renderer adds $latex, which is the whole of the LaTeX rendering API, and $SP.

BindingWhat it gives
$latex.blockThe Block being rendered.
$latex.processorRenders other Blocks, and other templates.
$latex.toolEscaping, the document language, Block tests and the shared stacks.
$latex.propertiesThe options the export was started with.
$latex.resourceConverterPuts attachments and generated files into the exported package.
$latex.includesThe page files already written to that package.
$SPOne space character.

The last four belong to an export. A template rendered outside one still finds them, and the FAQ below says what they hold there.

$latex.block

The org.xwiki.rendering.block.Block the template was chosen for, with its own API: getChildren(), getParameter($name), isInline(), getParent(), and whatever its class adds on top — getWord() on a WordBlock, getLevel() on a HeaderBlock. It is set again for every Block, so a template always sees its own.

Handing its children back to the processor is the line almost every template contains:

$latex.processor.process($latex.block.getChildren())

$latex.processor

CallReturns
process($blocks)The LaTeX for a collection of Blocks, each rendered through its own template.
render($name)The LaTeX the named template produces.
getTemplate($name)That template, or nothing when it exists on neither lookup path.
render($template)The LaTeX a template already in hand produces, which is how an override renders the default it has just modified.

A name here is relative and carries no latex/ prefix — Preamble, macros/code — and goes through the same two-path lookup as a Block's own template. getTemplate is how a template checks that another one is there before rendering it.

$latex.tool

CallReturns
escape($text)The text with LaTeX's special characters replaced by their escaped forms.
normalizeLabel($text)The text with the characters a \label cannot hold removed.
languageThe document language spelled the way babel wants it, lowercase: english, french.
getStack($id)A stack shared by every template of the export, created empty on first use.
getSiblings($block)The Blocks following the one passed, under the same parent.
getParentBlock($block)Its parent, skipping the MacroMarkerBlock wrappers.
isTableCell($block)True for a table cell, header cells included.
isFigure($block)True for a FigureBlock.
isIdBlockInline($idBlock)True when an id macro sits inside a paragraph rather than on its own.
previousSiblingsContainsOnlyIdMacros($block)True when nothing but id macros precedes the Block under its parent.
getInlineDescendants($block)The descendants of the Block that can be rendered as inline content.
getDescendantMetaDataBlockWithParameterName($block, $name)The first MetaDataBlock below it carrying that parameter-name, or nothing.
figureToolThe figure helpers below.

A template that writes text cannot do without escape: these ten characters mean something to LaTeX and have to be neutralised first.

\ { } # $ % & ^ _ ~

normalizeLabel is the variant for a \label, which refuses seven of them outright rather than accepting an escape; it drops those and keeps &, ^ and _.

Figure Helpers

$latex.tool.figureTool holds what the FigureBlock and FigureCaptionBlock templates need.

CallReturns
isTable($figureBlock)True when the figure holds a table.
getFigureEnvironment($figureBlock)The environment to open for it, table or figure.
getFigureEnvironmentParameter($figureBlock)The Block written as that environment's placement option, h unless the figure overrides it.
isFigureCaptionLast($captionBlock)True when the caption comes after the figure content rather than before it.
displayFigureCaption($captionBlock)Whether the caption should be written at all.

$latex.properties

The options the export was started with. They are the eleven fields of the export form, and Export Options says what each one does.

PropertyType
$latex.properties.documentClassString
$latex.properties.coverPageBoolean
$latex.properties.coverPageImageString
$latex.properties.pageNumberingBoolean
$latex.properties.tocBoolean
$latex.properties.listOfFiguresBoolean
$latex.properties.listOfTablesBoolean
$latex.properties.titleString
$latex.properties.subtitleString
$latex.properties.authorString
$latex.properties.dateDate

The index template is where they are read, one guard per option:

#if ($latex.properties.toc)
  \setcounter{tocdepth}{3}
  \tableofcontents
#end

$latex.resourceConverter

CallReturns
convert($linkBlock), convert($imageBlock)The Block's reference, rewritten to the copy the converter has just added to the package.
convert($reference, $forceDownload)The same for a bare ResourceReference; with true, external URLs are downloaded so that they become local files too.
convert($reference, $baseReference, $forceDownload)The same again, resolving a relative reference against the base reference passed.
store($path, $stream)Adds content to the package at that path, and returns nothing.

The shipped ImageBlock template shows the pattern: convert first, write the reference the converter hands back, escape it like any other text.

#set ($resourceReference = $latex.resourceConverter.convert($imageBlock))
\includegraphics{$latex.tool.escape($resourceReference.reference)}

Its Java interface is LaTeXResourceConverter.

$latex.includes

The paths of the page files written to the package so far, in the order they were added. The index template is what loops over them to pull each page into the document. Outside an export nothing writes page files, so the list is empty.

$SP

Leading whitespace is stripped from every line of a template before it runs, so that a template can be indented for reading without leaking that indentation into the LaTeX. A line whose output really has to begin with spaces asks for them with $SP:

\newcommand{\xwikititle}[1]{
  ${SP}${SP}\title{#1}}

FAQ

Why is $latex.properties empty?

Only an export fills it. A template reached through a plain latex/1.0 render, from a script or from Java, gets a $latex binding with no properties in it, so every option reads as nothing. A template that has to work both ways guards with $!latex.properties.toc rather than $latex.properties.toc.

Why does $latex.resourceConverter leave my references alone?

Because outside an export the renderer binds a converter that does nothing and returns the reference unchanged. The one that copies files comes with the export package machinery, and without a package there is nowhere to copy anything to.

Related

Get Connected