I need to convert strings with valid python syntax such as:
\'1+2**(x+y)\'
and get the equivalent LaTeX:
$1+2^{x+y}$
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.