-1

At the end of this script, I would like to add a few lines which would :

  • compile myfile.tex, in order to update the modifications made by the previous script ;
  • (produce and) eventually display myfile.pdf, which is the pdf generated by myfile.tex.

Note that I don't want TexMaker to open the LaTeX file during compilation. I had this issue when I tried a short solution using the os module and that I found on a forum ; that's why I'm searching a substitute. Moreover, I don't know how to use commands (outside of the python shell) like in this solution.

The main goal remains simplicity since the entire script (not only the few lines to add) should be readable and understandable by beginners-intermediate students, including me ;) I hope there is such a solution. If you have any idea, feel free to volunter your answer so I can test it !

EDIT : The document does not contain any reference, picture, graphs... or other complex elements. I tried to use the command pdflatex <name>.tex as suggested in the commentaries, but the PDF wasn't produced nor updated.

  • 1
    Welcome to TeX.SX! If I understand well, you have a Python script that generates a .tex file that you want to compile? Have you tried just calling the compiler from your script with pdflatex <name of your file>.tex for example? Or used latexmk if the compilation scheme is a bit more complex? I don't understand your remark about command directories. Indeed, if you want to compile a file without an editor as TeXMaker to boot, you will have to call the compiler in command line. – KersouMan Oct 19 '20 at 15:49
  • I don't know that much Python, but it seems to me that you will have to do something along the lines of os.system('pdflatex <relative path to your file>/<name of your file>.tex') and run this command two times if you want to correctly update potential references to labels that you might have in your document. Or switch directly to os.system('latexmk -pdf <relative path to your file>/<name of your file>.tex') if your document is more complex. – KersouMan Oct 19 '20 at 15:55
  • What operating system are you using? Why not use TeXMaker to compile the TeX file made by the Python script? "At the end of this script": what is "this script"? – Teepeemm Oct 20 '20 at 14:37

1 Answers1

4
# works with Python3
import os
import platform
import subprocess

# TeX source filename
tex_filename = 'my_doc.tex'
filename, ext = os.path.splitext(tex_filename)
# the corresponding PDF filename
pdf_filename = filename + '.pdf'

# compile TeX file
subprocess.run(['pdflatex', '-interaction=nonstopmode', tex_filename])

# check if PDF is successfully generated
if not os.path.exists(pdf_filename):
    raise RuntimeError('PDF output not found')

# open PDF with platform-specific command
if platform.system().lower() == 'darwin':
    subprocess.run(['open', pdf_filename])
elif platform.system().lower() == 'windows':
    os.startfile(pdf_filename)
elif platform.system().lower() == 'linux':
    subprocess.run(['xdg-open', pdf_filename])
else:
    raise RuntimeError('Unknown operating system "{}"'.format(platform.system()))
Alan Xiang
  • 5,227
  • Thanks you very much for your answer. However, it does not work since the produced PDF is not updated (which is easy to see, its content remains the same while it is randomly created by the python script I didn't detailed). Anyway, thanks for trying, that's really nice for new people on the forum. – Pitchoune Oct 20 '20 at 13:58
  • 2
    That shouldn't happen. Are you sure the TeX file generated by your script is valid? Maybe run this script in a terminal and see what pdflatex's stdout looks like. If you are using Adobe Acrobat, it is important to close the document before recompile because Acrobat locks the PDF file. – Alan Xiang Oct 20 '20 at 14:08