Converting a python numeric expression to LaTeX

前端 未结 5 483
感动是毒
感动是毒 2020-12-08 03:00

I need to convert strings with valid python syntax such as:

\'1+2**(x+y)\'

and get the equivalent LaTeX:

$1+2^{x+y}$
         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-08 03:21

    You can use SymPy. Just pass the string to the sympify() function first, which will convert it to a valid SymPy expression (i.e., create the Symbols for you, etc.). So you could do

    >>> latex(sympify('1+2**(x+y)'))
    1 + 2^{x + y}
    

    S() is also a shortcut to sympify(), i.e., latex(S('1+2**(x+y)')) also works.

提交回复
热议问题