【问题标题】:Compile Latex files from Python using a string within a loop使用循环中的字符串从 Python 编译 Latex 文件
【发布时间】:2020-10-20 22:12:44
【问题描述】:

我正在创建大量数学测试,其中包含生成随机数的问题,我希望能够保存多个副本,将它们推送到乳胶并将它们全部保存在一个 python 代码中。我了解 python os.system 命令和 subprocess.run 命令可以做到这一点,但我无法弄清楚如何将文件放入命令中,因为它会迭代。我目前正在尝试使用字符串来完成此任务。

for i in range(1, 11):
    f = open("test_" + str(i).zfill(2) + ".tex", 'w+')
    ##### run test building code here ######
    f.close()
    #subprocess.run("pdflatex f.name")  #<--- How do I get this to run for each of the 10 files 

我可以使用实际的文件名运行它,所以我知道这并不难,但我似乎无法弄清楚如何传入字符串文件名。

【问题讨论】:

    标签: python latex pdflatex os.system


    【解决方案1】:

    预先构建名称,使用 f 字符串将其传入(假设您使用的是 Python 3.6+,否则请参阅 here)。注意字符串前面的f与文件f无关。

    for i in range(1, 11):
        file_path = "test_" + str(i).zfill(2) + ".tex"
        f = open(file_path , 'w+')
        #####
        f.close()
        subprocess.run(f"pdflatex {file_path}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多