表格允许生成适当的 LaTeX 表格斯维夫是否有等效的软件包编织? 向动态 LaTeX/Python 报告添加表格的最佳方法是什么?
%
% 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}
答案1
下面的代码将age
和合并age_colour
为一列,然后由formatter
函数进行处理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}
答案2
Python库pandas
具有to_latex()
将数据框转换为 LaTeX 表的函数。您可以在 Pweave 中通过将块设置为 来使用该函数results='tex'
。确切的字符串'tex'
是任意的,除verbatim
或之外的任何字符串hidden
都表示原始文本,这是此处的预期格式(请参阅http://mpastell.com/pweave/chunks.html#envvar-results='verbatim')。
该to_latex()
方法需要booktabs
LaTeX 包,因此您需要在模板\usepackage
中添加一条相关语句.texw
。
梅威瑟:
\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}
结果(.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}
结果(pdf):
答案3
Pandas 命令to_latex
不允许返回 LaTeX 指令。我想最好扩展一下to_latex
并有一个新的cellcolor
选择。
\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}