Getting Started¶
Basic Syntax¶
With \(\LaTeX\) syntax in mind, latest
defines a command and an environment.
But, first of all, let’s see how to leverage all the power of python in a template.
Python expressions¶
You can include a python expression within a template. By default, the syntax is:
{$ python expression $}
A python expression returns a python object, so if you want a string instead, you can use the latest
command.
For more complex tasks involving some logic like for loops or conditionals you can refer to
latest
environment.
Contexts¶
An expression needs a context (like a python dictionary) to be evaluated. A context is generally provided globally for a template. This context object is passed to every python expression, command or environment in the template. However, commands and environments can modify the context to work with in their content.
Commands¶
The latest
command let you automatically convert to a string the output of a python expression.
By default, the syntax is:
\latest[options]{$ python expression $}
For example, the expression:
If a = \latest{$ a $} and b = \latest{$ b $}, then a + b = \latest{$ a+b $}
with a data context {'a': 1, 'b': 2}
evaluates to:
If a = 1 and b = 2, then a + b = 3
Environments¶
A latest
environment allow us to change the globally defined context. This can be useful for many purposes
depending on the context provided:
dict
context: to ease the access to names in the context for the python expressions, commands, or enviroments nested inside the environmentboolean
context: to provide a conditional functionalitylist
context: to provide a loop functionality
By default, the syntax is:
\begin{latest}{$ context $}[options]
content...
\end{latest}
Creating a template¶
A template file can be of any type but latest
searches in it for latest
commands and enviroments.
The latest cli¶
Run latest
script from the command line:: bash
$ latest template data
where
- template is the path to a template file
- data is the path to a json or yaml formatted data file.
Example¶
An example template file can be something like
\documentclass{article}
\title{\latest{$ meta $}}
\begin{document}
\begin{latest}{$ False $}It doesn't exist.\end{latest}
\begin{latest}{$ True $}This exists and x = \latest{$ data.scalar $}.\end{latest}
\begin{latest}{$ data $}
x = \latest{$ scalar $} so that $x^2$ = \latest{$ scalar ** 2 $}.
\end{latest}
\begin{itemize}
\begin{latest}{$ [{'i': i, 'v': v} for i, v in enumerate(data.vector)] $}[join={$ '\n' $}]
\item index = \latest{$ i $}, value = \latest{$ v $}
\end{latest}
\end{itemize}
\begin{latest}{$ data.tensor $}[join={$ '\n' $}]
true = \latest{$ true $} and false = \latest{$ false $}
\end{latest}
\begin{latest}{$ data $}
\begin{latest}{$ tensor $}
Testing nested structures:
true = \latest{$ true $} and false = \latest{$ false $}
\end{latest}
\end{latest}
\end{document}
while the data file can be something like (yaml)
meta: Testing latest...
data:
scalar: 3
vector:
- 1
- 2
- 3
tensor:
'true': 1
'false': 0
The expected output is
\documentclass{article}
\title{Testing latest...}
\begin{document}
This exists and x = 3.
x = 3 so that $x^2$ = 9.
\begin{itemize}
\item index = 0, value = 1
\item index = 1, value = 2
\item index = 2, value = 3
\end{itemize}
true = 1 and false = 0
Testing nested structures:
true = 1 and false = 0
\end{document}