微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pylatexen无法分割段落

如何解决pylatexen无法分割段落

我正在使用 pylatexenc 解析 Latex 文档。我没有使用这个包的经验,找到它 here

我有这个简单的 Latex 文档:

\documentclass{article}

\begin{document}
\title{This is my Latex document}
\author{Gordon Shumway}
\maketitle

\section{Introduction}

First paragraph.

This is a second paragraph in the first section.

\section{Second section}

Many times we have equations like this one 
\begin{equation}
\left(\gamma^{\mu}p_{\mu}-m\right)\left|\psi\right\rangle =0\label{Equation: Dirac}
\end{equation}
 and inline math such as $\psi$. And I can insert a reference to~(\ref{Equation: Dirac}).
\end{document}

可以看出第一节有两段。如果我使用带有以下代码pylatexenc 解析文档

from pylatexenc.latexwalker import LatexWalker

with open('latex_document.tex','r') as file:
    latex_file_str = file.read()

w = LatexWalker(latex_file_str)
nodelist,_,_ = w.get_latex_nodes(pos=0)

document = None
for node in nodelist:
    if hasattr(node,'environmentname'):
        if node.environmentname == 'document':
            document = node

for node in document.nodelist:
    print(node,end=4*'\n')
    
document_title = None
for node in document.nodelist:
    if hasattr(node,'macroname'):
        if node.macroname=='title':
            document_title = node
print(f'Title = {document_title.nodeargd.argnlist[0].nodelist[0].chars}')

然而,这两个段落被解析为一个元素:

Bla bla bla...
LatexCharsNode(parsing_state=<parsing state 140254491546672>,pos=134,len=70,chars='\n\nFirst paragraph.\n\nThis is a second paragraph in the first section.\n\n')
Bla bla bla...

我做错了吗?这是意料之中的吗?这是 pylatexenc 的限制吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。