简体   繁体   English

通过python编译Latex文档

[英]Compile Latex document by python

I have been struggling with compiling latex document and creating pdf. 我一直在努力编译乳胶文档和创建pdf。

I have successfully created .tex file by python. 我已经通过python成功创建了.tex文件。 I want to make it even more automated by compiling .tex in python. 我想通过在python中编译.tex使其更加自动化。

The code I have tried is: 我尝试过的代码是:

import os 
os.system("filename.tex")

I put this codes at the end of my python code. 我将此代码放在python代码的末尾。

But it only opens the software Texmaker, not compiling it to pdf. 但是它仅打开软件Texmaker,而不将其编译为pdf。

If you could give me any hint I will be so grateful!! 如果您能给我任何提示,我将非常感激!

You need to actually run some kind of command that makes the pdf. 您实际上需要运行某种生成pdf的命令。 In my case I might use pdflatex , although you should know that compiling LaTeX isn't always as easy as running latex once. 就我而言,我可能会使用pdflatex ,尽管您应该知道编译LaTeX并不总是像运行一次乳胶一样容易。

import subprocess

with open("filename.tex", "w") as tex_file:
    tex_file.write(r"""
\documentclass{article}

\begin{document}
Hello World
\end{document}
""")

subprocess.call(["pdflatex", "filename.tex"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM