I use jupyter notebook, as part of anaconda navigaor, in order to follow along data science tutorials, to make scientific reports, to make presentations with nbconvert, etc. It is really neat and very useful. For some reason, I’d prefer if the mathjax engine within jupyter rendered math equations as svg and not as span which it does by default. I have done some research in order to change this which I explain here.
First, having statred a jupyter notebook in chrome, I opened View > Developer > View Source . By looking at the html markup, the mathjax javascript get fetched locally
which after some research turns out to be in my anaconda distribution folder (on mac) /anaconda3/lib/python3.7/site-packages/notebook/static/components/MathJax. By looking at /anaconda3/lib/python3.7/site-packages/notebook, I realized that jupyter notebook is actually a django web application, thereofore any customization of the app has to be done there. I don’t want to go down that road so not to break anything.
Doing more research, for instance through View > Developer > Developer Tools then clicking the network tab and reloading the notebook (⌘+R in mac) followed by a search for mathjax or mathjax.hub.config,
we can find any fetched ressource with the keyword mathjax in it. Looking inside these javascript ressources, one finds out about the window.MathJax global variable and how everything mathjax is stored in it. Type window.MathJax on chrome console to see for yourslef.
Enough with mathjax investigation, let’s go back to our problem. One harmless way I use to set SVG as MathJax output format for an opended notebook (after mathjax has finished loading and typesetting) is by typing and executing the following javascript snippets on the console:
delete (window.MathJax);
var head = document.getElementsByTagName("head")[0]; var script;
script = document.createElement("script"); script.type = "text/x-mathjax-config"; script[(window.opera ? "innerHTML" : "text")] = "MathJax.Hub.Config({\n" + "HTML: ['input/TeX', 'output/SVG'],\n" + "jax: ['input/TeX', 'output/SVG'],\n"+ "tex2jax: {\n"+ "inlineMath: [ ['$','$'], ['\\(','\\)'] ],\n"+ "displayMath: [ ['$$','$$'], ['\\[','\\]'] ],\n"+ "processEscapes: true,\n"+ "displayAlign: 'center',\n"+ "}\n"+ "});\n"+ "MathJax.Hub.Configured();\n"+ "MathJax.Hub.Queue(['Typeset',MathJax.Hub]);\n" head.appendChild(script);
script = document.createElement("script"); script.type = "text/javascript"; script.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_SVG&delayStartupUntil=configured"; head.appendChild(script);
Basically, we have deleted the global variable window.MathJax, then fetched mathjax from a cdn and configured it to output SVG. The method is inspired from Loading MathJax Dynamically.
This is a gross hack, however it has the merit of working and of not being persistent, a reload of the notebook (⌘+R in mac) gets you back to default mathjax configuration.