python - pylatexen 无法分割段落
问题描述
我正在使用pylatexenc
. 我对这个包没有经验,在这里找到了。
我有这个简单的 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
吗?
解决方案
如您所述,pylatexenc
目前不拆分段落。一个想法是将字符声明\n\n
为“特殊乳胶”(参见,例如此处),但在内部pylatexenc
特别对待空格,这个想法不起作用。您可以在github上发布功能请求或提交拉取请求 :)
推荐阅读
- powershell - 比较两个 csv 文件,在 powershell 中缺少预期结果
- java - Oauth2 刷新令牌
- reactjs - 调用 Jest 模拟函数。但是模拟的状态没有更新?
- java - 如何在java中制作待办事项列表?
- azure - 在 Microsoft Graph 中检索每个应用程序的角色和用户
- java - 在 Socket.io-client 中发送自定义标头
- vsto - 使用 MSI 安装程序(通过 Wix 生成)后无法更改 Word AddIn 的 VSTO appSettings
- powershell - Powershell - 在 Add-Content Cmdlet 内的 .log 文件中包含 Get-Date
- typescript - Vue with Typescript:将 Vue 应用程序从 JavaScript 转换为 TypeScript 时遇到的问题
- c# - C#如何将非数字值设置为0