从错误目录中创建的乳胶代码生成的 pdf 文件 - Python

A pdf file generated from latex code created in a wrong directory - Python

我目前正在尝试编写一个程序来打开一个乳胶文件并生成一个同名的 pdf 并将其保存在与乳胶文件相同的文件夹中。
我的问题:pdf 文件是在与我 运行 程序的 python 文件相同的文件夹中创建的,而不是 latex 文件所在的文件夹 - 我不能让我明白为什么会这样。我试图分两步实现我的目标。
首先,我使用一个函数获取目录和 latex 文件的标题,以便可以生成具有相同名称的 pdf:

 def getName(self, somestring):                                                    

    if "/" in somestring:
        indexsofslash = [i for i, ltr in enumerate(somestring) if ltr == "/"]
        indexsofdot = [i for i, ltr in enumerate(somestring) if ltr == "."]
        lastindexslash = max(indexsofslash)
        lastindexdot = max(indexsofdot)
        mainName = somestring[lastindexslash + 1:lastindexdot]
        pdfDirectory = somestring[:lastindexslash + 1]
        someothername = pdfDirectory +  mainName
        newname = someothername
    else:
        newname = somestring 
    return newname

之后我使用这个函数生成一个pdf文件,使用:

def generate_pdf(self):

    name=self.getName(self.fl)      
    f = open('%s.tex'%name,'w')
    tex = self.txt.get(1.0,"end-1c")
    f.write(tex)
    f.close()
    proc=subprocess.Popen(['pdflatex','%s.tex'%name])
    proc.communicate()
    os.startfile('%s.pdf'%name)
    #os.unlink('%s.tex'%name)
    os.unlink('%s.log'%name)
    os.unlink('%s.aux'%name)

*注意:打印name时显示的是latex文件的目录+文件的标题,即H:/Public/Test/test.

会发生什么:在我 运行 程序的 python 文件所在的文件夹中生成 pdf,而不是在我的 latex 文件所在的文件夹中生成 - 那是什么我做错了吗?

问题是默认情况下 pdflatex 将输出文件写入当前工作目录。要改变该行为,您需要提供一个选项。

看看 pdflatexman 页面:

$ man pdflatex

并搜索 -output-directory,您将看到:

-output-directory directory
Write output files in directory instead of the current directory. Look up input files in directory first, the along the normal search path.

因此您需要将 python 代码修改为如下内容:

proc=subprocess.Popen(['pdflatex', '-output-directory', pdfDirectory, '%s.tex' % name])

假设 pdfDirectory 是您的目的地。