markdownjekyllmathjaxkramdown

Use MathJax with kramdown, without protecting in div's (as advertised!)


I'm trying to set up jekyll to display math properly.

The problem is nothing renders if I do not nest equations inside div tags to protect them from the markdown parser :(

---
layout: default
math:   true
---

# Using Mathjax

along markdown parsers is a real pain...

$$ a + b = c $$

doesn't show, though the following does:

<div> 
$$ x + y + c $$
</div>

The layout tests includes the following mathjax script through an include {% if page.math %}:

<script async src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_CHTML"></script>

I'm confused because:

There is already a lot on this topic, however much is outdated! Any help for a smooth and simple configuration would be greatly appreciated.


Solution

  • The problem is that kramdown's output is not compatible with mathjax v3 anymore.

    You can check that kramdown does its job at nesting $$ a + b = c$$ inside the recommended script tags:

    <script type="tex/math"> a = b + c </script> 
    

    However mathjax does not use these script tags anymore, as mentioned here (last bullet).

    So having kramdown work with mathjax either requires to:

    MathJax = {
      options: {
        renderActions: {
          find: [10, function (doc) {
            for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            }
          }, '']
        }
      }
    };
    

    Can't figure why this backward combatability instead of a simple option switch though...