TransWikia.com

Tables with Pweave

TeX - LaTeX Asked by lars20070 on December 12, 2020

xtables allows the generation of proper LaTeX tables with Sweave. Is there an equivalent package for Pweave? What’s the best way to add tables to dynamic LaTeX/Python reports?

%
% virtualenv --system-site-packages -p python3 ./venv
% source ./venv/bin/activate
% pip install --upgrade pweave
% pip install --upgrade pandas
% pweave -f tex pweavetest.texw
% pdflatex pweavetest.tex
%

documentclass[a4paper]{article}
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 texttt{tabular}  LaTeX environment? I would like to set the table cell colour of texttt{age} with the innformation in texttt{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'])

table
@

end{document}

4 Answers

The code below combines age and age_colour into a single column which is then processed by the formatter function f2.

documentclass[a4paper]{article}
usepackage{booktabs}
usepackage{colortbl}

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 information 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']}

# combine 'age' and 'colour' in a single column (then pass to formatter function f2)
table = pd.DataFrame(pupils, columns = ['name', 'age', 'age_colour'])
table['age'] = table['age'].astype(str) + '_' + table['age_colour'].astype(str)
table = table[['name', 'age']]
@

<<echo=False, results='tex'>>=
def f1(x):
  return str(x)

def f2(x):
  # split argument into 'age' and 'age_colour'
  split = x.split('_')
  age = split[0]
  colour = split[1]
  return 'cellcolor{'+str(colour)+'}{'+str(age)+'}'

print(table.to_latex(index=False, column_format='|l|c|', formatters=[f1,f2], escape=False))
@

end{document}

enter image description here

Correct answer by lars20070 on December 12, 2020

The pandas Python library has a to_latex() function to convert dataframes into LaTeX tables. You can use that in Pweave by setting a block to results='tex'. The exact string 'tex' is arbitrary, anything other than verbatim or hidden means raw text, which is the intended format here (see http://mpastell.com/pweave/chunks.html#envvar-results='verbatim').

The to_latex() method requires the booktabs LaTeX package so you need to add a usepackage statement for that to your .texw template.

MWE:

documentclass[a4paper]{article}
usepackage{booktabs} % this package added
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 texttt{tabular}  LaTeX environment? I would like to set the table cell colour of texttt{age} with the information in texttt{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'>>=
print(table.to_latex())
@

end{document}

Result (.tex):

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 texttt{tabular}  LaTeX environment? I would like to set the table cell colour of texttt{age} with the information in texttt{age_colour}.

begin{tabular}{llr}
toprule
{} &      name &  age 
midrule
0 &      Jeff &   26 
1 &      Lisa &   34 
2 &       Sam &    6 
3 &  Victoria &   68 
bottomrule
end{tabular}

end{document}

Result (pdf):

enter image description here

Answered by Marijn on December 12, 2020

The Pandas command to_latex does not allow to return LaTeX instructions. Guess it would be best to extend to_latex with a new cellcolor option.

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):
  % TODO: Not possible to return LaTeX instructions. Backslashes returned as textbackslash.
  return 'cellcolor{'red'}{'+str(x)+'}'

print(table.to_latex(index=False, column_format='|l|c|', formatters=[f1,f2]))
@

end{document}

Answered by lars20070 on December 12, 2020

to_latex in Pandas and table in PyLaTeX are good ways to implement dynamic LaTeX tables. Thanks to @Marijn and @alan-xiang for pointing me in these directions. Dynamic cell background colours are discussed in a separate question.

Answered by lars20070 on December 12, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP