Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Okay, this has been a very frustrating adventure for me. I have spent many hours over several successive days trying to get MathJax to enable and recognize the mhchem extension within a Markdown cell in ipython notebook. Math expressions worked fine, but the mhchem macros \ce \cf \cee just go unrecognized. I have tried enabling the extension in the MathJax/config/default.js file. I tried putting the following script code with the Markdown window

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

I have tried every trick and tip I could find on the internet. Nothing seems to work.

I am running debian wheezy. I thought maybe the default mhchem and MathJax packages were causing the problem, so I installed a custom version of MathJax for ipython notebook into my profile_default/static directory and configured ipython to use this. Again, math fine, chemistry a no go. I am able to use the mhchem extensions directly from TeX but MathJax just refuses to load the extension or recognize the \ce tags.

I am stumped!

Anyone have any ideas?

like image 786
user3195384 Avatar asked Jan 14 '14 19:01

user3195384


3 Answers

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

like image 110
user3195384 Avatar answered Oct 17 '22 19:10

user3195384


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.

like image 40
Mike Avatar answered Oct 17 '22 18:10

Mike


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

like image 2
Davide Cervone Avatar answered Oct 17 '22 19:10

Davide Cervone