Check to see if syntax is valid before typesetting?

8 views
Skip to first unread message

Jason Alexander

unread,
Jun 26, 2024, 9:44:36 AM (3 days ago) Jun 26
to mathja...@googlegroups.com
Hello,

I have a web page that dynamically typesets mathematical text as the user types. Is it possible to have MathJax check whether the submitted syntax is valid before starting to typeset? (I’m assuming — perhaps erroneously — that checking validity would be a lot faster than typesetting.)

I asked Claude.ai, which gave an example using MathJax.compile(), but the code snippet didn’t work so I’m wondering if the answer might have been a hallucination.

Many thanks,

Jason

Davide Cervone

unread,
Jun 26, 2024, 11:15:48 AM (3 days ago) Jun 26
to mathja...@googlegroups.com
Is it possible to have MathJax check whether the submitted syntax is valid before starting to typeset?  (I’m assuming — perhaps erroneously — that checking validity would be a lot faster than typesetting.)

The only real way of telling if a TeX or LaTeX expression is valid is to run TeX or LaTeX on it.  So really, the only thing to do is to actually typeset it and check if it produced an error or not.  There are several approaches to this.  One is given in issue #2414


which gives the idea of how to use the formatError() function together with MathJax.tex2svgPromise() or MathJax.tex2chtmlPromise() to trap the errors.  (Note that in v4.0.0-beta.x for any x from 1 to 6, you need to add MathJax.startup.promiseResolve() before the return statement if you are doing typesetting within the pageReady() function).  This allows you to trap the error for the typesetting of a single expression.  You can also use formatError() to process error for a whole page by not throwing an error but just reporting it.  Note that jax.latex gives the LaTeX for the given error, and jax.parseOptions.mathItem is the MathItem that caused the error.

Another approach is given in issue #2992


The example there was used to replace the errant expression with a different message text, but you could use that idea to identify all the error-producing expressions within a page.

I asked Claude.ai, which gave an example using MathJax.compile(), but the code snippet didn’t work so I’m wondering if the answer might have been a hallucination.

I'dd be interested to see what it came up with.  Can you share the code it produced?

Davide
Reply all
Reply to author
Forward
0 new messages