问题描述
我正在使用 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 (将#修改为@)