2011-09-09 7 views
0

Python을 사용하여 LaTeX 문서를 작성하려고하지만 명령을 순서대로 실행하는 데 문제가 있습니다. LaTeX에 익숙한 사람들은 일반적으로 다음 명령을 실행하기 전에 완료해야하는 4 개의 명령을 실행해야한다는 것을 알게 될 것입니다. 파이썬에서 Python에서 순차적으로 명령을 실행하십시오.

pdflatex file 
bibtex file 
pdflatex file 
pdflatex file 

, 나는 그러므로 명령

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

를 정의하려면이 일을 해요하지만 문제는이를 실행 중입니다.

나는 this thread –에서 물건을 꺼내려고했습니다. 루프에서 os.system()을 사용하면 map(call, commands) 또는 Popen과 같은 문자열로 목록이 축소되고 & –으로 구분 된 단일 문자열로 목록이 축소되지만 이전 명령이 완료 될 때까지 기다리지 않고 명령이 모두 별도의 프로세스로 실행되는 것처럼 보입니다.

레코드의 경우 Windows에 있지만 크로스 플랫폼 솔루션을 원합니다.

EDIT
문제는 src_file 변수 speciyfing 버그이었다; 그것은 ".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} 
} 
+1

당신이 [그 스레드에서 허용 대답을 (사용하는 경우 http://stackoverflow.com/questions/359347/execute-commands-sequentially- :

하지만 subprocess.call를 사용하여 생각은 최선의 선택 in-python/359506 # 359506)가 작동해야하는 [Popen.wait()] (http://docs.python.org/library/subprocess.html?highlight=subprocess#subprocess.Popen.wait)을 사용합니다. – crashmstr

+0

나는 그것이 유망 해 보였다고 생각했지만 솔직히 말해서 코드 예제를 실제로 따르지는 않습니다. 2 개의 클래스는 간단한 작업을위한 많은 코드처럼 보입니다. – jkeirstead

+0

[answer from utdemir] (http://stackoverflow.com/questions/7365319/run-commands-sequential-from-python/7365350#7365350)이 더 간단합니다. 그러나 호출은 Popen (...)과 거의 비슷하게 반환 된 객체에서 wait()를 호출합니다. – crashmstr

답변

5

os.system이 발생하지해야

test.py

import subprocess 

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

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

test.tex 그러나, 그러나 subprocess.Popen해야 다음 코드는 이제 작동 .

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

for command in commands: 
    subprocess.call((command, src_file)) 
+0

아니요 - 순차적으로 실행하지 않습니다. LaTeX 문서에는 여전히 상호 참조가 없습니다. (수동 빌드로 소스 파일을 확인했습니다.) – jkeirstead

+0

. 코드에 또 다른 문제가 있어야합니다. – utdemir

+0

맞습니다! src_file 문자열에'tex' 확장자가 있으면 bibtex 컴파일이 중단됩니다. 위의 변경 사항을 추가했습니다. – jkeirstead

관련 문제