2017-06-01 60 views
2

我想使用PyLaTeX生成.pdf文件。我看到PyLaTeX有一個預定義的語法來生成LaTeX文檔,然後導出它們,但是我想簡單地加載一個已經構建好的LaTeX文件,而不是通過PyLaTeX語法重新創建它。導入自定義乳膠腳本到pylatex文件

我想現在使用的代碼如下,即使一切正常,我得到的文件「原始」代碼:

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) 

回答

0

你需要用texNoEscape,使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}