将表格数据发送到 Latex

将表格数据发送到 Latex

我需要将数据从 Python 发送到 Latex 代码。

数据是表格数据,因此在 Latex 中循环这些数据会很困难。所以,我认为用 Python 创建一个 latex 代码并将其添加到那里可以解决这个问题。

我可以用它来创建表格数据,但除了数据之外,它还会添加一个 amp;

row_data = '''\hline'''+1+'&'+"data1" +'&' + "data2"+'''\\'''+'''\\'''

extra amp; visible

转义序列“\”使“&”不可见,并且所有数据都出现在第一行。

row_data = '''\hline'''+1 +'\&' + "data1" +"\&" +"data2"+'''\\'''+'''\\'''

all data in 1 column

那么,如何删除这个多余的“amp;”?

最后传递给乳胶代码的数据是:

row_data = \hline1&data1&0:01:45\\\hline2&data2&0:01:40\\

我将数据以如下方式发送到 Latex:

s = Template('my_pdf_tex.tex')
pdf_data ={}
pdf_data['my_data'] = row_data
texfile, texfilename = mkstemp(dir=tmp_folder)
os.write(texfile, render_to_string(s.template,pdf_data))

我在 LateX 中接收的数据如下:

\begin{center}
            \begin{tabular}{|l|l|r|}\hline
            \textbf{No.} & \textbf{Topic Name} & \textbf{Time}\\
            {{my_data}}
            \hline
            & \textbf{Grand Total} & \textbf{ {{total}} }\\
            \hline

        \end{tabular}

    \end{center}

Expected Output

答案1

如果我可以建议对您的方法进行一些修改。在这里,我将展示一个适用于您的数据/变量的版本和几个不适用的版本 - 以供比较。(我通过将每个表行直接定义为变量而不是从文件中读取来简化工作流程)。

\documentclass[11pt]{article}

\usepackage{graphicx}
\usepackage{pythontex}

\begin{pycode}
import sys
row_data = '\\hline1&data1&0:01:45\\\\\n'
row_data = row_data + '\\hline2&data2&0:01:40\\\\\n'
\end{pycode}

\begin{document}

\begin{center}

% Does work!
\pyc{tabular_str = ""}
\pyc{tabular_str = tabular_str + "\\begin{tabular}{|l|l|r|}\n"}%
\pyc{tabular_str = tabular_str + "\\hline \n"}%
\pyc{tabular_str = tabular_str + "\\textbf{No.} & \\textbf{Topic Name} & \\textbf{Time}\\\\\n"}%
\pyc{tabular_str = tabular_str + row_data}%
\pyc{tabular_str = tabular_str + "\\hline \n"}%
\pyc{tabular_str = tabular_str + "& \\textbf{Grand Total} & \\textbf{ total }\\\\\n"}%
\pyc{tabular_str = tabular_str + "\\hline \n"}%
\pyc{tabular_str = tabular_str + "\\end{tabular}\n"}%
\pyc{print(tabular_str, file=sys.stdout, flush=True)}%

\vskip 2em

% Doesn't work
\begin{tabular}{|l|l|r|}
    \hline
    \textbf{No.} & \textbf{Topic Name} & \textbf{Time}\\
    \pyc{print(row_data, file=sys.stdout, flush=True)}%
    \hline
    & \textbf{Grand Total} & \textbf{ total }\\
    \hline
\end{tabular}

\vskip 2em

% Also doesn't work
\pyc{print("\\begin{tabular}{|l|l|r|}\n", file=sys.stdout, flush=True)}%
\pyc{print("\\hline \n", file=sys.stdout, flush=True)}%
\pyc{print("\\textbf{No.} & \\textbf{Topic Name} & \\textbf{Time}\\\\\n", file=sys.stdout, flush=True)}%
\pyc{print(row_data, file=sys.stdout, flush=True)}%
\pyc{print("\\hline \n", file=sys.stdout, flush=True)}%
\pyc{print("& \\textbf{Grand Total} & \\textbf{ total }\\\\\n", file=sys.stdout, flush=True)}%
\pyc{print("\\hline \n", file=sys.stdout, flush=True)}%
\pyc{print("\\end{tabular}\n", file=sys.stdout, flush=True)}%

\end{center}

\end{document}

Comparison of printing out a table with PythonTeX

您会立即注意到这三个版本的显示不同。上面的一个给出了您想要的结果,而其他两个则存在显示问题。但是,您无法从图像中看到后两个表格中出现的错误消息(此错误针对中间表格,最后一个表格也会出现类似的错误):

(pythontex-files-48/48.pytxmcr) (pythontex-files-48/48.pytxpyg)
(pythontex-files-48/py_default_default_10.stdout)
(pythontex-files-48/py_default_default_11.stdout
pythontex-files-48/py_default_default_11.stdout:1: Misplaced \noalign
pythontex-files-48/py_default_default_11.stdout:1: You can't use `\hrule' here 
except with leaders
pythontex-files-48/py_default_default_11.stdout:1: Missing number, treated as z
ero
pythontex-files-48/py_default_default_11.stdout:1: Illegal unit of measure (pt 
inserted)
)
48.tex:37: Misplaced \noalign
48.tex:37: You can't use `\hrule' here except with leaders
48.tex:37: Missing number, treated as zero
48.tex:37: Illegal unit of measure (pt inserted)
(pythontex-files-48/py_default_default_12.stdout)
(pythontex-files-48/py_default_default_13.stdout

我会完全诚实地告诉你……我不确定哪个字符打印错误。但看起来在表格环境中间打印会导致紧接在前后的某些字符与(可能还有其他环境)\hline不兼容tabular。我也不确定这实际上是编码问题(在 Python 和 LaTeX 之间?)还是只是插入了一个特殊/保留字符。

解决这个问题的方法是通过 PythonTeX 打印整个表格。我意识到这很烦人。按照预期查看 LaTeX 表格并仅打印生成的行很方便。但是,在玩了您的示例后,我找不到其他正常工作的方法。

最后要说的是,我无法重现&您遇到的确切的 & 符号问题。但我认为这与此问题有关。如果我建议的修复方法(tabular在打印之前将其完全构建为字符串)不起作用,那么我要求您在帖子中包含一个完整的 MWE 示例,包括几行实际数据(来自输入文件的数据)。

相关内容