DEV Community

Richard Vodden
Richard Vodden

Posted on • Originally published at vodden.com

MathJax and Hugo

The is a marvelous article which explains how to use Hugo and MathJax in close harmony. Unfortunately it was written a while ago and only works for MathJax 2.x. This article updates the content of the original to MathJax > 3.1. The original is still worth reading as it nicely explains the functionality.

Firstly MathJax > 3.0 likes to have pollyfill. So the script inclusion should look like this:

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
Enter fullscreen mode Exit fullscreen mode

The solution to the problem of having to escape many characters in LaTeX markup is solved in a simlar manner, however a combination of Hugo now using the Goldmark renderer and the MathJax API changing quite significantly at version 3.0, it largely needs re-writing. Firstly the script to add the class to the code blocks now looks like this:

<script>
  window.MathJax = {
    tex: {
      inlineMath: [['{% katex inline %}','{% endkatex %}'], ['\\(','\\)']],
      displayMath: [['{% katex inline %}{% endkatex %}','{% katex inline %}{% endkatex %}'], ['\[','\]']],
      processEscapes: true,
      processEnvironments: true
    },
    options: {
      skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre']
    },
    startup: {
      pageReady() {
        return MathJax.startup.defaultPageReady().then(function () {
          var all = window.MathJax.startup.document.getMathItemsWithin(document.body), i;
          for(i = 0; i < all.length; i += 1) {
            console.log(all[i])
            all[i].start.node.parentNode.className += ' has-jax';
          }
        });
      }
    }
  };
  </script>
Enter fullscreen mode Exit fullscreen mode

Now, by default, the Goldmark renderer does not permit raw html in markdown, instead converting it to a comment. To change this behaviour the Hugo configuration needs the following code added:

[markup]
  [markup.goldmark]
    [markup.goldmark.renderer]
      unsafe = true
Enter fullscreen mode Exit fullscreen mode

Now, LaTeX can be included in <div> tags without worrying about having to escape either _ or \ characters. The following code:

<div>
{% katex inline %}{% endkatex %}
\begin{align*}
  \phi(x,y) &= \phi \left(\sum_{i=1}^n x_ie_i, \sum_{j=1}^n y_je_j \right) \\
  &= \sum_{i=1}^n \sum_{j=1}^n x_i y_j \phi(e_i, e_j) \\
  &= (x_1, \ldots, x_n) \left( \begin{array}{ccc}
      \phi(e_1, e_1) & \cdots & \phi(e_1, e_n) \\
      \vdots & \ddots & \vdots \\
      \phi(e_n, e_1) & \cdots & \phi(e_n, e_n)
    \end{array} \right)
  \left( \begin{array}{c}
      y_1 \\
      \vdots \\
      y_n
    \end{array} \right)
\end{align*}
{% katex inline %}{% endkatex %}
</div>
Enter fullscreen mode Exit fullscreen mode

renders as:

\begin{align*}
\phi(x,y) &= \phi \left(\sum_{i=1}^n x_ie_i, \sum_{j=1}^n y_je_j \right) \
&= \sum_{i=1}^n \sum_{j=1}^n x_i y_j \phi(e_i, e_j) \
&= (x_1, \ldots, x_n) \left( \begin{array}{ccc}
\phi(e_1, e_1) & \cdots & \phi(e_1, e_n) \
\vdots & \ddots & \vdots \
\phi(e_n, e_1) & \cdots & \phi(e_n, e_n)
\end{array} \right)
\left( \begin{array}{c}
y_1 \
\vdots \
y_n
\end{array} \right)
\end{align*}

Top comments (0)