Converting a python numeric expression to LaTeX

前端 未结 5 476
感动是毒
感动是毒 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:12

    You can use sympy.latex with eval:

    s = "1+2**(x+y)"
    sympy.latex(eval(s))   # prints '$1 + {2}^{x + y}$'
    

    You still have to declare the variables as symbols, but if this is really a problem, it's much easier to write a parser to do this than to parse everything and generate the latex from scratch.

提交回复
热议问题