Converting a python numeric expression to LaTeX

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

    To build on tom10's answer you can define a dictionary that will generate symbols and use that when calling eval:

    from collections import defaultdict
    class GenerateSymbols(defaultdict):
      def __missing__(self, key):
        return sympy.Symbol(key)
    

    Then if you use

    sympy.latex(eval('1+2**(x+y)',GenerateSymbols()))
    

    you shouldn't have to worry about pre-creating Symbols for the variables.

提交回复
热议问题