feat(math): Support TeX-like syntax for roots with degrees #2342
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #2225
Supporting
\sqrt[degree]{radicand}requires support from the parser due to the unusual syntax with a math expression between square brackets.This draft PR does work nicely for simple cases, e.g.
It's however insanely slow for deeply nested constructs, e.g.
(From Joe's MathML Browser Test, "nested roots" scenario)
This being said, the "MathML-like" approach (without any change needed to the current parser) suffers from the same problem. The simple cases works too in a timely manner.
But the deeply nested cases is also equally real slow (to a point where this is not acceptable).
Just to be clear, a very (very) long time is then spent in
convertTexlike()that just calls the LPEG/EPNF parser... (The time spent afterwards in the tree massaging for conversion to MathML is negligible).So the slowdown isn’t caused by the proposed "sqrt" parsing rule itself, but by the heavy recursion in our grammar overall. LPeg’s backtracking makes this cost grow exponentially with nesting depth --- the real bottleneck is likely the general deep recursion in "mathlist" constructs...1
Both KaTeX and MathJax are blazingly fast when it comes to render the long nested TeX example...2
So my suggestion would be to accept this PR (so that users would have the usual TeX-like construct support for regular cases, and it's not worse than the MathML-like workaround)...
... But perhaps should we open a dedicated for fully replacing our LPEG-based parser with something more efficient. Aye, this is a painful observation. (EDIT --> tracked as #2343)
Footnotes
For the record, the degree rule in
\sqrtas proposed in the PR uses a- P("]")on a large element pattern: this will cause some inefficient backtracking too. A faster approach there would be to to parse a shallow "mathlist" specifically for the degree. Still, the performance bottleneck is reached with nested\mroots or even\msqrts... So while the degree root is not of the best efficiency, it's also negligible in this scenario... ↩And Typst is real fast too with its own syntax
sqrt(1 + root(3, 2 + root(5, 3 + root(7, 4 + root(11, 5 + root(13, 6 + root(17, 7 + root(19, A))))))))-- Just sayin' ↩