I am trying to get into report automation with LaTeX and Jinja2 with a python script. I managed to fill a simple table with some data in a .txt file.
I would like to use variables names inside my data file containing the character "/". However python interprets the /. I tried to use filters taken from a few websites but I cannot manage to escape it.
My key is the following : <RF/Freq>
. I am asking Jinja to spot terms between '<' and '>', and I have the same in the LaTeX template.
This is what my filter is at the moment :
LATEX_SUBS = (
(re.compile(r'\\'), r'\\textbackslash'),
(re.compile(r'([{}_#%&$])'), r'\\\1'),
(re.compile(r'~'), r'\~{}'),
(re.compile(r'\^'), r'\^{}'),
(re.compile(r'"'), r"''"),
(re.compile(r'\.\.\.+'), r'\\ldots'),
(re.compile(r'/'), r'\/')
)
def escape_tex(value):
newval = value
for pattern, replacement in LATEX_SUBS:
newval = pattern.sub(replacement, newval)
return newval
But Jinja returns :
jinja2.exceptions.UndefinedError: 'RF' is undefined
For info, my latex template contains :
\documentclass[12pt,a4paper]{article}
\begin{document}
\begin{tabular}{c|c}
Test & Result \\
\hline
Frequency & <RF\Freq | escape_tex>
\end{tabular}
\end{document}
I tried multiple solutions found on SE or other websites, but without success.