__init__.py 991 B

1234567891011121314151617181920212223242526272829303132333435
  1. from sympy.external import import_module
  2. from sympy.utilities.decorator import doctest_depends_on
  3. from .errors import LaTeXParsingError # noqa
  4. @doctest_depends_on(modules=('antlr4',))
  5. def parse_latex(s):
  6. r"""Converts the string ``s`` to a SymPy ``Expr``
  7. Parameters
  8. ==========
  9. s : str
  10. The LaTeX string to parse. In Python source containing LaTeX,
  11. *raw strings* (denoted with ``r"``, like this one) are preferred,
  12. as LaTeX makes liberal use of the ``\`` character, which would
  13. trigger escaping in normal Python strings.
  14. Examples
  15. ========
  16. >>> from sympy.parsing.latex import parse_latex
  17. >>> expr = parse_latex(r"\frac {1 + \sqrt {\a}} {\b}")
  18. >>> expr
  19. (sqrt(a) + 1)/b
  20. >>> expr.evalf(4, subs=dict(a=5, b=2))
  21. 1.618
  22. """
  23. _latex = import_module(
  24. 'sympy.parsing.latex._parse_latex_antlr',
  25. import_kwargs={'fromlist': ['X']})
  26. if _latex is not None:
  27. return _latex.parse_latex(s)