使用 C# 将 Tex 转换为 txt
我有一个 Tex 文件。我想使用 C# 将其转换为 txt 文件。 请帮忙。 我确信它包含的信息是纯文本,但使用的格式是 Tex。
I have a Tex file. Which I want to convert to txt file using C#.
Please help.
I am sure the info it contains is plain text but format used is Tex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在可靠地提取任何类型的文本之前,需要完全解释 TeX 文件。用 C# 编写一个成熟的 TeX 解释器可能是一项真正的任务,因此我建议使用现有的解释器,它可以输出更容易解析的格式,例如 HTML。
您可能倾向于编写一个正则表达式来简单地删除所有
\command
,但这不会给您一个非常准确的结果,因为某些命令会生成自己的文本,而[不太常见]一些命令会生成自己的文本。命令会吞噬其后面的文本。此外,您还会遇到字符更改类别代码的问题,某些字符可能位于活动类别代码中,这意味着字符本身可以是命令。TeX files need to be fully interpreted before any sort of text can be reliably extracted. Writing a full-fledged TeX interpreter in C# may be a real task, so instead I would suggest using an existing interpreter that can output a format that can be parsed more easily, such as HTML.
You may feel inclined to write a regular expression to simply strip all
\command
, but this won't give you a very accurate result, because some commands produce text of their own, and [less commonly] some commands gobble the text that follows it. Also, you will run into issues where characters change category codes, some characters may be in the active category code, meaning characters on their own can be commands.