Joe
Joe

Reputation: 4514

Using python to run Latex compiler, why does it hang if there are errors in the latex?

I have a python script that takes the (latex source) content of a google doc and creates a pdf.

This is the function I use for the 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)

Everything runs fine if the latex has NO errors, but if there is a problem, the function hands and nothing gets done. What I want is that problems are noted and exported into the trace. Any ideas what's going wrong?

Upvotes: 0

Views: 1056

Answers (2)

Alicia Garcia-Raboso
Alicia Garcia-Raboso

Reputation: 13913

When it encounters errors, pdflatex asks the user about how to proceed, so your script "hangs" because it is expecting input. Use pdflatex -interaction=nonstopmode -halt-on-error. See this TeX StackExchange question.

Upvotes: 1

amicitas
amicitas

Reputation: 13651

I think what you are missing is that you need to also need to setup a pipe for STDERR. This will let you see the error messages from pdflatex. You could also try explicitly setting the buffer size to zero when calling Popen.

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

Upvotes: 0

Related Questions