Reputation: 21
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
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
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