Hexo 中显示数学公式

配置Hexo的plugin Hexo-math 来支持数学公式。

Install

1
$ npm install hexo-math --save

在站点的_config.yml中编辑:

1
2
math:
engine: 'mathjax'

或者:

1
2
math:
engine: 'katex' #目前不能用,等待检查

使用

Inline:

1
Simple inline $a = b + c$.

Simple inline $a = b + c$.

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:

Single line content will be parsed as inline math (same as $...$):

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

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

Multiple line content will be parsed as block math (same as $$...$$)

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

以上和以下内容引用自https://github.com/hexojs/hexo-math,以下为原文。

Deprecated since 2.0.0
Tag Block:

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

Migration Note

Migrating to 3.0.0

  • mathjax should be moved under math section in your _config.yml

    Migrating to 2.1.0

  • math_block is removed

    Migrating to 2.0.0

  • math_block is deprecated (but still usable. This won’t break your site)

  • math block is used to handle both inline and block math due to breaking changes made since Hexo 3.x. (This will break your site if you’re using math block)

Since Hexo 3.x, nunjucks is used as tag engine instead of swig. Syntax like this won’t work any more:

1
{% math \frac{|ax + by + c|}{\sqrt{a^{2}+b^{2}}} %}

Instead, you should use open and close tag since this version:

1
{% math %}\frac{|ax + by + c|}{\sqrt{a^{2}+b^{2}}} {% endmath %}

Change all your math tags accordingly.