1

I have latex

\frac{-\left(2\left(b_{x}-a_{x}\right)\right)+\sqrt{\left(2\left(b_{x}-a_{x}\right)\right)^{2}-4\left(a_{x}-2b_{x}+c_{x}\right)\left(a_{x}-g_{x}\right)}}{2\left(a_{x}-2b_{x}+c_{x}\right)}

It's not malformed and renders as:

$$ \frac{-\left(2\left(b_{x}-a_{x}\right)\right)+\sqrt{\left(2\left(b_{x}-a_{x}\right)\right)^{2}-4\left(a_{x}-2b_{x}+c_{x}\right)\left(a_{x}-g_{x}\right)}}{2\left(a_{x}-2b_{x}+c_{x}\right)} $$

Wolfram Alpha is ignoring it though:

enter image description here

How can I convert latex in general into a representation of an expression that wolframalpha will accept?

minseong
  • 1,293
  • 1
    Can you post an example of Wolfram accepting LaTeX as input? That would be useful. This question is probably better suited for Mathematica.SE https://mathematica.stackexchange.com – Giuseppe Negro Nov 16 '22 at 19:25
  • @GiuseppeNegro simplify \frac{a}{2a} works – minseong Nov 16 '22 at 19:26
  • Here's it simplified: $$\frac{a_{x} - b_{x} + \sqrt{\left(a_{x} - b_{x}\right)^{2} - \left(a_{x} - g_{x}\right) \left(a_{x} - 2 b_{x} + c_{x}\right)}}{a_{x} - 2 b_{x} + c_{x}}$$ \frac{a_{x} - b_{x} + \sqrt{\left(a_{x} - b_{x}\right)^{2} - \left(a_{x} - g_{x}\right) \left(a_{x} - 2 b_{x} + c_{x}\right)}}{a_{x} - 2 b_{x} + c_{x}} – Sam Nov 16 '22 at 19:28
  • 1
    Weirdly, WA appears to dislike a space after \frac. this works, this does not. – lulu Nov 16 '22 at 19:28
  • Ok, I don't know. I actually would be interested in understanding this, it is useful to just feed LaTeX into Wolfram. My guess here is that it does not digest those subscripts $x$. – Giuseppe Negro Nov 16 '22 at 19:28
  • @GiuseppeNegro it looks like your guess might be right, replacing a with a_x or a_{x} in my previously commented working example leads to wolframalpha interpreting them as function applications $a(x)$ – minseong Nov 16 '22 at 19:30
  • (2a_{x} - 2b_{x} + sqrt((-4a_{x} + 4g_{x})(a_{x} - 2b_{x} + c_{x}) + (-2a_{x} + 2b_{x})*2))/(2a_{x} - 4b_{x} + 2c_{x}) $$$$ This should be digestible by wolfram – Sam Nov 16 '22 at 19:32
  • @Sam how did you generate that? – minseong Nov 16 '22 at 19:35
  • I've written a bunch of python functions that go to and from latex-style math equations to regular math equations. Would you like me to write them out in an answer? – Sam Nov 16 '22 at 19:36
  • @Sam definitely that would be a perfect answer – minseong Nov 16 '22 at 19:43

1 Answers1

2

Here are some functions I use to convert $\LaTeX$ expressions to regular math expressions and vice versa:

from sympy import latex
from sympy import sympify
from sympy.parsing.latex import parse_latex
from pyperclip import copy

def LatexToMath(expr): # Input a string written in latex format. Have the math format copied to clipboard return copy(str(parse_latex(expr))) def MathToLatex(expr): # Input a string written in regular math format. Have the corresponding latex format copied to clipboard return copy(latex(sympify(expr, evaluate=False)))

Sam
  • 1,119