mathjaxgollum-wiki

gollum-wiki mathjax.config.js use "<math>" and "</math>" to delimit


Gollum-wiki is up and running. For some math formulas I'd like to use "<math>" and "</math>" as delimiters.

Here is the full mathjax.config.js

window.MathJax = {
  tex2jax: {
    inlineMath:  [ ['$','$'], ['\\(','\\)'], ['<math>', '</math>'] ],
    displayMath: [ ['$$','$$'], ['\\[','\\]'], ['<math>', '</math>']  ],
    processEscapes: true
    },
  TeX: { extensions: ["autoload-all.js"] }
};

Here is the config.rb:


    :mathjax => true,

    #-----------------------------------------------------------------------------
    # Specify path to a custom MathJax configuration.
    # Default: mathjax.config.js file from repository root.
    #
    # Equivalent to --mathjax-config [FILE]

    :mathjax_config => 'mathjax.config.js',

The source file is a markdown (xx.md) file.

This works. Integration sign and all infinity sybols show up as expected:

\\[ s(t) = \int_{-\infty}^{+\infty} {F(f) e^{j 2 \pi f t} } dt \\]

This does not work:

<math>Avg = \sum_{}^{N} {x_i} </math>

The error message is:

Unexpected text node: 'Avg = \sum_{}^{N} {x_i}'

Solution

  • You can not use HTML tags as math delimiters for MathJax, so <math> is not legal in inlineMath or displayMath. The documentation indicates

    Note that the delimiters can’t look like HTML tags (i.e., can’t include the less-than sign), as these would be turned into tags by the browser before MathJax has the chance to run. You can only include text, not tags, as your math delimiters.

    Also, you can't use the same delimiter for both in-line and display math, as MathJax will not be able to tell which one you want it to use for a given expression.


    UPDATE:

    One issue with using <math>...</math> for TeX notation is that <math> already has a meaning in HTML, which is that its contents is MathML, not TeX, and MathJax already processes <math> as MathML delimiters. That is why you are getting the "unexpected text node" message.

    On the other hand, it is possible to provide a new pre-processor that looks for <math> tags and considers the contents to be TeX commands. Your configuration appears to be for MathJax v2 (the current version is 3.2.2, with 4.0 out in beta release), so here is a v2 configuration that includes a pre-processor that uses <math> to enclose in-line TeX math.

    window.MathJax = {
      tex2jax: {
        inlineMath:  [ ['$','$'], ['\\(','\\)'] ],
        displayMath: [ ['$$','$$'], ['\\[','\\]'] ],
        processEscapes: true
      },
      TeX: {
        extensions: ["autoload-all.js"]
      },
      AuthorInit() {
        MathJax.Extension.math2jax = {
          version: "2.7.9",
          
          PreProcess: function (element) {
            if (typeof(element) === "string") {element = document.getElementById(element)}
            if (!element) {element = document.body}
            const math = element.getElementsByTagName("math");
            for (i = math.length-1; i >= 0; i--) {
              this.ConvertMath(math[i]);
            }
          },
          
          ConvertMath(node) {
            const parent = node.parentNode,
            script = document.createElement("script");
            script.type = "math/tex";
            MathJax.HTML.setScript(script, node.textContent);
            parent.insertBefore(script, node);
            parent.removeChild(node);
          }
        };
        MathJax.Hub.Register.PreProcessor(["PreProcess", MathJax.Extension.math2jax], 3);
      }
    };
    

    It is not clear whether you wanted <math> to represent in-line or display math (it can't be both, as there is no way to tell which is which, unless there is some other marker that distinguishes the two). I've made the above configuration to work for in-line math, with the assumption that you are using other markup to make it look like displayed math (and are using \displaystyle within the math itself). If that's not the case, then more clarity about the format you actually have might make it possible to modify this to better handle your files.