LaTeX 到“自由形式”转换器
我想开发一个转换器,它将接受以“自由形式”编写的数学或类似 Wolfram Alpha 会接受的东西。例如:2*x+(7+y)/(z^2)
将转换为 2\cdot x+\frac{7+y}{z^{2}}< /code> (请忽略我在这里可能犯的任何语法错误),反之亦然。我想知道是否存在用于 C++/Java 的 LaTeX 库,可以解析和/或在内存中保存 LaTeX 表达式。如果有,请分享。
如果没有,你会如何写这样的东西?可以使用普通的 Java/C++ 代码吗?还是应该使用 lex 之类的代码?
I would like to develop a converter that will accept math written in "freeform" or something like Wolfram Alpha would accept. For example: 2*x+(7+y)/(z^2)
will be transformed to 2\cdot x+\frac{7+y}{z^{2}}
(please ignore any syntax mistakes I might have made here) and vice versa. I was wondering if there exists a LaTeX library for C++/Java that parses and/or holds LaTeX expression in memory. If so, please share.
If not, how would you go about writing something like this? Is it okay to use normal Java/C++ code for this or should I use something like lex?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,在 Flex/Bison 中编写“自由形式”的解析器是绝对可以的,事实上我建议不要使用 TeX,因为该语言不是为解析而编写的。我什至记得曾经用 Flex 和 Bison 和 Kimwitu++,允许在输出之前执行任意转换。请注意,您删除了原始公式的括号。在一般情况下,这可能不是您想要的。
相反,评论已经提到在一般情况下几乎不可能做到。对于受限情况,Bison 就足够了。
Yes, it is absolutely OK to write the parser for ‘freeform’ in Flex/Bison, in fact I’d recommend against using TeX, as that language was not written for parsing. I even remember having done exactly that (‘freeform’ to LaTeX) as an exercise with Flex and Bison and Kimwitu++, which allowed to perform arbitrary transformations before output. Note that you dropped the parentheses of your original formula. This might of might not be what you want in the general case.
For the other way round, the comments already mentioned that it’s nearly impossible to do in the general case. For restricted cases, Bison is good enough.