jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
514 stars 82 forks source link

problem with converting from latex #601

Closed MAGNETO903 closed 3 years ago

MAGNETO903 commented 3 years ago

I want to convert this latex expression:

\frac{\pi\ \cdot\ u^{2\cdot\pi-1}}{5}\ +\ \frac{9\ \cdot\ \pi\ \cdot\ u^{\pi-1}}{10}

CodeCogsEqn (1) this works very strange

nerdamer.convertFromLaTeX("\frac{\pi\ \cdot\ u^{2\cdot\pi-1}}{5}\ +\ \frac{9\ \cdot\ \pi\ \cdot\ u^{\pi-1}}{10}").toString()
// it returns "10*9^pi^u^(-1+pi)*rac+5*pi^u^(-1+2*cdotpi)*rac"
jiggzson commented 3 years ago

@MAGNETO903, there are two issues in your case. The first issue is the fact that you're not escaping your backslashes. The second issue is that your expression has spaces which currently aren't supported anywhere except matrices as those in #584 and I'd have to think of a way to distinguish between the two. If you don't have matrices as those in the aforementioned issue then the easiest way forward is to remove the spaces yourself so this would become:

var latexExpr = '\\frac{\\pi\\ \\cdot\\ u^{2\\cdot\\pi-1}}{5}\\ +\\ \\frac{9\\ \\cdot\\ \\pi\\ \\cdot\\ u^{\\pi-1}}{10}';

// Remove spaces
latexExpr = latexExpr.replace(/\\\s+/g, ' ');

var expression = nerdamer.convertFromLaTeX(latexExpr).toString()

console.log(expression);

I'll revisit this issue in the future to see if this can be handled internally.