简体   繁体   中英

How do I configure mathjax for iPython notebooks?

I'm trying to find a way for mathjax to not use STIX fonts for math in my iPython notebook. Instead, I'd much rather have it use the 'TeX' fonts. According to the documentation for Mathjax I should use:

MathJax.Hub.Config({
  "HTML-CSS": {
    preferredFont: "TeX"
  }
});

That being said, I'm not sure where to put this. I've already tried putting this chunk of code into my custom.js file pertaining to my own ipython profile, but it doesn't work. Ideally, I'd like to make ipython profile specific adjustments for mathjax.

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

A simple test to make sure that you're getting the configuration correct is to change preferredFont: "TeX" to scale: 200 . Then save and reload a notebook. The math should be obviously way bigger than before. So assuming that worked, it means your config.js is doing what it needs to.

Now, more to the point, try adding another line so that your configuration looks like

MathJax.Hub.Config({
  "HTML-CSS": {
    availableFonts: ["TeX"],
    preferredFont: "TeX",
  }
});

Don't forget to fully refresh the notebook page after you've saved that. This overrides (what I'm guessing is) the default value of that availableFonts variable , which would allow STIX if mathjax can't find TeX. I'm not sure why it seems to ignore the preferred font, but this seems more like a mathjax issue than an ipython issue.

So now, if it still isn't in TeX font (which mathjax seems to call MathJax_Math-Italic.otf , or similar), I would guess that mathjax just can't find that font, and may have fallen back on something else. If that is the case, there's something messed up about your mathjax installation.

看看一些numericalmooc教训,如这一个在MathJax配置是通过一个包含css文件,这是在笔记本的某一点进口。

I also wanted to change the math font in the Jupyter Notebook. I think this is the same question:

First I looked into the MathJax documents: http://mathjax.readthedocs.org/en/latest/options/HTML-CSS.html

Following Mike's "scale:200 test" (thanks!) I found this file to be my correct configuration file on my windows machine:
C:\\Users\\mhof.ipython\\profile_default\\static\\custom\\custom.js. However trials like:

MathJax.Hub.Config({
  "HTML-CSS": {
    availableFonts: [], preferredFont: null, // force Web fonts
    webFont: "Neo-Euler"
  }
});

or

MathJax.Hub.Config({
  "HTML-CSS": {
   availableFonts: ["TeX","STIX-Web","Neo-Euler"],
   preferredFont: "Neo-Euler",
  }
});

did not change the Jupyter Notebook math font appearance after saving the file and refresh (F5) in the browser. (For the "scale:200 test" the difference was clearly visible).

Looking into my Anaconda installation Mathjax files under C:\\Anaconda3\\Lib\\site-packages\\notebook\\static\\components\\MathJax I found several math fonts in the HTML-CSS missing. I think here it is explained why: https://github.com/jupyter/notebook/issues/1037 . Project Jupyer member minrk adviced of how to tell the notebook to use a full MathJax installed in nbextensions by using: c.NotebookApp.mathjax_url = 'nbextensions/mathjax'

I do not know in which file I need to use this expression, can anybody help?

As an alternative trial approach I replaced the MathJax folder in my Anaconda installation by a MathJax folder downloaded from the mathjax project web page. Jupyter notebook runs and display formulas as before, but still no success in changing the font.

I would very much appreciate if somebody can give me a hint what I am doing wrong in my trials to change the math font in the Jupyter Notebook.

Thanks for answers and in general thanks a lot for that really interesting Jupyter Notebook project.

Regards

Malte

I've tweaked @Stefan Shi's answer to something a little easier, at least if you have the command-line svn installed.

  • Put the following into a script file called install_tex_fonts ( install_tex_fonts.bat in Windows-land):

     svn export https://github.com/mathjax/MathJax/trunk/fonts/HTML-CSS/TeX/woff fonts/HTML-CSS/TeX/woff svn export https://github.com/mathjax/MathJax/trunk/jax/output/HTML-CSS/fonts/TeX jax/output/HTML-CSS/fonts/TeX
  • Move the script file into {PYTHON}/Lib/site-packages/notebook/static/components/MathJax where {PYTHON} is the root directory where you installed Python

  • Open a shell (command prompt) in this directory
  • Run the script by typing install_tex_fonts (or ./install_tex_fonts on *nix systems; I guess you also have to chmod a+x it)
  • Add the following section in your ~/.jupyter/custom/custom.js file (the $([IPython.events]).on('app_initialized.NotebookApp') line should already be there):

     $([IPython.events]).on('app_initialized.NotebookApp', function(){ MathJax.Hub.Config({ "HTML-CSS": { availableFonts: ["TeX"], preferredFont: "TeX", webFont: "TeX" } });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM