It turns out that sympy
has an (experimental) LaTeX parser,
which however does not seem to be up to the task you ask.
You'll need to install the antlr4-python3-runtime
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)
Some of the version mismatch warnings are explored further.
Notwithstanding these warnings, the parser seems to be able
to correctly parse simple LaTeX expression and generate
the SymPy representation of the objects they represent
(which can then 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 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.
Could you keep us posted if you decide to go further?
EDIT : I did a bit of exploring myself. The problem seems 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 point to the lack of an 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,
The expression $$ z_k = 2 \sqrt{\frac{-p}3} \cos\left( \frac13\arccos\left(\frac{-q}2\sqrt{\frac{27}{-p^3}}\right) + \frac{2k\pi}3\right) $$ (with $k\in {0,1,2}$) is not too long. Typing with bare hands should be please enough in this case. (It is really hard to parse such a thing. Python does not have
latex( myExpression )
. Then sage makes us a present, providinglatex
. But its reverse engineering... i.e. parse spaces, the superfluous brackets, function names, (sums and products with variables running "implicitly"), and prepositions in all languages in latex expressions taken from all possible wiki or article latex expressions... This is really too much for a rather small community.)you could try this one (i haven't) : https://github.com/augustt198/latex2s...