algebra.js
algebra.js copied to clipboard
Parse from latex
I am really impressed with this project.
I'm in need of a way of parsing latex, to the string format used here. It's already possible to parse to latex, so I'm not sure if the other way is possible too. I looked at Jison and stuff, but I'm in way over my head here.
The string parsing in algebra.js is split into 2 steps:
- The lexical analysis of the input string, generating a token stream representing the input:
"2 + 2"is transformed to{type:'Number', value: 2, pos:0},{type:'Operator', value: 'PLUS', pos:2},{type:'Number', value: 2, pos:4} - The syntactic analysis (the parser) of the token stream, generating Equations, Expressions, etc..
{type:'Number', value: 2, pos:0},{type:'Operator', value: 'PLUS', pos:2},{type:'Number', value: 2, pos:4}is transformed tonew Expression(2).add(2)
The parser is independent of the used input format, as it operates on the generated token stream. Therefore, for the support of a new input format e.g. LaTeX, the existing lexer has to be modified or an additional lexer has to be written.
In general, I don't think LaTeX is the best input format as it's purpose is to encode how the input is displayed and not what it represents.
For example: In LaTex the multiplication of two elements can be written in various ways
x \cdot 2, x * 2, x \times 2, 2x.
Also, how would you treat annotated characters like \hat{a}, \dot{a}, \grave{a} ?
To write a lexer that accepts basic LaTeX as input is not too difficult. You could give it a try with the existing lexer as a template or I could write one if others also see a use for it ;)
TL;DR: I think LaTeX is not an appropriate input format but a lexer for it would be not too hard to write.
Thanks for that elaborate reply.
I agree that latex probably isn't the best input format, but I'm getting latex formatted formulas returned from some handwriting recognition service I'm using.
If you could make an example file I could look and and expand, that would be nice.
I made a package that can parse latex to algebra.js link.
Woah awesome @viktorstrate !!! Will get this added to the docs.
Cool 🎉 Thank you ^^
that would be amazing and thank you @viktorstrate