代码之家  ›  专栏  ›  技术社区  ›  Kai Pommerenke

如何配置MathJax(AsciiMath)以允许数千个分隔符

  •  1
  • Kai Pommerenke  · 技术社区  · 7 年前

    1,000/5 等于1000/5,其中分数的分子只显示为000,而不是1000。

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

    下面的堆栈溢出答案回答了TeX输入的这个问题,但我需要它来进行AsciiMath格式的输入,并且无法通过在配置文件中将“TeX”更改为“AsciiMath”使其工作: mathjax commas in digits

    任何帮助都将不胜感激。非常感谢。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Davide Cervone    7 年前

    背景

    decimal: ','
    

    告诉AsciiMath使用欧洲数字格式,该格式使用逗号作为小数点分隔符,而不是句点。这就是为什么你不再将“0.12”视为一个数字。AsciiMath没有每三位解析逗号的机制。

    我建议最好使用一个AsciiMath预过滤器来预处理AsciiMath,以便在AsciiMath解析表达式之前删除逗号。添加以下内容

    <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>
    

    到页面仅 之前 加载MathJax的脚本。js应该做到这一点。注意,这意味着逗号也不会出现在输出中;没有自然的方法可以做到这一点,除非您想在所有具有4位或更多数字的数字中添加逗号(即使它们的开头没有逗号)。这将需要一个后过滤器返回生成的MathML,并将数字转换为具有逗号的数字。类似于:

    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);
      });
    });
    

    应该有效。

        2
  •  2
  •   Peter Krautzberger    7 年前

    遗憾的是,没有正确记录AsciiMath配置选项。

    您可以通过指定

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

    在MathJax配置块中。

    注意,这会导致(例如,(1,2))的各种解析复杂化。

    http://docs.mathjax.org/en/latest/options/input-processors/AsciiMath.html

    推荐文章