How do I configure mathjax for iPython notebooks?

I recently had the exact problem. I really don't like the default STIX-Web font to render equation. After experimenting for a little while, I found a way to change the MathJax font in Jupyter Notebook. My Jupyter Notebook version is 4.3.1 and it is shipped with Anaconda. I assume the solutions for other versions should be similar.

I tried to edit custom.js both in /notebook/static/custom/custom.js and ~/.jupyter/custom/custom.js. Doesn't work. I also tried to edit mathjaxutils.js. It does nothing. Finaly I saw this post https://github.com/jupyter/help/issues/109. I realize Jupyter uses main.min.js to read MathJax configuration. So here is the solutions:

  • Download MathJax(https://github.com/mathjax/MathJax) from Github.
  • Unzip the MathJax file and go into the folder
    • copy jax/output/HTML-CSS/fonts/TeX into directoy ../notebook/static/components/MathJax/jax/output/HTML-CSS/fonts/
    • copy fonts/HTML-CSS/TeX into ../notebook/static/components/MathJax/fonts/HTML-CSS/
  • open ../notebook/static/notebook/js/main.min.js, search for availableFonts. It should be around line 14894. Change it to

    ...
    availableFonts: ["STIX-Web","TeX"],
    imageFont: null;
    preferredFont: "TeX",
    webFont: "TeX"
    ...
    
  • Refresh the notebook.

Jupyter ships with its own (smaller) version of MathJax. This is why it is not able to find the (Computer Modern) 'TeX' font -- there only is the STIX font.

To fix this, I was able to do the following:

  1. Download MathJax 2.7 and copy the jax directory.
  2. Replace Jupyter's jax directory with the copied one:
  • For the default environment: ~/anaconda3/lib/python3.7/site-packages/notebook/static/components/MathJax/jax
  • For a different environment: ~/anaconda3/envs/<ENVIRONMENT>/lib/python3.7/site-packages/notebook/static/components/MathJax/jax
  1. Restart Jupyter, right-click on a piece of math and switch the 'Math Renderer' to SVG.

(Adjust python version in path if yours is not 3.7; If you are using miniconda the path should be ~/opt/miniconda3/lib/...)