How to configure MathJax (AsciiMath) to permit thousands separators

660 Views Asked by At

MathJax with AsciiMath renders the expression 1,000/5 as 1, 000/5, where the numerator of the fraction just shows as 000 instead of 1,000.

JSFiddle: https://jsfiddle.net/kai100/wLhbqkru/

The MathJax documentation is silent about thousands separators.

The Stack Overflow answer below answers this question for TeX input, but I need it for input in AsciiMath format, and have not been able to make it work by changing "Tex" to "AsciiMath" in the config file: mathjax commas in digits

Any help would be much appreciated. Thank you.

2

There are 2 best solutions below

0
On BEST ANSWER

Setting

decimal: ','

tells AsciiMath to use the European number format, which uses a comma as the decimal place separator rather than a period. That is why you are not longer seeing "0.12" treated as a number. AsciiMath does not have a mechanism of parsing commas every three digits.

The best I can suggest is use a AsciiMath pre-filter to pre-process the AsciiMath to remove the commas before AsciiMath parses the expression. Adding something like

<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("AsciiMath Jax Ready", function () {
  function removeCommas(n) {return n.replace(/,/g,'')}
  MathJax.InputJax.AsciiMath.prefilterHooks.Add(function (data) {
    data.math = data.math.replace(/\d{1,3}(?:\,\d\d\d)+/g,removeCommas);
  });
});
</script>

to the page just before the script that loads MathJax.js should do the trick. Note that this will mean the commas don't appear in the output, either; there is no natural way to do that, unless you wanted to add commas to ALL numbers that have 4 or more digits (even if they didn't have commas to start with). That would require a post-filter to go back over the MathML that is produced and convert the numbers to having the commas. Something like:

MathJax.Hub.Register.StartupHook("AsciiMath Jax Ready", function () {
  function removeCommas(n) {
    return n.replace(/,/g,'');
  }
  function addCommas(n){
    return n.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  }
  function recursiveAddCommas(node) {
    if (node.isToken) {
      if (node.type === 'mn') {
        node.data[0].data[0] = addCommas(node.data[0].data[0]);
       }
    } else {
      for (var i = 0, m = node.data.length; i < m; i++) {
        recursiveAddCommas(node.data[i]);
      }
    }
  }
  MathJax.InputJax.AsciiMath.prefilterHooks.Add(function (data) {
    data.math = data.math.replace(/\d{1,3}(?:\,\d{3})+/g, removeCommas);
  });
  MathJax.InputJax.AsciiMath.postfilterHooks.Add(function (data) {
    recursiveAddCommas(data.math.root);
  });
});

should work.

1
On

The AsciiMath configuration options are unfortunately not properly documented.

You can specify this via

//...
   AsciiMath: {
         decimal: ","
   },
//...

in your MathJax configuration block.

Note that this causes various parsing complications for (e.g., (1,2) ).

For completion, the documentation is at http://docs.mathjax.org/en/latest/options/input-processors/AsciiMath.html