KaTeX: Katex does not parse but MathJax does
So we have an equation with _ (underscores) in the name like:
\text{Score} = 2\times\text{(Protein_(g))} - 0.75\times\text{(Lipid_Tot_(g))}
And in mathjax it renders it just fine (url). However as seen if one uses https://khan.github.io/KaTeX/, Katex yields this error:
KaTeX parse error: Expected '}', got '_' at position 37: …s\text{(Protein_̲(g))} - 0.75\…
Do we know why, and if so should we fix Katex or is mathjax wrong?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (9 by maintainers)
@pkra thanks linking to the relevant MathJax issue. I’m glad to hear that you’re considering making this configurable in v3. In the interest of improving interoperability KaTeX could add the current MathJax behaviour and make it configurable as well. For KaTeX though, I think we’d probably want to make our current behaviour the default. The rationale being that there’s probably more TeX code in the world that relies on the TeX behaviour than than the current MathJax behaviour.
A relevant issue would be https://github.com/mathjax/mathjax-v3/issues/135.
Before going all xkcd#386, you might consider that are valid reasons to avoid text mode (e.g., it is essentially a different layout system, also incompatible with CSS). Some are mentioned at https://docs.mathjax.org/en/latest/tex.html#differences.
If you are interested in thinking about the larger community, you might consider starting a discussion on the W3C MathOnWeb community group to work towards better compatibility across TeX-like conversion tools. (Disclaimer: I co-chair the CG.)
While we maybe can’t change MathJax’s definition of
\text{_}, I think we could file a bug that macros like\_or\textunderscoredon’t work in\text{...}. Also, for example, text accents (\text{\'e}), text font commands (\text{\emph{hello}},\text{\textit{italic}}) won’t work. Everything inside\text, except$, seems to be treated verbatim.This is the first big feature I’ve noticed missing in MathJax – a good argument for using KaTeX.
As has been pointed out, this has been an issue for MathJax users, and so I have capitulated and put together an extension to allow processing (some) text-mode macros in
\text{}and other text-mode settings. Perhaps that will make things more compatible.