Reputation: 189
I have a Python program that generates plaintext LaTeX markup suitable for typesetting with MiKTeX. How can I make the process of typesetting the source file part of the Python program? So far the only thing I figured out was that subprocess.call(['C:\\Program Files\\MikTeX\\miktex\\bin\\miktex-texworks.exe'])
opens the typesetting interface. At the end of the day I want to have a Python program which essentially outputs a pdf (I need LaTeX for equations.)
Upvotes: 1
Views: 3976
Reputation: 64068
You want to call the latex
program, providing the filename and desired output filename:
import subprocess
def create_pdf(input_filename, output_filename):
process = subprocess.Popen([
'latex', # Or maybe 'C:\\Program Files\\MikTex\\miktex\\bin\\latex.exe
'-output-format=pdf',
'-job-name=' + output_filename,
input_filename])
process.wait()
So, if I did create_pdf("my_input_file.tex", "my_output_file.pdf")
, this will cause Windows to run the following command on the command line:
latex -output-format=pdf -job-name=my_output_file.pdf my_input_file.tex
...then wait for LaTeX to finish before returning from the function. Note that we're not calling texworks.exe
, which is essentially a text editor -- we're directly calling latex.exe
, the actual program which converts tex files.
As long as input_file.tex
exists within the same folder as the Python script, you should get the output pdf. If you don't want to make that extra text file, you may be able to pipe the generated string into the latex program, though you'd have to do more research on how both latex and subprocess works to see if that's feasible.
Upvotes: 4