文件
gpt_academic/crazy_functions/Latex输出PDF结果.py
505030475 e769f831c7 latex
2023-06-02 14:07:04 +08:00

42 行
1.3 KiB
Python

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

from toolbox import update_ui, trimmed_format_exc
from toolbox import CatchException, report_execption, write_results_to_file, zip_folder
import glob
def 编译Latex(main_tex, work_folder):
import os
current_dir = os.getcwd()
os.chdir(work_folder);
main_file = os.path.basename(main_tex)
assert main_file.endswith('.tex')
main_file = main_file[:-4]
os.system(f'pdflatex {main_file}.tex')
os.system(f'bibtex {main_file}.aux')
os.system(f'pdflatex {main_file}.tex')
os.system(f'pdflatex {main_file}.tex')
os.chdir(current_dir)
pdf_output = os.path.join(work_folder, f'{main_file}.pdf')
assert os.path.exists(pdf_output)
return pdf_output
def Latex预处理(tar_file):
from toolbox import extract_archive
import shutil
work_folder = 'private_upload/latex_workshop_temp'
try:
shutil.rmtree(work_folder)
except:
pass
res = extract_archive(tar_file, dest_dir=work_folder)
for texf in glob.glob('private_upload/latex_workshop_temp/*.tex'):
with open(texf, 'r', encoding='utf8') as f:
file_content = f.read()
if r'\documentclass' in file_content:
return texf, work_folder
else:
continue
raise RuntimeError('无法找到一个主Tex文件包含documentclass关键字')