Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It turns out that sympy has an (experimental) latex parser, which however does not seem to be able to do what you mean. You'll need to install the antlr4-python3-runtime library in Sage's pip.

sage: from sympy.parsing.latex import parse_latex
sage: parse_latex(r"\int_a^b f(x) dx")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
Integral(f(x), (x, a, b))
sage: parse_latex(r"\int_a^b f(x) dx")._sage_()
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
integrate(f(x), x, a, b)

One notes warnings about version mismatches (tome explored further). Notwithstanding these warnings, the parser seems to be able to correctly parse simple latex expression and genetare the sumpy representation of the objects they represent (which be re-translated to sage).

However, your latex expression is to complex to be parsed :

sage: parse_latex(z_k)
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2

Eliminating the "obvious suspects" (text at the end of the expression, implicit multiplications) is not sufficient :

sage: z_k1 = r"2*\sqrt{\frac{-p}3}*\cos{\left(\frac13\arccos{\left(\frac{-q}2\sqrt{\frac{27}{-p^3}}\right)}+ \frac{2*k\pi}3\right)}"
sage: parse_latex(z_k1)
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2

Some exploration seems to be in order. Could you keep us posted if you decide to go further ?

HTH,

It turns out that sympy has an (experimental) latex parser, which however does not seem to be able to do what you mean. You'll need to install the antlr4-python3-runtime library in Sage's pip.

sage: from sympy.parsing.latex import parse_latex
sage: parse_latex(r"\int_a^b f(x) dx")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
Integral(f(x), (x, a, b))
sage: parse_latex(r"\int_a^b f(x) dx")._sage_()
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
integrate(f(x), x, a, b)

One notes warnings about version mismatches (tome explored further). Notwithstanding these warnings, the parser seems to be able to correctly parse simple latex expression and genetare the sumpy representation of the objects they represent (which be re-translated to sage).

However, your latex expression is to complex to be parsed :

sage: parse_latex(z_k)
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2

Eliminating the "obvious suspects" (text at the end of the expression, implicit multiplications) is not sufficient :

sage: z_k1 = r"2*\sqrt{\frac{-p}3}*\cos{\left(\frac13\arccos{\left(\frac{-q}2\sqrt{\frac{27}{-p^3}}\right)}+ \frac{2*k\pi}3\right)}"
sage: parse_latex(z_k1)
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2

Some exploration seems to be in order. Could you keep us posted if you decide to go further ?

EDIT : I did a bit of exploring myself. The problem seems to be bound to the fact that the arguments of \frac must be surrounded by braces in order to be parsed :

parse_latex(r"\frac{1}{2}")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
1/2
sage: parse_latex(r"\frac12")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
---------------------------------------------------------------------------
LaTeXParsingError                         Traceback (most recent call last)

[  Snip... ]

LaTeXParsingError: missing '{' at '12'
\frac12
~~~~~^

And, indeed, :

sage: parse_latex(r"2 \sqrt{\frac{-p}{3}} \cos{\left(\frac{1}{3}\arccos{\left(\frac{-q}{2}\sqrt{\frac{27}{-p^3}}\right)}+ \frac{2k\pi}{3}\right)}")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2*((sqrt(3)*sqrt(-p)/3)*cos((2*(k*pi))/3 + acos(((-q)/2)*(3*sqrt(3)*sqrt(1/(-p**3))))/3))

which is, indeed, (mathematically) equal to the typeset expression ; however, sympy and/or sage refactored some parts of the expression (see the innumerable questions about the "right" way to format an expression, which amount to the lack of algorithmic definition of "well-typeset"...), and the expressions are cosmetically different.

TL;DR : parse_latex might help more with some latex expressions if they were not too much "hand-optimized" (the frac in \frac12 has indeed two arguments (see the \TeX book...), but is too "hand-optimized" for parse_latex (or for my taste, BTW...)).

HTH,

It turns out that sympy has an (experimental) latex parser, LaTeX parser, which however does not seem to be able to do what up to the task you mean. ask. You'll need to install the antlr4-python3-runtime library in library using Sage's pip.

sage: from sympy.parsing.latex import parse_latex
sage: parse_latex(r"\int_a^b f(x) dx")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
Integral(f(x), (x, a, b))
sage: parse_latex(r"\int_a^b f(x) dx")._sage_()
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
integrate(f(x), x, a, b)

One notes Some of the version mismatch warnings about version mismatches (tome are explored further). further. Notwithstanding these warnings, the parser seems to be able able to correctly parse simple latex LaTeX expression and genetare the sumpy generate the SymPy representation of the objects they represent represent (which can then be re-translated to sage). Sage).

However, your latex LaTeX expression is to complex to be parsed :parsed:

sage: parse_latex(z_k)
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2

Eliminating the "obvious suspects" (text at the end of the expression, expression, implicit multiplications) is not sufficient :enough:

sage: z_k1 = r"2*\sqrt{\frac{-p}3}*\cos{\left(\frac13\arccos{\left(\frac{-q}2\sqrt{\frac{27}{-p^3}}\right)}+ \frac{2*k\pi}3\right)}"
sage: parse_latex(z_k1)
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2

Some exploration seems to be in order. order. Could you keep us posted if you decide to go further ?further?

EDIT : I did a bit of exploring myself. The problem seems to be bound bound to the fact that the arguments of \frac must be surrounded surrounded by braces in order to be parsed :parsed:

parse_latex(r"\frac{1}{2}")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
1/2
sage: parse_latex(r"\frac12")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
---------------------------------------------------------------------------
LaTeXParsingError                         Traceback (most recent call last)

[  Snip... ]

LaTeXParsingError: missing '{' at '12'
\frac12
~~~~~^

And, indeed, :indeed:

sage: parse_latex(r"2 \sqrt{\frac{-p}{3}} \cos{\left(\frac{1}{3}\arccos{\left(\frac{-q}{2}\sqrt{\frac{27}{-p^3}}\right)}+ \frac{2k\pi}{3}\right)}")
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
ANTLR runtime and generated code versions disagree: 4.9!=4.7.2
2*((sqrt(3)*sqrt(-p)/3)*cos((2*(k*pi))/3 + acos(((-q)/2)*(3*sqrt(3)*sqrt(1/(-p**3))))/3))

which is, indeed, (mathematically) equal to the typeset expression ; expression; however, sympy SymPy and/or sage Sage refactored some parts of the expression expression (see the innumerable questions about the "right" way to format an expression, expression, which amount point to the lack of an algorithmic definition of "well-typeset"...), "well-typeset"...), and the expressions are cosmetically different.

TL;DR : parse_latex might help more with some latex expressions LaTeX expressions if they were not too much "hand-optimized" (the frac in \frac12 has indeed two arguments (see the \TeX book...), but is too too "hand-optimized" for parse_latex (or for my taste, BTW...)).

HTH,