Ask Your Question
0

invoke external program

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

Mathemage gravatar image

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

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
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:

(/tmp/foo.tex
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

Comments

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 +0100 )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 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
0

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

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------+ 15 palmieri  palmieri   510 Aug 10 13:17 Downloads
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.

edit flag offensive delete link more
0

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

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

Comments

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 +0100 )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 +0100 )edit
1

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 +0100 )edit
0

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

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

file_handler.close()

I typed

file_handler.close

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

Stats

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

Seen: 10,014 times

Last updated: Aug 12 '12