首页 > 解决方案 > 替换行并循环编译乳胶文件

问题描述

我有一个 LaTeX 代码,其中有我单独编译的多个章节。有时我必须编译很多这样的文件,我试图自动化这个过程,而不是取消注释、编译和注释,再次是章节的行。我还必须重命名并将我编译的章节放到另一个文件夹中。

我已经设法为一章编写代码:

import os

texdoc = []

with open('aluno.tex') 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') 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实现这一点?

标签: pythonlatex

解决方案


推荐阅读