使用 Pweave 的表格

使用 Pweave 的表格

表格允许生成适当的 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()方法需要booktabsLaTeX 包,因此您需要在模板\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}

答案4

到乳胶在 Pandas 和桌子在 PyLaTeX 中实现动态 LaTeX 表格是很好的方法。感谢 @Marijn 和 @alan-xiang 为我指明了这些方向。动态单元格背景颜色在单独的问题中讨论。

相关内容