How do I get MathJax to enable the mhchem extension in ipython notebook

喜你入骨 提交于 2019-11-29 03:57:27
user3195384

Thanks for your answer. I had tried loading the script with an without the Tex/. I had tried /ce \ce. Just about everything imaginable. I finally found a solution and it was as follows:

If I use the 'non-standard' /require macro within a math expression to force load the mhchem extension everything works great.

I added the following code at the top of the Markdown cell

$$\require{mhchem}$$       

Strangely, once I have done this in the first line of the first markdown cell, it seems to work flawlessly throughout the notebook. Even from within a code page where I was executing the following to test the mhchem extensions:

from IPython.display import display, Math, Latex                                                                                                                                                                                             
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))                                                                                                                                                                     
display(Math(r'\ce{H2O}'))   

Without the /require macro, the above code would generate the math function correctly but the chemistry formula would simply be rendered as '\ceH2O'

By including the /require line, all mhchem usage in the notebook seems to work fine.

Also, I should note that I was running the ipython packages installed from the Debian Jesse repositories. Turns out that is still 0.13. In the end, I removed these packages and installed 1.10 directly using the setuptools. The MathJax worked out of the box with this install.

Anyway, hopefully, this will save some other poor chemistry user some frustration

The preferred way to do this, if you want it for every notebook you create, is to put it into your ipython profile's custom.js file. To be specific, just put the following in that file:

/* Add some extensions to mathjax */
MathJax.Hub.Config({
    TeX: {
        extensions: ["mhchem.js"]
    },
});

If you don't know where to find the file, you can run

ipython profile locate

on the command line. It should be in static/custom/ under that directory. (I think if your ipython profile is too old, you might need to create static/custom/ and the file.)

My own custom.js has lots more in it, like the extensions

"AMSmath.js", "AMSsymbols.js", "autobold.js",

and other mathjax options, as well as custom keyboard shortcuts, etc. So there's a lot that can be done.

First, notes that the commands are \ce, \cf, and \cee, not /ce, /cf, and /cee, so if you have typed the latter, that certainly would be one reason that they don't do what you expect.

Also, you have not given your complete configuration nor how you are loading MathJax.js, so it is not clear whether what you have done will be effective or now. But if your HTML page includes

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: {extensions: ["mhchem.js"]}
});
</script>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

then that should do it for you. Note in particular that you don't need to use TeX/ in front of mhchem.js since your extension array is part of the TeX block, and MathJax knows to look in the TeX directory for those. Finally, if you use config=TeX-AMS_HTML (or one of the other configuration files), then default.js is not loaded, so has no effect.

If this doesn't answer your question, please post more details about how you have loaded MathJax (I'm afraid I don't know how ipython notebook handles it).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!