首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python导入(.tex文件),创建新的(.tex文件),并从导入的(.tex文件)附加新的(.tex文件)

如何使用Python导入(.tex文件),创建新的(.tex文件),并从导入的(.tex文件)附加新的(.tex文件)
EN

Stack Overflow用户
提问于 2019-07-14 09:21:25
回答 1查看 641关注 0票数 0

我有几个(1000+) .tex文件,如下所示:

File1.tex:

代码语言:javascript
复制
\documentclass[15pt]{article}
\begin{document}

Question1:

$f(x)=sin(x)$\\

Question2:

$f(x)=tan(x)$

\end{document}

File2.tex在结构上类似:

代码语言:javascript
复制
\documentclass[15pt]{article}

\begin{document}

Question1:

$f(x)=cos(x)$\\


Question2:

$f(x)=sec(x)$\\

Question3:

$f(x)=cot(x)$

\end{document}

我想要做的是写一个Python脚本,允许我从file1.tex中选择问题1,从file2.tex中选择问题3,并以以下格式编译一个新的file3.tex文件:

代码语言:javascript
复制
\documentclass[15pt]{article}
\begin{document}

Question1:
$f(x)=sin(x)$\\

Question2:
$f(x)=cot(x)$

\end{document}

PS-如果我可以在LaTex上执行这种类型的工作,我不介意。我只是想用Python最终可以创建一个GUI。

到目前为止,我已经设法通过手动键入我想要的内容来读取/附加.tex文件,而不是创建某种允许我将.tex文件的特定部分“复制”到另一个.tex文件中的系统。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-14 10:28:51

我在file1和file2.tex中使用的完全一样。我从头到尾都留下了评论,而不是一步一步地解释。

PreProcess

预处理包括创建一个xlsx文件,该文件将在第一列中包含tex文件的所有名称。

代码语言:javascript
复制
import os
import xlsxwriter

workbook = xlsxwriter.Workbook('Filenames.xlsx')
worksheet = workbook.add_worksheet("FileNames")
worksheet.write(0, 0, "NameCol")

path = os.getcwd()  # get path to directory
filecount = 1
for file in os.listdir(path):  # for loop over files in directory
    if file.split('.')[-1] == 'tex':  # only open latex files
        worksheet.write(filecount, 0, file)
        filecount += 1

workbook.close()

选择问题

现在你向右看一个列表,就像我有什么问题你想从文件中找出来一样。

PostProcess

现在,我们可以运行我们的xlsx文件,并从它创建一个新的latex文件。

代码语言:javascript
复制
import pandas as pd
import math
import os

# get data
allfileqs = []
df = pd.read_excel('Filenames.xlsx')
for row in df.iterrows():
    tempqs = []
    for i in range(len(row[1].values) - 1):
        if math.isnan(row[1].values[i + 1]):
            continue
        else:
            tempqs.append(int(row[1].values[i + 1]))
    allfileqs.append(tempqs)
print(allfileqs)
allfileqs = [["Question" + str(allfileqs[i][j]) + ':' for j in range(len(allfileqs[i]))] for i in range(len(allfileqs))]

starttex = [r'\documentclass[15pt]{article}', r'\begin{document}']
endtex = [r'\end{document}']
alloflines = []


path = os.getcwd()  # get path to directory
for file in os.listdir(path):  # for loop over files in directory
    if file.split('.')[-1] == 'tex':  # only open latex files
        lf = open(file, 'r')
        lines = lf.readlines()
        # remove all new lines, each item is on new line we know
        filt_lines = [lines[i].replace('\n', '') for i in range(len(lines)) if lines[i] != '\n']
        alloflines.append(filt_lines)  # save data for later
        lf.close()  # close file
# good now we have filtered lines
newfile = []
questcount = 1
for i in range(len(alloflines)):
    for j in range(len(alloflines[i])):
        if alloflines[i][j] in allfileqs[i]:
            newfile.append("Question" + str(questcount) + ":")
            newfile.append(alloflines[i][j + 1])
            questcount += 1
# okay cool we have beg, middle (newfile) and end of tex
newest = open('file3.tex', 'w')  # open as write only
starter = '\n\n'.join(starttex) + '\n' + '\n\n'.join(newfile) + '\n\n' + endtex[0]
for i in range(len(starter)):
    newest.write(starter[i])
newest.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57023922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档