I am a newbie, exploring the template engine Jinja2 to help me to typeset a couple of LaTeX documents using Python. In my initial explorations, I am simply trying to populate a table using the following code in Python:
latex_jinja_env = jinja2.Environment(
block_start_string = '\BLOCK{',
block_end_string = '}',
variable_start_string = '\VAR{',
variable_end_string = '}',
comment_start_string = '\#{',
comment_end_string = '}',
line_statement_prefix = '%%',
line_comment_prefix = '%#',
trim_blocks = True,
autoescape = False,
loader = jinja2.FileSystemLoader(os.path.abspath('.'))
)
header = ['Num', 'Date', 'Ticker']
data = [[1, 2, 3], [4, 'STR', 'Test'], [5, 6, 'Ticker']]
template = latex_jinja_env.get_template('template.tex')
print(template.render(section1='Test Table', header = header, data = data))
The LaTeX-template is constructed as follows:
\documentclass[12pt,a4paper]{article}
\begin{document}
\section{\VAR{section1}}
\BLOCK{set colsep = joiner("&")}
\begin{tabular}{ ccc }
\hline
\BLOCK{for col in header} \VAR{colsep()} \textbf{\VAR{col}} \BLOCK{endfor} \\
\hline
\hline
\BLOCK{for row in data} \BLOCK{for col in row} \VAR{colsep()} \VAR{col} \BLOCK{endfor} \\
\BLOCK{endfor}
\hline
\end{tabular}
\end{document}
Whilst the header is generated as wanted, concerning the data an empty column appears to be prepended to the data:
bash-3.2$ ./rep.py
\documentclass[12pt,a4paper]{article}
\begin{document}
\section{Test Table}
\begin{tabular}{ ccc }
\hline
\textbf{Num} & \textbf{Date} & \textbf{Ticker} \\
\hline
\hline
& 1 & 2 & 3 \\
& 4 & STR & Test \\
& 5 & 6 & Ticker \\
\hline
\end{tabular}
\end{document}
bash-3.2$
What is the appropriate manner fill the template table with data? Thanks in advance for helping out this newbie.