Ask Your Question
1

reverse function of latex()

asked 2017-06-06 12:01:17 +0100

ortollj gravatar image

updated 2020-12-31 10:42:59 +0100

slelievre gravatar image

Imagine I got this Latex expression from wikipedia:

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)}\qquad\mbox{ avec }\qquad k\in\{0,1,2\}

$$ 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)}\qquad\mbox{ avec }\qquad k\in{0,1,2} $$

Is there a reverse function kind of

unlatex(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)}\qquad\mbox{ avec }\qquad k\in\{0,1,2\})

which will gives me the expression in classical form (not in Latex)?

edit retag flag offensive close merge delete

Comments

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, providing latex. 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.)

dan_fulea gravatar imagedan_fulea ( 2017-06-06 19:25:18 +0100 )edit

you could try this one (i haven't) : https://github.com/augustt198/latex2s...

mforets gravatar imagemforets ( 2017-06-17 10:43:21 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-12-31 11:28:32 +0100

Emmanuel Charpentier gravatar image

updated 2020-12-31 15:38:00 +0100

slelievre gravatar image

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,

edit flag offensive delete link more

Comments

on the other hand I must say that the latex example from wiki that I gave is a little weird !

personally I didn't know that we had the right to write fractions like that :\sqrt{\frac{-p}3} !

ortollj gravatar imageortollj ( 2020-12-31 18:57:19 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-06-06 12:01:17 +0100

Seen: 1,088 times

Last updated: Dec 31 '20