Deploying MathJax

Published on April 2, 2017


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:

  • English locale (which is built-in)
  • TeX woff fonts
  • TeX input
  • HTML-CSS output
#!/bin/bash

wget https://github.com/mathjax/MathJax/archive/master.zip
unzip master.zip
rm master.zip
cd MathJax-master
rm -rf docs test unpacked .gitignore README-branch.txt README.md bower.json latest.js \
	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 {} \+

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
	},
	showMathMenu: false,
	tex2jax: {
		inlineMath: [ ['$','$'], ['\\(','\\)'], ['\\[','\\]'] ]
	}
});


References

  1. https://ro-che.info/articles/2017-04-02-deploying-mathjax