esphire
esphire

Reputation: 21

Make the string as parameter of sympy.latex function

Help! I am learning sympy and have trouble with sympy.latex function. I want the sympy.latex function to take string as parameter, but in this case it doesn't work. For example,

from sympy import  *

x, y, n = symbols('x y n')
print latex(n + x/y)

the output is : n + \frac{x}{y}

but when i write

myString = 'n + x/y'
print latex(myString)

output is : n + x/y although i want to see : n + \frac{x}{y}

What should I do?

Upvotes: 2

Views: 224

Answers (2)

Sartaj Singh
Sartaj Singh

Reputation: 306

latex works only with SymPy objects AFAIK. You should first convert the string to a SymPy object. A simple solution should be latex(sympify('n + x/y')).

Upvotes: 2

asmeurer
asmeurer

Reputation: 91620

In general, you shouldn't rely on the fact that some SymPy functions happen to work with strings as input. You should convert strings to SymPy expressions first with sympify.

expr = sympify(myString)
latex(expr)

Upvotes: 2

Related Questions