如何获取LaTeX文件中的所有`\ begin {definition} ... \ end {definition}`块? [英] How can I get all the `\begin{definition}...\end{definition}` blocks in a LaTeX file?

查看:47
本文介绍了如何获取LaTeX文件中的所有`\ begin {definition} ... \ end {definition}`块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚写完了Latex中的微积分摘要.

I just finished to write the summary for calculus in Latex.

现在的主要问题是文件包含许多我现在并不真正需要的东西.

The main problem now is that the files contains many things I don't really need now.

.tex文件包含许多我需要认真研究的定义和定理.

The .tex files contains many definitions and theorems that i need to study by heart.

这些定义在tex文件中有其自己的定义,因此该文件中的任何定义都将以以下内容开头:

The definitions have their own definition in the tex file, so any definition in the file will start with:

\begin{definition}

\end{definition}

定理也一样.

我需要写点东西来取出 \ begin {} ... \ end {} 内部的所有内容.

I need to write something to take out whatever is inside the \begin{}...\end{}.

例如在名为A的列表中:

For example in a list called A:

\begin{document}

\begin{center}
\begin{definition} Hello WOrld! \end{definition}
\begin{example}A+B \end{example}
\begin{theorem} Tre Capre \end{theorem}
\begin{definition} Hello WOrld2! \end{definition}
\end{center}
\end{document}

应包含: [[\\ begin {definition}您好,WOrld!\ end {definition}],[\ begin {theorem} Tre Capre \ end {th​​eorem}],[\ begin {definition}您好,WOrld2!\ end {definition}]]

在这个站点上,我发现我可以使用正则表达式:

Looking in this site i found that i can use Regular Expressions:

for i in range(5):
    x = i+1
    raw = open('tex/chapter' + str(x) + '.tex')
    A = []
    for line in raw:
        A.append(re.match(r'(\begin{definition})://.*\.(\end{definition})$', line))
print(A)

但输出仅为 None ,我真的不知道为什么.

but the output is just None and I don't really know why.

import re


for i in range(5):
    x = i+1
    raw = open('tex/chapter' + str(x) + '.tex')
    A = re.findall(r'\\begin{definition}(.*?)\\end{definition}', raw.read())
    print(A)

输出如下:

[]
[]
[]
[]
[]

推荐答案

从我从问题中得到的结果来看,您只需要Latex文件中的定义.您可以使用 findall 直接获取您的定义:

From what I get from the question you just want the definitions from the Latex file. You can use findall to directly get your definitions:

A = re.findall(r'{definition}(.*?)\\end{definition}', raw.read())

请注意.*?的用法,以解决

Note the usage to .*? in order to tackle the greedy regex matching

这篇关于如何获取LaTeX文件中的所有`\ begin {definition} ... \ end {definition}`块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆