Question

How do you enable MathJax extension mhchem in Sphinx? MathJax is loaded from CDN.

Was it helpful?

Solution

Add the following block into your rst file. Or see Adding a javascript script tag some place so that it works for every file in sphinx documentation to add it into your template, so it can be applied globally.

.. raw:: html

    <script type="text/javascript" >
    MathJax.Hub.Config({
        TeX: { extensions: ["mhchem.js"] }
    });
    </script>

OTHER TIPS

To solve this problem for all rst files, I 'subclassed' mathjax... AKA, I made another js file in my _static directory that would load mathjax then manually add the extension I needed (cancel in my case). Then I specified that path for the mathjax_path option in the conf.py. The contents of mathjax_config.js are:

// Dynamically load script then call helper when script has loaded.
function dynamicallyLoadScript(url, helper) {
    var script = document.createElement("script"); // Make a script DOM node
    script.src = url; // Set it's src to the provided URL

    document.head.appendChild(script); // Add it to the end of the head section of the page (could change 'head' to 'body' to add it to the end of the body section instead)
    script.onreadystatechange= function () {
      if (this.readyState == 'complete') helper();
     }
     script.onload= helper;
}

// Configure MathJax
function mathjax_config() {
  MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  MathJax.Hub.Insert(MathJax.InputJax.TeX.Definitions.macros,{
    cancel: ["Extension","cancel"],
    bcancel: ["Extension","cancel"],
    xcancel: ["Extension","cancel"],
    cancelto: ["Extension","cancel"]
    });
  });
}
var mathjax_url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
dynamicallyLoadScript(mathjax_url, mathjax_config)

Since Sunhwan Jo's answer hasn't worked for me, here's my attempt to address this:

.. math::

   \require{mhchem}
   \ce{E + S <=> ES -> ES* -> E + P}

I think it's simple and elegant. Besides, you don't need to call \require{mhchem} more than once in a single .rst file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top