MathJax icon indicating copy to clipboard operation
MathJax copied to clipboard

MathJax 4.0.0 cant render \rightleftharpoons in \ce

Open WizardMeow opened this issue 2 months ago • 2 comments

Issue Summary

 \(\ce{F^- + H2O \rightleftharpoons HF + OH^-}\)

This latex cant be render at v4. \mhchemrightleftharpoons not define.

At version 3.2.0 it will work.

Maybe should fix those arrow.

      mhchemleftrightarrows: '\\leftrightarrows',
      mhchemrightleftharpoons: '\\rightleftharpoons',
      mhchemRightleftharpoons: '\\rightleftharpoons',
      mhchemLeftrightharpoons: '\\leftrightharpoons'

Steps to Reproduce:

  1. Render the latex
Image

Technical details:

  • MathJax Version: 4.0.0
  • Client OS: (e.g., Mac OS X 10.8.4)
  • Browser: (e.g., Chrome 29.0.1547.57)

Supporting information:

  • Please supply a link to a (live) minimal example page, when possible.
  • If your issue is with the display of the mathematics produced by MathJax, include a screen snapshot that illustrates the problem, when possible.
  • Check your browser console window for any error messages, and include them here.
  • Include the MathJax configuration you are using, and the script tag that loads MathJax itself.

WizardMeow avatar Oct 20 '25 07:10 WizardMeow

In v3, the characters needed to make a number of the arrows used by mhchem were not available, and it created those characters using horrible hacks that were unreliable. In v4, which supports fonts and font extensions, we were able to add the needed characters to make all the arrows used in mhchem, and a post-processor looks for the horrible hacks and replaces them with regular characters. Because the font extension used for this is based on the mathjax-newcm arrows, if mhchem were being used with a different font (say the STIX2 font), these new extra characters would not match the other arrows in the formulas, and so even the arrows that were available in v3 are replaced by ones that use the same mathjax-newcm characters, for consistency. That is done by converting things like \rightarrow to \mhchemrightarrow, which maps to the newcm versions in the font extension.

The font extension includes versions for all the arrows used internally by mhchem when you use the usual arrow constructors like ->. The right-left harpoons are usual generated by <=> and produce the long version of that arrow. I didn't consider the fact that people would enter the arrows via explicit macro calls, since they are built into the mhchem notation. As a result, the pattern match that is being performed is over-aggressive and catches some arrows for which there is no mhchem version, as in your case.

I will make a PR to fix this, but in the meantime, there are several possible solutions.

The first is to use

\(\ce{F^- + H2O <=> HF + OH^-}\)

as the natural way to do this in mhchem.

Alternatively, you could define \mhchemrightleftharpoons incorporating

MathJax = {
  tex: {
    macros: {
      mhchemrightleftharpoons: '\u21CC',
    },
  },
};

into your Mathjax configuration (if you control that), or using

\(\let\mhchemrightleftharpoons = \rightleftharpoons\)` if you don't.

For a more comprehensive solution, you can use

MathJax = {
  loader: {
    '[tex]/mhchem': {
      ready() {
        const {MhchemReplacements} = MathJax._.input.tex.mhchem.MhchemConfiguration;
        const {MapHandler} = MathJax._.input.tex.MapHandler;
        const mhchemMacros = MapHandler.getMap('mhchem');
        const mhchemChars = MapHandler.getMap('mhchem-chars');
        MhchemReplacements.delete('\\mhchem$1');
        MhchemReplacements.set(
          (match, arrow) => {
            const mharrow = `mhchem${arrow}`;
            return mhchemChars.lookup(mharrow) || mhchemMacros.lookup(mharrow) ? `\\${mharrow}` : match;
          },
          /\\(x?(?:long)?(?:left|right|[Ll]eftright|[Rr]ightleft)(?:arrow|harpoons))/g,
        );
      }
    },
  },
};

as part of your configuration in order to have MathJax check that the mhchem version of the arrow is defined before converting it.

dpvc avatar Oct 20 '25 21:10 dpvc

PS, if you do end up still using the explicit \rightleftharpoons rather than <=>, then you may want to remove the space just before it in order to get the correct spacing around the arrows. It turns out the mhchem thinks that space is significant, so keeps it, producing more space on the left of the harpoons than on the right.

dpvc avatar Oct 20 '25 23:10 dpvc