Physics Derivation Graph navigation Sign in

latex to sympy and sympy to latex

Published 2020-06-20T14:09:00.002Z by Physics Derivation Graph

From Latex to Sympy:
$ python
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy
>>> from sympy.parsing.latex import parse_latex
>>> sympy.srepr(parse_latex('\\frac{p}{\\hbar} = k'))
"Equality(Mul(Pow(Symbol('hbar'), Integer(-1)), Symbol('p')), Symbol('k'))"

A more complicated example

>>> sympy.srepr(parse_latex('\\frac{\\partial}{\\partial t} \\psi( r,t)'))
"Derivative(Function('psi')(Symbol('r'), Symbol('t')), Tuple(Symbol('t'), Integer(1)))"

Check that Sympy-to-Latex produces the same:
>>> sympy.latex(sympy.Derivative(sympy.Function('psi')(sympy.Symbol('r'), sympy.Symbol('t')), sympy.Tuple(sympy.Symbol('t'), sympy.Integer(1))))
'\\frac{\\partial}{\\partial t} \\psi{\\left(r,t \\right)}'