Ask Your Question

invoke external program

asked 2012-08-10 16:10:26 +0200

Mathemage gravatar image

updated 2012-08-12 12:18:06 +0200

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
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, loaded.
* [blinking cursor here]

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:

LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, loaded.
! 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 flag offensive close merge delete


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

John Palmieri gravatar imageJohn Palmieri ( 2012-08-12 12:53:40 +0200 )edit

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

Mathemage gravatar imageMathemage ( 2012-08-12 14:01:48 +0200 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2012-08-11 00:56:21 +0200

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

sage: import subprocess
sage: ret =['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------+ 15 palmieri  palmieri   510 Aug 10 13:17 Downloads
drwx------@ 74 palmieri  palmieri  2516 Aug  9 15:28 Library
sage: ret

The first argument to 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['pdflatex', 'foo.tex'])

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

edit flag offensive delete link more

answered 2012-08-11 12:51:14 +0200

Volker Braun gravatar image

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.

edit flag offensive delete link more


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

kcrisman gravatar imagekcrisman ( 2012-08-12 00:15:12 +0200 )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.

Volker Braun gravatar imageVolker Braun ( 2012-08-12 02:35:50 +0200 )edit

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

John Palmieri gravatar imageJohn Palmieri ( 2012-08-12 11:44:13 +0200 )edit

answered 2012-08-12 14:07:08 +0200

Mathemage gravatar image

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


I typed


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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-08-10 16:10:26 +0200

Seen: 9,926 times

Last updated: Aug 12 '12