使用 python 替换行并循环编译 latex 文件

使用 python 替换行并循环编译 latex 文件

我有一个 LaTeX 代码,其中包含多个章节,我分别对其进行编译。有时我必须编译很多这样的文件,我试图使这个过程自动化,而不是取消注释、编译和注释章节的行。我还必须重命名我编译的章节并将其放入另一个文件夹中。

我已经成功地为一个章节编写了代码:

import os

texdoc = []

with open('aluno.tex') as fin:
   for line in fin:
        texdoc.append(line.replace('% \\include{chapters/funcoes}', '\\include{chapters/funcoes}'))

with open('aluno.tex', 'w') as fout:
    for i in range(len(texdoc)):
        fout.write(texdoc[i])

os.system("latexmk -pdfxe -c aluno.tex")

with open('aluno.tex', errors="ignore") as fin:
    for line in fin:
        texdoc.append(line.replace('\\include{chapters/funcoes}', '% \\include{chapters/funcoes}'))


# write back the new document
with open('aluno.tex', 'w', errors="ignore") as fout:
    for i in range(len(texdoc)):
        fout.write(texdoc[i])

os.rename('aluno.pdf','Introdução às Funções.pdf')

shutil.copy('Introdução às Funções.pdf', 'subfolder', follow_symlinks=True)
os.remove('Introdução às Funções.pdf')

我现在想做的是通过我想要编译的章节列表来自动化这个过程。比如

chapter = ["funcoes","taxa"]

name = ["Introdução às Funções", "Taxa de Variação"]

for j in chapter and k in name

   with open('aluno.tex') as fin:
   for line in fin:
        texdoc.append(line.replace('% \\include{chapters/j}', '\\include{chapters/j}'))
    
   os.rename('aluno.pdf','k.pdf')

我怎样才能用 python 实现这个?

相关内容