mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Parsing TeX to math.js

Open Lakedaemon opened this issue 10 years ago • 7 comments

It would be nice, if at some point math.js could parse a subset of TeX into a node tree.

There is absolutely no urgency and it might be possible to take inspiration/use code from other projects for that :

MathJax manages with success to parse a lot of TeX commands and to coerce it into mathml (content and presentationnal).

Some possible applications of such a parser :

  1. testing the toTex function and the fromTeX function (roundtrip)
  2. getting input from users in the TeX language
  3. import/reuse available snipets of TeX maths
  4. data exchange math.js <-> TeX <-> MathMl ...

Lakedaemon avatar Mar 23 '15 20:03 Lakedaemon

Thanks for your suggestion!

josdejong avatar Mar 23 '15 20:03 josdejong

I would also like this feature in mathjs, it would be very nice and useful for users who needs a simpler way of typing a mathematical expressions. In this way you might be able to let users type an expression with for example mathquill and parse it to maths js and make it computable.

websimon avatar May 12 '15 09:05 websimon

hmm I was looking for a feature like this

Kreijstal avatar Mar 23 '19 10:03 Kreijstal

@Kreijstal I also needed to convert LaTeX to MathJS. For my purposes, it has been good enough to:

  1. replace all fractions: '\frac{a}{1 + \frac{b}{c}}' becomes '{a}/{1 + {b}/{c}}'
  2. replace some names
    • '{' becomes '('
    • '\cdot' becomes '*'
    • etc

Then the resulting string can be sent to MathJS's parser. This certainly isn't perfect, but it's covered all the use-cases I need.

Here's links to my code. It's currently written for Babel with Flow annotations, but wouldn't be too hard to strip those:

ChristopherChudzicki avatar Mar 23 '19 15:03 ChristopherChudzicki

@Kreijstal I also needed to convert LaTeX to MathJS. For my purposes, it has been good enough to:

  1. replace all fractions: '\frac{a}{1 + \frac{b}{c}}' becomes '{a}/{1 + {b}/{c}}'

  2. replace some names

    • '{' becomes '('
    • '\cdot' becomes '*'
    • etc

Then the resulting string can be sent to MathJS's parser. This certainly isn't perfect, but it's covered all the use-cases I need.

Here's links to my code. It's currently written for Babel with Flow annotations, but wouldn't be too hard to strip those:

@ChristopherChudzicki needing to do the same. would love to take a look at what you built if you can share that project again.

magnum6actual avatar May 26 '20 20:05 magnum6actual

For anyone still looking for a solution to this, I wrote a library here: https://github.com/davidtranhq/tex-math-parser that parses TeX strings into MathJS expression trees.

davidtranhq avatar Nov 12 '21 20:11 davidtranhq

Nice, thanks for sharing @davidtranhq !

josdejong avatar Nov 27 '21 18:11 josdejong