I am using LaTeX, Python and Pweave for the generation of dynamic reports. Panda to_latex
and its option formatters
allow me to include tables. But how could I set the table cell background color i.e. set cellcolor
dynamically?
\documentclass[a4paper]{article}
\usepackage{booktabs}
\begin{document}
\section*{Tables with LaTeX and Pweave}
The table below list the names and ages of pupils. How can I dynamically generate LaTeX tables i.e. using the {\tt tabular} LaTeX environment? I would like to set the table cell colour of {\tt age} with the innformation in {\tt age\_colour}.\\ \\
<<echo=False>>=
import pandas as pd
pupils = {'name':['Jeff','Lisa','Sam','Victoria'],'age':[26,34,6,68],'age_colour':['white','white','red','white']}
table = pd.DataFrame(pupils, columns = ['name', 'age'])
@
<<echo=False, results='tex'>>=
def f1(x):
return str(x)
def f2(x):
return '\cellcolor{\'red\'}{'+str(x)+'}'
print(table.to_latex(index=False, column_format='|l|c|', formatters=[f1,f2]))
@
\cellcolor
command in the cell yourself? It's not terribly hard to do.