【问题标题】:MathJax extension mhchem in Sphinx狮身人面像中的 MathJax 扩展 mhchem
【发布时间】:2013-10-15 09:53:14
【问题描述】:

如何在Sphinx 中启用MathJax 扩展mhchem? MathJax 是从 CDN 加载的。

【问题讨论】:

    标签: python-sphinx mathjax


    【解决方案1】:

    将以下块添加到您的第一个文件中。或查看Adding a javascript script tag some place so that it works for every file in sphinx documentation 将其添加到您的模板中,以便全局应用。

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

    【讨论】:

    • 当我直接添加这个时,我得到Uncaught ReferenceError: MathJax is not defined。所以看起来 Sphinx 默认没有定义 MathJax,即使我可以使用它。更具体地说,不能保证在运行原始 html 之前加载 MathJax 脚本。
    【解决方案2】:

    为了解决所有 rst 文件的这个问题,我对 mathjax 进行了“子类化”...AKA,我在我的 _static 目录中创建了另一个 js 文件,该文件将加载 mathjax,然后手动添加我需要的扩展名(在我的情况下取消)。然后我在 conf.py 中为mathjax_path 选项指定了该路径。 mathjax_config.js 的内容是:

    // 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)
    

    【讨论】:

      【解决方案3】:

      由于Sunhwan Jo's answer 对我不起作用,我尝试解决这个问题:

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

      我认为它简单而优雅。此外,您无需在单个.rst 文件中多次调用\require{mhchem}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-03
        • 2010-11-28
        • 1970-01-01
        相关资源
        最近更新 更多