将自定义乳胶脚本导入 pylatex 文档

Import custom latex script into pylatex document

我正在尝试使用 PyLaTeX 生成 .pdf 文件。我看到 PyLaTeX 有一个预定义的语法来生成 LaTeX 文档然后导出它们,但我只想加载一个我已经构建的 LaTeX 文件,而不是通过 PyLaTeX 语法重新创建它。

我现在尝试使用的代码如下,即使一切正常,我也得到了文档的 "raw" 代码:

from pylatex import Document, Section, Subsection, Command
from pylatex.utils import italic, NoEscape

latex_document = 'path'
with open(latex_document) as file:
    tex= file.read()

doc = Document('basic')
doc.append(tex)
doc.generate_pdf(clean_tex=False)

你需要用 NoEscape 包裹 tex,这样 PyLaTeX 就会按字面解释字符串内容。

如果文件path的内容是

\begin{equation}
  \hat{H}\Psi = E\Psi
\end{equation}

然后 doc.append(tex) 创建

\begin{document}%
\normalsize%
\textbackslash{}begin\{equation\}\newline%
  \textbackslash{}hat\{H\}\textbackslash{}Psi = E\textbackslash{}Psi\newline%
\textbackslash{}end\{equation\}\newline%
%
\end{document}

doc.append(NoEscape(tex))创造了

\begin{document}%
\normalsize%
\begin{equation}
  \hat{H}\Psi = E\Psi
\end{equation}
%
\end{document}