Skip to content

LaTeX template tips and tricks

Tom Houlé edited this page Dec 22, 2017 · 4 revisions

Since LaTeX has quirks, and Tera too, here are a few tips about common problems we encountered writing templates for Papers.

Handling null values

Tera will crash when trying to dereference fields on null values. The way to handle that is conditionals:

{% if basket and basket.totalPrice %}
    {{basket.totalPrice}}
{% else %}
    --
{% endif %}

Handling empty strings

One thing to pay attention to is when using \\ for line breaks in LaTeX, if the line is empty, compilation will fail. You can use a non-breaking space (~) to make sure the line is not empty.

Multiline string variables

If you want to preserve the line breaks in the LaTeX output, you will have to do the following.

Outside tables:

{{ my_multiline_variables | replace(from="
", to="~ \\")}}

Inside tables, you will have to ensure the appropriate number of empty cells, so it is trickier. Assuming you want your variable to fill the last column:

total price & × & green & 33 \\
            &   &       & {{ my_multine_variable | replace(from="
", to="\\ & & &")}}

LaTeX-Tera curly brackets conflicts

It can be hard to put a variable with the {{ name }} syntax in Tera inside a LaTeX macro invocation (something like \textbf{mytext}). Here is the current solution:

\textbf{% raw %}{{% endraw %}{{user.email}}{% raw %}}{% endraw %}

Papers may provide a helper for this in the future.

Formatting numbers

The numprint LaTeX package implements locale-aware number formatting, so you can have the expected decimal and thousand separators.

\usepackage{numprint}

Also make sure to properly set your language environment in the \documentclass.

And in the body of the template:

\numprint{% raw %}{{% endraw %}{{space.size}}{% raw %}}{% endraw %}

If you can format the numbers before sending them to Papers, this is also a good idea.

Formatting dates

There are native LaTeX solutions to format dates, but Tera also does a good job of that:

{{ booking.start_date | date(format="%d.%m.%Y") }}

Layout

Minipages

Minipages enable complex layouts. \hfill combined with minipages works a bit like a CSS flexbox.