如何为iPython笔记本配置mathjax?

时间:2022-04-23 07:21:25

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在我的iPython笔记本中不使用STIX字体进行数学运算。相反,我宁愿让它使用'TeX'字体。根据Mathjax的文档,我应该使用:

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.

话虽这么说,我不知道该把它放在哪里。我已经尝试将这段代码放入我自己的ipython配置文件中的custom.js文件中,但它不起作用。理想情况下,我想对mathjax进行ipython配置文件特定的调整。

5 个解决方案

#1


3  

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.

确保您获得正确配置的简单测试是更改preferredFont:“TeX”以扩展:200。然后保存并重新加载笔记本。数学应该显然比以前更大。所以假设这有效,这意味着你的config.js正在做它需要的东西。

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.

保存后,请不要忘记完全刷新笔记本页面。这会覆盖(我猜的是)那个availableFonts变量的默认值,如果mathjax找不到TeX,它将允许STIX。我不确定为什么它似乎忽略了首选字体,但这似乎更像是一个mathjax问题而不是ipython问题。

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.

所以现在,如果它仍然不是TeX字体(mathjax似乎调用MathJax_Math-Italic.otf,或类似),我猜想mathjax只是找不到那种字体,并且可能已经落后于其他东西了。如果是这种情况,那么你的mathjax安装就会出现问题。

#2


6  

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.

我最近遇到了确切的问题。我真的不喜欢默认的STIX-Web字体来渲染方程式。经过一段时间的实验,我找到了一种方法来改变Jupyter Notebook中的MathJax字体。我的Jupyter笔记本版本是4.3.1,它随Anaconda一起提供。我认为其他版本的解决方案应该类似。

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:

我试图在/notebook/static/custom/custom.js和〜/ .jupyter / custom / custom.js中编辑custom.js。不起作用。我也尝试编辑mathjaxutils.js。它什么都不做。最后我看到这篇文章https://github.com/jupyter/help/issues/109。我意识到Jupyter使用main.min.js来读取MathJax配置。所以这是解决方案:

  • Download MathJax(https://github.com/mathjax/MathJax) from Github.
  • 从Github下载MathJax(https://github.com/mathjax/MathJax)。

  • 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/
    • 将jax / output / HTML-CSS / fonts / TeX复制到directoy ../notebook/static/components/MathJax/jax/output/HTML-CSS/fonts/

    • copy fonts/HTML-CSS/TeX into ../notebook/static/components/MathJax/fonts/HTML-CSS/
    • 将fonts / HTML-CSS / TeX复制到../notebook/static/components/MathJax/fonts/HTML-CSS/

  • 解压缩MathJax文件并进入文件夹复制jax / output / HTML-CSS / fonts / TeX到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

    打开../notebook/static/notebook/js/main.min.js,搜索availableFonts。它应该在第14894行附近。改为

    ...
    availableFonts: ["STIX-Web","TeX"],
    imageFont: null;
    preferredFont: "TeX",
    webFont: "TeX"
    ...
    
  • Refresh the notebook.
  • 刷新笔记本。

#3


2  

Take a look at some of the numericalmooc lessons such as this one where the MathJax configuration is included through a css file which is imported at some point in the notebook.

看看一些数学课程,例如通过css文件包含MathJax配置的课程,该文件是在笔记本中的某个点导入的。

#4


2  

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

我已经调整了@Stefan Shi对某些事情的回答,至少如果你安装了命令行svn。

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

    将以下内容放入名为install_tex_fonts的脚本文件中(Windows-land中的install_tex_fonts.bat):

    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

    将脚本文件移动到{PYTHON} / Lib / site-packages / notebook / static / components / MathJax,其中{PYTHON}是安装Python的根目录

  • Open a shell (command prompt) in this directory
  • 在此目录中打开shell(命令提示符)

  • 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)
  • 通过键入install_tex_fonts(或* nix系统上的./install_tex_fonts来运行脚本;我猜你还需要chmod a + x)

  • Add the following section in your ~/.jupyter/custom/custom.js file (the $([IPython.events]).on('app_initialized.NotebookApp') line should already be there):

    在〜/ .jupyter / custom / custom.js文件中添加以下部分($([IPython.events])。on('app_initialized.NotebookApp')行应该已存在):

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

#5


1  

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

我还想更改Jupyter笔记本中的数学字体。我认为这是同一个问题:

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

首先,我查看了MathJax文档: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:

继迈克的“规模:200测试”之后(谢谢!)我发现这个文件是我的Windows机器上正确的配置文件:C:\ Users \ mhof.ipython \ profile_default \ static \ custom \ custom.js。但是试验如下:

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

保存文件并在浏览器中刷新(F5)后,没有更改Jupyter Notebook数学字体外观。 (对于“比例:200测试”,差异清晰可见)。

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'

在C:\ Anaconda3 \ Lib \ site-packages \ notebook \ static \ components \ MathJax下查看我的Anaconda安装Mathjax文件我发现HTML-CSS中缺少几种数学字体。我想这里解释了原因:https://github.com/jupyter/notebook/issues/1037。项目Jupyer成员minrk建议如何告诉笔记本使用nbextensions中安装的完整MathJax: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.

作为替代试用方法,我用Mathjax项目网页下载的MathJax文件夹替换了Anaconda安装中的MathJax文件夹。 Jupyter笔记本像以前一样运行并显示公式,但在更改字体方面仍然没有成功。

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.

如果有人可以在我的试验中给我一个提示我在Jupyter笔记本中改变数学字体的错误,我将非常感激。

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

感谢您的回答,并且非常感谢Jupyter笔记本项目非常有趣。

Regards

Malte

#1


3  

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.

确保您获得正确配置的简单测试是更改preferredFont:“TeX”以扩展:200。然后保存并重新加载笔记本。数学应该显然比以前更大。所以假设这有效,这意味着你的config.js正在做它需要的东西。

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.

保存后,请不要忘记完全刷新笔记本页面。这会覆盖(我猜的是)那个availableFonts变量的默认值,如果mathjax找不到TeX,它将允许STIX。我不确定为什么它似乎忽略了首选字体,但这似乎更像是一个mathjax问题而不是ipython问题。

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.

所以现在,如果它仍然不是TeX字体(mathjax似乎调用MathJax_Math-Italic.otf,或类似),我猜想mathjax只是找不到那种字体,并且可能已经落后于其他东西了。如果是这种情况,那么你的mathjax安装就会出现问题。

#2


6  

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.

我最近遇到了确切的问题。我真的不喜欢默认的STIX-Web字体来渲染方程式。经过一段时间的实验,我找到了一种方法来改变Jupyter Notebook中的MathJax字体。我的Jupyter笔记本版本是4.3.1,它随Anaconda一起提供。我认为其他版本的解决方案应该类似。

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:

我试图在/notebook/static/custom/custom.js和〜/ .jupyter / custom / custom.js中编辑custom.js。不起作用。我也尝试编辑mathjaxutils.js。它什么都不做。最后我看到这篇文章https://github.com/jupyter/help/issues/109。我意识到Jupyter使用main.min.js来读取MathJax配置。所以这是解决方案:

  • Download MathJax(https://github.com/mathjax/MathJax) from Github.
  • 从Github下载MathJax(https://github.com/mathjax/MathJax)。

  • 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/
    • 将jax / output / HTML-CSS / fonts / TeX复制到directoy ../notebook/static/components/MathJax/jax/output/HTML-CSS/fonts/

    • copy fonts/HTML-CSS/TeX into ../notebook/static/components/MathJax/fonts/HTML-CSS/
    • 将fonts / HTML-CSS / TeX复制到../notebook/static/components/MathJax/fonts/HTML-CSS/

  • 解压缩MathJax文件并进入文件夹复制jax / output / HTML-CSS / fonts / TeX到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

    打开../notebook/static/notebook/js/main.min.js,搜索availableFonts。它应该在第14894行附近。改为

    ...
    availableFonts: ["STIX-Web","TeX"],
    imageFont: null;
    preferredFont: "TeX",
    webFont: "TeX"
    ...
    
  • Refresh the notebook.
  • 刷新笔记本。

#3


2  

Take a look at some of the numericalmooc lessons such as this one where the MathJax configuration is included through a css file which is imported at some point in the notebook.

看看一些数学课程,例如通过css文件包含MathJax配置的课程,该文件是在笔记本中的某个点导入的。

#4


2  

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

我已经调整了@Stefan Shi对某些事情的回答,至少如果你安装了命令行svn。

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

    将以下内容放入名为install_tex_fonts的脚本文件中(Windows-land中的install_tex_fonts.bat):

    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

    将脚本文件移动到{PYTHON} / Lib / site-packages / notebook / static / components / MathJax,其中{PYTHON}是安装Python的根目录

  • Open a shell (command prompt) in this directory
  • 在此目录中打开shell(命令提示符)

  • 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)
  • 通过键入install_tex_fonts(或* nix系统上的./install_tex_fonts来运行脚本;我猜你还需要chmod a + x)

  • Add the following section in your ~/.jupyter/custom/custom.js file (the $([IPython.events]).on('app_initialized.NotebookApp') line should already be there):

    在〜/ .jupyter / custom / custom.js文件中添加以下部分($([IPython.events])。on('app_initialized.NotebookApp')行应该已存在):

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

#5


1  

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

我还想更改Jupyter笔记本中的数学字体。我认为这是同一个问题:

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

首先,我查看了MathJax文档: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:

继迈克的“规模:200测试”之后(谢谢!)我发现这个文件是我的Windows机器上正确的配置文件:C:\ Users \ mhof.ipython \ profile_default \ static \ custom \ custom.js。但是试验如下:

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

保存文件并在浏览器中刷新(F5)后,没有更改Jupyter Notebook数学字体外观。 (对于“比例:200测试”,差异清晰可见)。

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'

在C:\ Anaconda3 \ Lib \ site-packages \ notebook \ static \ components \ MathJax下查看我的Anaconda安装Mathjax文件我发现HTML-CSS中缺少几种数学字体。我想这里解释了原因:https://github.com/jupyter/notebook/issues/1037。项目Jupyer成员minrk建议如何告诉笔记本使用nbextensions中安装的完整MathJax: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.

作为替代试用方法,我用Mathjax项目网页下载的MathJax文件夹替换了Anaconda安装中的MathJax文件夹。 Jupyter笔记本像以前一样运行并显示公式,但在更改字体方面仍然没有成功。

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.

如果有人可以在我的试验中给我一个提示我在Jupyter笔记本中改变数学字体的错误,我将非常感激。

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

感谢您的回答,并且非常感谢Jupyter笔记本项目非常有趣。

Regards

Malte