# SymPy latexify output

I am using SymPy in a jupyter notebook. It gives me nice LaTeX output, which I want to use as input for further work (in julia). Let’s say SymPy gives me the determinant of a symbolic matrix in LaTeX code, which is long and complicated. I want it in the kind of code that I can feed julia, for example I want to convert “\frac{1}{x+1}” to “1/(x+1)”

Can you give a simple example of what you are doing to get the
output?

This works for me in the regular Python interpreter, I haven’t tried it
in Jupyter or IPython.

import sympy
x = sympy.symbols('x')
sympy.latex(x**2)  # outputs the string 'x^{2}'
sympy.latex(1/x)   # outputs the string '\\frac{1}{x}'


Steven,

This is really helpful. When I asked the question, I was not very clear. I want the opposite of what you are doing. I want to convert “\frac{1}{x}” to “1/x” – you can apparently do this in mathematica (using ToExpression), but I didn’t find anything for python. SymPy provides output in LaTeX, for example if you are looking for the determinant of a symbolic matrix.

>>> from sympy.parsing.latex import parse_latex
>>> str(parse_latex(r'\frac{1}{x}'))
'1/x'


I don’t really understand why you would want to produce LaTeX, and then parse LaTeX to generate code in a different syntax. SymPy probably has a simpler way to achieve what you want to do.

I don’t know what it is, but you might be able to find it somewhere around here: Code Generation — SymPy 1.9 documentation