# invoke external program

What is the proper way to call external applications in my programs? Specifically, pdfLaTeX compiler. When I invoke os.system("pdflatex foo.tex") in Sage's CLI console, the command executes as expected. However, when I embedded this into my code and run this code, TeX asks for another input:

This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
(/tmp/foo.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
)


Am I missing something or why doesn't this work?

P. S. My original post may have been confusing. When I wrote:

However, when I embedded this into my code and run this code,

I meant embedding into "my_code.sage" source code, which is consequently attached and run in Sage's CLI.

P. P. S. When use it in my "foo.sage" code as below:

os.system("pdflatex -interaction=batchmode foo.tex")


following log is produced:

(/tmp/foo.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
)
! Emergency stop.
<*> /tmp/foo.tex

*** (job aborted, no legal \end found)


However, written explicitly in Sage's console and even in bash console, everything compiles correctly. Does this mean something? Just FYI, I did end my source file with \end{document}

edit retag close merge delete

Your file "foo.tex" is in /tmp, right? That's where pdflatex is looking for it.

( 2012-08-12 05:53:40 -0500 )edit

Yes, right, it is. I've already found out the cause of problem - I'm gonna write the solution in a few minutes...

( 2012-08-12 07:01:48 -0500 )edit

Sort by » oldest newest most voted

I apologize to everyone for my inconvenience. The heart of problem was my making of a rookie mistake. When creating TeX file, I didn't close the file properly. Namely, instead of

file_handler.close()


I typed

file_handler.close


Without brackets, the final \end{document} did not take effect which caused the missing '\end' problem.

more

Your question is off-topic, next time you have a non-Sage question ask it on stackexchange, for example. I take it you are not in the correct directory when calling os.system. You should instruct latex to not stop for errors if you call it programmatically, for example pdflatex -interaction=batchmode. Read the man page for more information.

more

Of course, the original post *did* say that it was from within the Sage command line that this was the question.

( 2012-08-11 17:15:12 -0500 )edit

The original post said that it works in Sage, it just doesn't work in his own code. So its not a Sage question.

( 2012-08-11 19:35:50 -0500 )edit
1

I interpreted the question to mean that it worked from the command line but not from "foo.sage".

( 2012-08-12 04:44:13 -0500 )edit

Python's subprocess module is a good tool for this.

sage: import subprocess
sage: ret = subprocess.call(['ls', '-al'])
drwxr-xr-x  17 palmieri  palmieri   578 May  5 09:02 Applications
drwx------+ 13 palmieri  palmieri   442 Aug 10 13:20 Desktop
drwx------+ 27 palmieri  palmieri   918 Jun  1 08:57 Documents
drwx------@ 74 palmieri  palmieri  2516 Aug  9 15:28 Library
sage: ret
0


The first argument to subprocess.call is a list: the first entry is the command, and the remaining entries are its arguments. It returns the return code. You should be able to put this into your code:

import subprocess
subprocess.call(['pdflatex', 'foo.tex'])


You could also use other functions from the subprocess module if you want other capabilities.

more