mathjax/MathJax-demos-node

Force MathJax to try to render the equation even though there are syntax errors (and ideally: to automatically fix the errors it self)

netw0rkf10w opened this issue · 8 comments

Hello,

When using direct/tex2chtml-page, if there are LaTeX syntax errors in a formula, then the original code is displayed (and highlighted):

Screenshot 2024-01-23 at 18 43 04

I would like to force MathJax to try to render the formula as best as it can, similarly to the results obtained from the MathJax demo page:

Screenshot 2024-01-23 at 18 45 01

So my first question is: How can we modify tex2chtml-page to obtain similar results than the above MathJax demo?

Moreover, when inspecting the output HTML, I see that MathJax knew exactly what was wrong:

<merror data-mjx-error="\ldots is only supported in math mode" title="\ldots is only supported in math mode"><mtext> \{(x_{1},\ldots,x_{d}) : \mbox{ $a_{i} &lt; x_{i} \leq b_{i}$ for $i = 1$, \ldots, $d$}\}. </mtext></merror>

A simple fix seems to be just enclosing \ldots with the dollar signs. Can MathJax do that?

The complete example HTML file for your convenience:

<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
  <title>Test</title>
 </head>
 <body>
    $$ \{(x_{1},\ldots,x_{d}) : \mbox{ $a_{i} &lt; x_{i} \leq b_{i}$ for $i = 1$, \ldots, $d$}\}. $$
 </body>
</html>

Thank you very much in advance for your help!

dpvc commented

MathJax does not have a means of trying to correct your incorrect LaTeX to try to process it further. When it finds an error it stops, and produces the error; trying to resolve the error is outside the scope of MathJax's mission.

Normally, MathJax will display the error message (like "\ldots is only supported in math mode"), but there is an extension (the noerrors extension) that causes the original LaTeX code to be shown instead. Because text2chtml-page loads (nearly) all the extensions, it loads noerrors, which is why you get the LaTeX rather than the message.

The reason you get a different result on MathJax's demo page is again because tex2chtml-page loads all extensions. IN this case, it is the textmacros extension that causes the difference. By default, MathJax doesn't process most macros in text-mode, and so something like \mbox{a \ldots b} would produce a \ldots b literally, without trying to process \ldots. That is what is happening in the demo page. But with the textmacros package, macros are processed in text-mode when possible, but this means that unknown or unsupported macros will cause errors. That is what is occurring in your case.

It is possible to cause unsupported macros in text-mode to be treated as unknown ones, and display them in red. If you add

const {Configuration} = require('mathjax-full/js/input/tex/Configuration.js');
const {TextMacrosMethods} = require('mathjax-full/js/input/tex/textmacros/TextMacrosMethods.js');

Configuration.create('my-text-macros', {
  fallback: {
    macro: (parser, name) => {
      const texParser = parser.texParser;
      const macro = texParser.lookup('macro', name);
      if (!macro || macro._func === TextMacrosMethods.Macro) {
        texParser.parse('macro', [parser, name]);
        return;
      }
      const textNode = parser.create('text', '\\' + name);
      const options = parser.options.noundefined || {color: 'red', background: '', size: ''};
      const def = {};
      for (const id of ['color', 'background', 'size']) {
        if (options[id]) {
          def['math' + id] = options[id];
        }
      }
      parser.Push(parser.create('node', 'mtext', [], def, textNode));
    }
  },
  priority: 1
});

to the tex2chtml-page file, and change

const tex = new TeX({packages: argv.packages.split(/\s*,\s*/), inlineMath: [['$','$']]});

to

const tex = new TeX({
  packages: argv.packages.split(/\s*,\s*/),
  inlineMath: [['$','$']],
  textmacros: {
    packages: {'[+]': ['my-text-macros']}
  }
});

Then you should get a red \ldots instead of the error. Note, however, that this only resolves this one type of error message. Other errors (like missing close braces, or incorrect number of arguments, etc.) will still cause errors. MathJax won't try to continue past those.

@dpvc Thank you so much for the detailed explanation and for the macro! Greatly appreciated!

Regarding the error correction, I admit that it was too much to ask :D
Just to give a bit of context, I'm using MathJax to process a lot of ePubs (fixing their ugly image-based math formulas), and found that the authors made quite a lot of syntax errors similar to the above in their LaTeX code. What is strange is that the image equations still look good, and so do the ones in the original PDF. Then, I realized that this is due to the behavior of LaTeX compilers. Checking with my local TeXlive, I found that despite some compilation errors (Missing $ inserted), the final results are good (the \ldots is correctly displayed despite not being in math mode). "There are some errors but I will try my best to render as much as I can". I expected MathJax to behave the same, that's why I asked the question. Voilà just some explanation for my unreasonable request.

dpvc commented

Yes, TeX itself does try to continue on in some circumstances (though not all), but my experience is that such attempt most often fail miserably and just generate further errors.

In real LaTeX, \ldots actually is allowed in text mode, which is why the image is fine in that case. MathJax's text mode is more limited; I suppose it would be possible to make \ldots available, though I'm not sure it is worth it.

Thanks for the reply @dpvc!

Yes, TeX itself does try to continue on in some circumstances (though not all), but my experience is that such attempt most often fail miserably and just generate further errors.

I totally agree with you on this!

In real LaTeX, \ldots actually is allowed in text mode, which is why the image is fine in that case.

I thought \ldots caused the same error in LaTeX but now that you mentioned this, I've checked again and it turns out that I have to apologize to the authors because their code does compile without errors. In my previous test above, I obtained an error because I mistakenly used the standalone document class (this question has more details).

MathJax's text mode is more limited; I suppose it would be possible to make \ldots available, though I'm not sure it is worth it.

I think it would be desirable for equations in MathJax to behave the same way as in real LaTeX (of course we are not talking about document typesetting as a whole but only about equations). However, I also think that this feature is too minor to spend your precious time on, given that you seem to be the only developer of MathJax at the moment.

(By the way, talking about development, I believe that the single most important feature (and by a large margin) that MathJax is still lacking, is being fully responsive.)

dpvc commented

I believe that the single most important feature (and by a large margin) that MathJax is still lacking, is being fully responsive.

I'm not sure I understand what you are asking for. Version 4 (now in beta release) has automatic line breaking for both display and inline expressions. The inline expressions will break dynamically if the container changes, though display equations will not. Is that the sort of thing you are talking about?

dpvc commented

I also think that this feature is too minor to spend your precious time on, given that you seem to be the only developer of MathJax at the moment.

There are actually two of us, but Volker is mainly in charge of the speech-rule engine that underlies the assistive support, and that is developed in a separate repository. I have been the primary developer since the beginning, and we have really only had two other people on the project for a while in the past.

@dpvc Thanks a lot!

I'm not sure I understand what you are asking for. Version 4 (now in beta release) has automatic line breaking for both display and inline expressions. The inline expressions will break dynamically if the container changes, though display equations will not. Is that the sort of thing you are talking about?

The last time I checked, MathJax was still not fully responsive, please see mathjax/MathJax#3027. I haven't been following the progress since then, but it it's been fixed then that'd be awesome news!!

dpvc commented

Sorry, I'm not sure I understand what you mean by "fully responsive". If you are talking about the various overflow options that are listed in the issue you cite, all by elide are supported in v4. I believe the issue with the semantic enrichment has been resolved, but I'm not sure if it is in beta.4 or just in the develop branch at the moment.

If you have something other than that in mind, can you be more explicit about what you are looking for?