Hexo中输入数学公式

根据Next主题官方文档Math Equation配置好Hexo对数学公式的支持,对于一些小问题可以写成LaTeX记录在博客中。但是这里面遇到的问题是,MarkDown毕竟不是针对LaTeX设计的,所以一些特殊的宏包在博客中是不能使用的,对于一些特殊复杂的公式需要撰写tex文件编译,而不是发表在博客上。对于一些简单的公式,如果在Hexo博客上编写,应当注意使用原始的LaTeX命令,以增强其兼容性,对于一些数学符号的输入,请参考:关于LaTeX公式编辑 Introduce, 而对于Hexo支持的数学公式格式,为了方便使用,直接引用其官方的例子。

支持的数学公式

Hexo中开启数学支持,有两种插件支持,其中MathJax依赖JavaScript,但其支持的数学公式更多,而KaTeX不依赖JavaScript, 同时速度更快,但是其持的数学公式少一些,因些根据Next主题官方,推荐使用MathJax.它们支持的TeX/LaTeX命令参考:

Simple Equations

For simple equations, use the following form to give a tag,

1
2
3
$$\begin{equation} \label{eq1}
e=mc^2
\end{equation}$$

\[\begin{equation} \label{eq1} e=mc^2 \end{equation}\]

Then, you can refer to this equation in your text easily by using something like:

1
The famous matter-energy equation $\eqref{eq1}$ proposed by Einstein...

The famous matter-energy equation \(\eqref{eq1}\) proposed by Einstein...

Multi-line Equations

For multi-line equations, inside the equation environment, you can use the aligned environment to split it into multiple lines:

1
2
3
4
5
6
7
$$\begin{equation} \label{eq2}
\begin{aligned}
a &= b + c \\
&= d + e + f + g \\
&= h + i
\end{aligned}
\end{equation}$$

\[\begin{equation} \label{eq2} \begin{aligned} a &= b + c \\ &= d + e + f + g \\ &= h + i \end{aligned} \end{equation}\]

1
Equation $\eqref{eq2}$ is a multi-line equation.

Equation \(\eqref{eq2}\) is a multi-line equation.

Multiple Aligned Equations

We can use align environment to align multiple equations. Each of these equations will get its own numbers.

1
2
3
4
5
$$\begin{align}
a &= b + c \label{eq3} \\
x &= yz \label{eq4} \\
l &= m - n \label{eq5}
\end{align}$$

\[\begin{align} a &= b + c \label{eq3} \\ x &= yz \label{eq4} \\ l &= m - n \label{eq5} \end{align}\]

1
There are three aligned equations: equation $\eqref{eq3}$, equation $\eqref{eq4}$ and equation $\eqref{eq5}$.

There are three aligned equations: equation \(\eqref{eq3}\), equation \(\eqref{eq4}\) and equation \(\eqref{eq5}\).

Since align in and of itself is a complete equation environment (read here about the difference between aligned and align in LaTeX). You do not need to wrap it with equation environment.

Exclude Equations from Numbering

In the align environment, if you do not want to number one or some equations, just use \nonumber right behind these equations. Like the following:

1
2
3
4
5
$$\begin{align}
-4 + 5x &= 2 + y \nonumber \\
w + 2 &= -1 + w \\
ab &= cb
\end{align}$$

\[\begin{align} -4 + 5x &= 2 + y \nonumber \\ w + 2 &= -1 + w \\ ab &= cb \end{align}\]

Use \tag to Tag Equations

Sometimes, you want to use more «exotic» style to refer your equation. You can use \tag{} to achieve this. For example:

1
$$x+1\over\sqrt{1-x^2} \tag{i}\label{eq_tag}$$

\[x+1\over\sqrt{1-x^2} \tag{i}\label{eq_tag}\]

1
Equation $\eqref{eq_tag}$ use `\tag{}` instead of automatic numbering.

Equation \(\eqref{eq_tag}\) use \tag{} instead of automatic numbering.