Search code examples
githubjekyllgithub-pagesequationmathjax

MathJax equation numbers do not show using Jekyll on GitHub Pages


This is my mathjax.html:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
    tex: {
      equationNumbers: { autoNumber: "AMS" },
      tagSide: "right"
    },
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
      processEscapes: true
    }
  });
</script>
<script type="text/javascript"
  charset="utf-8"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

It is being included in head.html in the _includes\ folder

{% include mathjax.html %}

which is in turn referenced in default.html in the _layouts\ folder

{%- include head.html -%}

When I then use \label{eq:xyz} and \ref{eq:xyz} or \eqref{eq:xyz} in a Markdown document on the Jekyll website deployed via GitHub Pages,

  • equations are not being numbered
  • equation references appear as "(???)"

Example:

$$ \label{eq:MSE}\tag{1}
\mathrm{MSE}(\hat{\theta}) = \mathrm{Var}(\hat{\theta}) - \mathrm{Bias}(\hat{\theta},\theta)^2
$$
(...)
\eqref{eq:MSE}

I have tried virtually any MathJax configuration I could find on the internet, but to no avail. The only way I get it working is to add \tag{1}, \tag{2}, \tag{3}, ... after the label in each equation and even then, equations using \begin{split} ... \end{split} still remain unnumbered. Who can tell me what's going wrong?


Solution

  • With autoNumber: "AMS" only certain equation environments are numbered automatically. In particular, math delimited by $$...$$ are not numbered automatically; you would need to use \begin{equation}...\end{equation} (or one of the other numbered alignment environments) for that. So using \tag{} is the only way to get an equation number for an equation using $$...$$.

    If you want double-dollar delimited display equations to be numbered, use autoNumber: 'all'. This will make every displayed equation have a number, not just the AMS numbered environments.

    Note also that the split environment does not provide equation numbering; it is for use within an outer numbered environment.


    Edit: It turns out that your issue is the capitalization of the TeX configuration block. It should be TeX: not tex: (in version 2, which is what you re using), so your TeX configuration wasn't being found by the TeX input jax, and so wasn't being used. Here is a working example:

    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        TeX: {
          equationNumbers: { autoNumber: "AMS" },
          tagSide: "right"
        },
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"] ],
          displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
          processEscapes: true
        }
      });
      MathJax.Hub.Register.StartupHook("TeX AMSmath Ready", function () {
        MathJax.InputJax.TeX.Stack.Item.AMSarray.Augment({
          clearTag() {
            if (!this.global.notags) {
              this.super(arguments).clearTag.call(this);
            }
          }
        });
      });
    </script>
    <script type="text/javascript" charset="utf-8"
      src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS_CHTML">
    </script>
    
    <p>
    \begin{equation}
    \label{eq:square}
    \begin{split}
       (x+1)^2 &= (x+1)(x+1)\\
               &= x^2 + 2x + 1
    \end{split}
    \end{equation}
    </p>
    
    <p>
    Link to equation \eqref{eq:square}
    </p>

    I've also changed the CDN to not be the decommissioned cdn.mathjax.org, and changed the config file to one that just loads TeX (not MathML) and uses the CommonHTML output rather than the older (and slower) HTML-CSS output. Finally, I removed unneeded pieces of your configuration (the extensions and jax arrays, since these are already included in the combined configuration file TeX-AMS_CHTML.