使用python运行Latex编译器,如果乳胶中有错误,为什么会挂起?

时间:2016-06-29 14:32:28

标签: python latex

我有一个python脚本,它获取谷歌文档的(乳胶源)内容并创建一个pdf。

这是我用于pdf的功能:

# -*- coding: utf-8 -*-
#!/usr/bin/python
"""One of the main activiating files of IMPS - this downloads all the files in a directory, creates  the input.tex file and archives them a tar file

TODO

Before we send to stackoverflow we should make sure that everthing is in a function and that the If __main__ trigger is working
I'd also like to have doc strings for all of the methods


"""

import os
import glob
import tarfile
import time
import datetime
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import urlparse
import argparse
import re

def generate_pdf(filename,destination=""):
    """
    Genertates the pdf from string
    from http://stackoverflow.com/q/19683123/170243
    """
    import subprocess
    import shutil
    current = os.getcwd()
    this_dir=os.path.dirname(os.path.realpath(__file__))
    os.chdir(this_dir+"/latex")
    proc=subprocess.Popen(['pdflatex',filename+'.tex'],stdout=subprocess.PIPE)
#   subprocess.Popen(['pdflatex',tex])
    temp=proc.communicate()
    #Have to do it twice so that the contents pages are right
    proc=subprocess.Popen(['pdflatex',filename+'.tex'],stdout=subprocess.PIPE)
    temp=proc.communicate()
    shutil.copy(filename+'.pdf',"../../live/"+destination+filename+ str(datetime.datetime.now()).replace(".", "-").replace(":", "_") + ".pdf")
    trace_file = open("../../live/"+destination+"trace.txt", "w")
    print >>trace_file, temp[0]
    print >>trace_file, temp[1]
    trace_file.close()
    os.chdir(current)

如果乳胶没有错误,一切都运行良好,但如果出现问题,该功能就可以完成。我想要的是记录问题并将其导出到跟踪中。任何想法出了什么问题?

2 个答案:

答案 0 :(得分:1)

当遇到错误时,pdflatex会询问用户如何继续,因此您的脚本会挂起"因为它期待输入。使用pdflatex -interaction=nonstopmode -halt-on-error。请参阅this TeX StackExchange question

答案 1 :(得分:0)

我认为您缺少的是您还需要为STDERR设置管道。这样您就可以看到来自pdflatex的错误消息。您还可以在调用Popen时尝试将缓冲区大小明确设置为零。

self.child = subprocess.Popen(command
                              ,bufsize=0
                              ,stdout=subprocess.PIPE
                              ,stderr=subprocess.PIPE)