Deploying MathJax

Published on ; updated on

Update (2020-03-21): the instructions in this post do not apply for the recently released MathJax-3.0, and I haven’t found a similar optimization recipe for it.


MathJax have just announced that they will shut down their CDN by the end of this month.

Although they suggest an alternative CDN, I have long wanted to host a copy of MathJax myself — this has a nice side-effect of being able to write math-heavy articles while offline (I have a local nginx server running on my laptop that serves a copy of this site).

The major obstacle has been the huge size of a default MathJax installation. It is huge both in terms of the size in bytes (inflating backups) and in terms of the number of files (slowing down jekyll). In their blog post, MathJax developers admit this problem and point to the optimization guide. The guide, unfortunately, contains too much text and too few commands.

Therefore, I want to document here my process of installing MathJax 2.7.0 — so that I can repeat this in the future, but also in the hope that this will be useful for others.

These commands leave only:

wget https://github.com/mathjax/MathJax/archive/2.7.0.zip
unzip MathJax-2.7.0.zip
rm MathJax-2.7.0.zip
cd MathJax-2.7.0
rm -rf docs test unpacked .gitignore README-branch.txt README.md bower.json \
  CONTRIBUTING.md LICENSE package.json composer.json .npmignore .travis.yml \
  config/ fonts/HTML-CSS/TeX/png/ localization/ \
  extensions/MathML extensions/asciimath2jax.js extensions/jsMath2jax.js \
  extensions/mml2jax.js extensions/toMathML.js
find fonts/HTML-CSS/ -mindepth 1 -maxdepth 1 ! -name TeX -exec rm -rf {} \+
find fonts -mindepth 3 -maxdepth 3 ! -name woff -exec rm -rf {} +
find jax/input/ -mindepth 1 -maxdepth 1 ! -name TeX -exec rm -rf {} \+
find jax/output/ -mindepth 1 -maxdepth 1 ! -name HTML-CSS -exec rm -rf {} \+
find jax/output/HTML-CSS/fonts -mindepth 1 -maxdepth 1 ! -name TeX -exec rm -rf {} \+

This brings the installation from 32069 files and 181MiB down to 182 files and 1.7MiB.

A MathJax config consistent with this installation should look something like:

MathJax.Hub.Config({
  extensions: ["tex2jax.js"],
  jax: ["input/TeX", "output/HTML-CSS"],
  "HTML-CSS": {
    availableFonts: ["TeX"],
    imageFont: null
  },
  MathMenu: {
   showRenderer: false,
   showFontMenu: false,
   showLocale: false
  }
});