Skip To Article
Jinja LaTeX Templates

Templating Rules

jtex uses nunjucks, which is a javascript port of Jinja templating language with the modifications to allow easy reading and syntax highlighting with LaTeX\LaTeX. The nunjucks ecosystem is compatible with jinja templates, and they are modified with custom rules.

Syntax

customizedstandard jinja2
Statements[# #]{% %}
Expressions[- -]{{ }}
Comments%# #%{# #}
Line Comment%%##

Statements can be if-blocks or for-loops, a minimal freeform LaTeX\LaTeX example illustrating these would be:

\documentclass{article}
\begin{document}
\section{Famous People}
%% Print a list of famous people defined in the context dictionary
\begin{itemize}
[# for person in famous_people #]
\item [-person.name-], [-person.job-] [# if person.email #]([-person.email-])[# endif #]
[# endfor #]
\end{itemize}
\end{document}

Other environment differences

In addition to the custom syntax we also set the following options:

OptionjtexDefaultDescription
trim_blockstruefalseFirst newline after a block is removed
autoescapefalsetrueHTML autoescaping feature is disabled

jinja provide a whole host of tags, expressions and filters at global scope.

Filters

Some of the main filters used in the LaTeX\LaTeX templates, are, for example, join, title, or trim.

template.tex

\keywords{[- doc.keywords | join(", ") -]}

output.tex

\keywords{Keyword1, keyword2}

Controlling Whitespace

Jinja allows you to strip all leading or trailing whitespace by adding a minus sign (-) to the start or end block or a variable. See nunjucks docs for more information.

The following expression will ensure that a template block, that may be templated over many lines in the template, will only show up on a single line in the exported LaTeX. In this case the syntax is [#- for blocks and [-- for variables.

[#- if parts.acknowledgments -#]
\section*{Acknowledgments}
[- parts.acknowledgments -]
[#- endif -#]

This can also be used for inline variables that will have the final output only exist on a single line in the final export.

Expressions

Note that even though the template is being rendered from a javascript environment, expressions and comparisons are Python-like. That is, they follow and over && as well as not over !.

MyST MarkdownMyST Markdown
Community-driven tools for the future of technical communication and publication, part of Jupyter.