Feature image

Hexo MathJax插件

介绍

Hexo不支持数学公式,网上可以找到很多人肉修改theme使其支持用MathJax渲染公式的方法,主要分为两个步骤:

  • 在theme的header中插入对MathJax CDN script的引用,并配置inline math
  • 在文章中用inline math插入公式

这个方法有两个明显的缺点:

  • 需要人肉进行的工作太多
  • 遇到特殊符号需要人肉escape,否则会被markdown parser吃掉

于是开发了一个插件,实现:

  • 自动部署MathJax
  • 添加MathJax Tag

安装

1
$ npm install hexo-math --save

初始化

在blog文件夹中执行:

1
$ hexo math install

_config.yml文件中添加:

1
plugins:
- hexo-math

使用

对于不含特殊符号的公式,可以直接使用MathJax的inline math表达式.
如果含有特殊符号,则需要人肉escape,如\之类的特殊符号在LaTex表达式中出现频率很高,这样就很麻烦,使用tag能够省不少事。

MathJax Inline:

1
Simple inline $a = b + c$.

效果:

Simple inline $a = b + c$.

MathJax Block:

1
2
3
4
$$\frac{\partial u}{\partial t}
= h^2 \left( \frac{\partial^2 u}{\partial x^2} +
\frac{\partial^2 u}{\partial y^2} +
\frac{\partial^2 u}{\partial z^2}\right)$$

效果:

$$\frac{\partial u}{\partial t}
= h^2 \left( \frac{\partial^2 u}{\partial x^2} +
\frac{\partial^2 u}{\partial y^2} +
\frac{\partial^2 u}{\partial z^2}\right)$$

Tag inline:

1
This equation {% math \cos 2\theta = \cos^2 \theta - \sin^2 \theta =  2 \cos^2 \theta - 1 %} is inline.

效果:

This equation $\cos 2\theta = \cos^2 \theta - \sin^2 \theta = 2 \cos^2 \theta - 1$ is inline.

Tag Block:

1
2
3
4
5
6
7
{% math_block %}
\begin{aligned}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{aligned}
{% endmath_block %}

效果:

$$\begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned}$$

源代码

GitHub