-
Notifications
You must be signed in to change notification settings - Fork 0
Template
In template mode you create a LaTeX template file. In general you can't compile the file with LaTeX directly, you have to process it with gummibaum first.
You write template files as defined by the Go template package. Detailed instructions on how to use this package can be found in the Go documentation here. Important note: In LaTeX it often happens that the go standard delimiters ({{
and }}
) are used for LaTeX stuff. Thus, gummibaum changed these delimiters. Instead of {{ .FOO}}
you have to use #(
and #)
like this #(.FOO#)
. This is not yet configurable, but I hope to add that soon. #
is usually only used as a macro placeholder and never in this combination (I hope).
Here is a small example demonstrating how to use the package, with both constant values and a data set imported via an csv file.
Save following file to template.tmpl.tex:
\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\title{Template File}
\begin{document}
\maketitle
Hello #(latex .REPLNAME#),
This is just a file demonstrating you how to use the gummibaum template system.
Here is some verbatim text: #(verb "|" "foo" "bar" 42#).
You can also use it to iterate over data from a csv file.
% The minus suppresses new line at the end of the template instruction
#(range .data.Columns -#)
Token is #(latex .Map.token#) and value is #(latex .Map.value#)\\
#(end#)
You can also iterate over each entry in head.
\begin{tabular}{ll}
#(if .data.Head#)
#(- join " & " .data.Head#)\\
#(end -#)
\end{tabular}
Here's another example using the join function #(join "," "foo" "bar"#)
Here's an example to get an entry by index, get first column and get entry on position 0:
#(with $col := (index .data.Columns 0) -#)
#(- index $col.Entries 0#)
#( end -#)
\end{document}
Then create const.json with
{
"REPLNAME": "John"
}
and data.csv with
token,value
A,21
B,42
C,84
Then execut the template with
./gummibaum template --const-file const.json --const REPLFOO=bar --csv data.csv template.tmpl.tex
The output is
\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\title{Template File}
\begin{document}
\maketitle
Hello John,
This is just a file demonstrating you how to use the gummibaum template system.
Here is some verbatim text: \verb|foo bar 42|.
Here is another constant bar
You can also use it to iterate over data from a csv file.
% The minus suppresses new line at the end of the template instruction
Token is A and value is 21\\
Token is B and value is 42\\
Token is C and value is 84\\
You can also iterate over each entry in head.
\begin{tabular}{ll}
token & value\\
\end{tabular}
Here's another example using the join function foo,bar
Here's an example to get an entry by index, get first column and get entry on position 0:
A
\end{document}
Here is an explanation of what happens: --const "repl=value" defines a constant with name repl and specified value. Thus REPLFOO=bar creates a constant value REPLFOO with value bar. Constants can also be loaded from a file with --const-file const.json. In the example above the file contains the assignment REPLNAME=John. --csv data.csv loads the file data.csv and makes it available under the name data. You can use multiple --const, --const-file and --csv directives.
In the template we can then use #(.REPLFOO#)
to get the value of the constant REPLFOO. With #(latex .REPLFOO#)
LaTeX special characters are escaped.
Each file NAME.csv is available in the template as #(.NAME#)
. For details about such data collections see below.
csv files must have a head line containing names for each row. Data from a csv file can then be accessed in the following way:
- .Head is an array containing all entries from the head
-
.Columns Contains all data from the file in the following way
-
. Columns.Head contains again the head of the csv file, this is useful in some situations. Example from template:
#(- join " & " .data.Head#)
. Creates all entries in head separated by " & ". Values will be escaped. -
.Columns.Entries is an array containing all data in that column in the order as parsed from the csv file. Example from template:
#(- latex (index $col.Entries 0)#)
. Gets entry on position 0 and escapes it. -
.Columns.Map maps the row name as defined in the head to the value in the column. Example from template:
#(latex .Map.value#)
. From the current column (range moves dot to the columns) lookup the row "value".
-
. Columns.Head contains again the head of the csv file, this is useful in some situations. Example from template:
Use ./gummibaum template --help
to show all arguments. If you want to write to a file instead of standard out use --out file.
Also more than one template file can be used, templates can be included into each other etc. See Go templage package documentation for details.
In addition to the functions from the template package the following functions may be used:
-
latex arg1 ... argn
Escape special LaTeX characters in an arbitrary number of arguments -
join sep arg1 ... argn
Join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string. Special LaTeX characters are replaced in each arg. -
verb del arg1 ... argn
: Creates a LaTeX verb environment where del is used to specify the beginning and ending character of the \verb command. For example#(verb "|" "foo" "bar" 42#)
creates\verb|foo bar 42|
.