I am able to use python script to create a LaTeX file, but I want to take that file and compile it, so it creates a pdf by using a python script. I have seen some things using os and subprocess but I really don't understand it.
Asked
Active
Viewed 2.5k times
11
-
2related: [Calling an external command in Python](http://stackoverflow.com/q/89228/4279) – jfs Oct 19 '15 at 21:47
1 Answers
18
Try this out.
import os
os.system("pdflatex mylatex.tex")

Riyaz
- 1,430
- 2
- 17
- 27
-
Thank you! This definitely works, but do you know of a way to save it to a specific location by any chance? – Alex Oct 19 '15 at 06:50
-
2You can use ``os.system("mv mylatex.pdf path/to/directory")`` to move the pdf to any specific location. – Riyaz Oct 19 '15 at 06:52
-
-
11@Alex: no need to use `os.system()` that runs the shell here. Use `subprocess.check_call(['pdflatex', 'mylatex.tex'])` instead. To save the result in a specific location: pass the appropriate command-line argument to `pdflatex` or use `shutil.move()` -- again, no need to use `os.system()` here. – jfs Oct 19 '15 at 21:46
-
1
-
@jfs, why are you adverse to using `os.system()`? I've got no experience in either, and am trying to decide which to use. Thanks! – David Collins Oct 22 '20 at 00:53
-
1@DavidCollins: it is not just my preference, the docs says so explicitly: 'The `subprocess` module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." https://docs.python.org/3/library/os.html#os.system For example, 1- you don't need to worry about escaping shell meta-characters (even if you know what shell will be used) 2- `check_call()` raises exception if the command returns non-zero status: errors should not go silently by default. – jfs Oct 22 '20 at 15:47