读取LaTeX表达式并使用python将其打印为ASCII

时间:2022-12-01 13:52:55

I'm running some python code in the terminal and I want to output a pretty ASCII representation of a LaTeX expression. I realize I can pop up a separate window using matplotlib, but I don't want the text in a separate window. Sympy does a nice job printing, but doesn't seem to import LaTeX (at least not that I've found).

我在终端中运行一些python代码,我想输出LaTeX表达式的漂亮ASCII表示。我意识到我可以使用matplotlib弹出一个单独的窗口,但我不想在单独的窗口中显示文本。 Sympy印刷很好,但似乎没有导入LaTeX(至少不是我发现的)。

Ideally, It would work something like this:

理想情况下,它会像这样工作:

print('$x^2$')

would output in the console:

将在控制台中输出:

 2
x

Just like sympy would if I made a sympy symbol x and printed x**2.

就像同情,如果我做了一个同情符号x并打印x ** 2。

More complicated expressions would have to be handled appropriately. For example:

必须妥善处理更复杂的表达。例如:

\lim_{n \to \inf} \sum_{x=1}^{n} \frac{1}{x}

could be rendered as:

可以表示为:

        n
       -----
       \      1
lim     >   -----
n->inf /      x
       -----
        x=1

Characters which are not supported in ASCII such as $\alpha$ could be expanded to an ASCII equivalent such as "alpha" or an error could be thrown.

ASCII不支持的字符(如$ \ alpha $)可以扩展为ASCII等效项,例如“alpha”,否则可能会抛出错误。

1 个解决方案

#1


1  

LaTeX parsing is not currently included in SymPy, although it has been speculated since 2012 with a decently high priority.

LamX解析目前尚未包含在SymPy中,尽管自2012年以来推测其具有相当高的优先级。

The third-party latex2sympy may be able to fulfill your needs.

第三方latex2sympy可能能够满足您的需求。

#1


1  

LaTeX parsing is not currently included in SymPy, although it has been speculated since 2012 with a decently high priority.

LamX解析目前尚未包含在SymPy中,尽管自2012年以来推测其具有相当高的优先级。

The third-party latex2sympy may be able to fulfill your needs.

第三方latex2sympy可能能够满足您的需求。