0

I'm trying to build a LaTeX document using Python but am having problems getting the commands to run in sequence. For those familiar with LaTeX, you'll know that you usually have to run four commands, each completing before running the next, e.g.

pdflatex file
bibtex file
pdflatex file
pdflatex file

In Python, I'm therefore doing this to define the commands

commands = ['pdflatex','bibtex','pdflatex','pdflatex']
commands = [(element + ' ' + src_file) for element in commands]

but the problem is then running them.

I've tried to suss things out from this thread – e.g. using os.system() in a loop, subprocess stuff like map(call, commands) or Popen, and collapsing the list to a single string separated by & – but it seems like the commands all run as separate processes, without waiting for the previous one to complete.

For the record, I'm on Windows but would like a cross-platform solution.

EDIT
The problem was a bug in speciyfing the src_file variable; it's shouldn't have a ".tex". The following code now works:

test.py

import subprocess

commands = ['pdflatex','bibtex','pdflatex','pdflatex']

for command in commands:
    subprocess.call((command, 'test'))

test.tex

\documentclass{article}
\usepackage{natbib}

\begin{document}
This is a test \citep{Body2000}.
\bibliographystyle{plainnat}
\bibliography{refs}
\end{document}

refs.bib

@book{Body2000,
  author={N.E. Body},
  title={Introductory Widgets},
  publisher={Widgets International},
  year={2000}
}
Community
  • 1
  • 1
jkeirstead
  • 2,881
  • 3
  • 23
  • 26
  • 1
    If you use the [accepted answer from that thread](http://stackoverflow.com/questions/359347/execute-commands-sequentially-in-python/359506#359506) that uses [Popen.wait()](http://docs.python.org/library/subprocess.html?highlight=subprocess#subprocess.Popen.wait), that should work. – crashmstr Sep 09 '11 at 17:46
  • I thought that looked promising but to be honest, I don't really follow the code example. 2 classes seems like a lot of code for a simple task. – jkeirstead Sep 09 '11 at 17:53
  • The [answer from utdemir](http://stackoverflow.com/questions/7365319/run-commands-sequential-from-python/7365350#7365350) is simpler. but call is pretty much equivalent to Popen(...) then calling wait() on the returned object. – crashmstr Sep 09 '11 at 17:56

1 Answers1

4

os.system shouldn't cause this, but subprocess.Popen should.

But I think using subprocess.call is the best choice:

commands = ['pdflatex','bibtex','pdflatex','pdflatex']

for command in commands:
    subprocess.call((command, src_file)) 
crashmstr
  • 28,043
  • 9
  • 61
  • 79
utdemir
  • 26,532
  • 10
  • 62
  • 81
  • Nope - that doesn't run them sequentially. The LaTeX document still has missing cross-references. (I've checked the source files with a manual build.) – jkeirstead Sep 09 '11 at 17:57
  • Right you are! Having the `tex` extension on the src_file string breaks the bibtex compilation. I've added the change above. – jkeirstead Sep 09 '11 at 18:13