YuMo's Blog

MathJax在hexo中的使用总结

说明

由于marked.js的预处理, 在使用MathJax.js编写多行公式时会出现转义异常问题.解决办法较通用的是多加一层转义, 不建议直接修改marked.js文件.

目前我主要遇到的有:

  • \\会被转义成\
  • \}会被转义成}

对于以上两种情况, 编写公式时以此按如下修改即可.

  • \\\\
  • \\}

使用

在当前使用的主题after-footer.ejs或footer.ejs里引用并配置(主要是自动转义相关配置)即可.

1
cat themes/royce/layout/_partial/after-footer.ejs

引用的完整代码片段(来自官网:https://www.mathjax.org/):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<script type="text/x-mathjax-config">
MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
TeX: { noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
messageStyle: "none"
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script charset="utf-8" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

公式实例总结

1
$$ \mathsf{Data = PCs} \times \mathsf{Loadings} $$

效果:
$$ \mathsf{Data = PCs} \times \mathsf{Loadings} $$

1
$$ \mathbf{X}\_{n,p} = \mathbf{A}\_{n,k} \mathbf{B}\_{k,p} $$

效果:
$$ \mathbf{X}_{n,p} = \mathbf{A}_{n,k} \mathbf{B}_{k,p} $$

1
$$a^2 + b^2 = c^2$$

效果:
$$a^2 + b^2 = c^2$$

1
$$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$

效果:
$$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$

1
2
3
4
5
6
7
8
$$
\begin{eqnarray}
\nabla\cdot\vec{E} &=& \frac{\rho}{\epsilon_0} \\
\nabla\cdot\vec{B} &=& 0 \\
\nabla\times\vec{E} &=& -\frac{\partial B}{\partial t} \\
\nabla\times\vec{B} &=& \mu_0\left(\vec{J}+\epsilon_0\frac{\partial E}{\partial t} \right)
\end{eqnarray}
$$

效果:
$$
\begin{eqnarray}
\nabla\cdot\vec{E} &=& \frac{\rho}{\epsilon_0} \
\nabla\cdot\vec{B} &=& 0 \
\nabla\times\vec{E} &=& -\frac{\partial B}{\partial t} \
\nabla\times\vec{B} &=& \mu_0\left(\vec{J}+\epsilon_0\frac{\partial E}{\partial t} \right)
\end{eqnarray}
$$

1
2
3
4
5
6
$$ \left[
\begin{array}{cc|c}
1&2&3\\\\
4&5&6
\end{array}
\right] $$

效果:
$$ \left[
\begin{array}{cc|c}
1&2&3\\
4&5&6
\end{array}
\right] $$

$\bigl( \begin{smallmatrix} a & b \\\\ c & d \end{smallmatrix} \bigr)$

效果:
$\bigl( \begin{smallmatrix} a & b \\ c & d \end{smallmatrix} \bigr)$

1
2
3
4
5
6
7
8
9
$$
\begin{align}
\sqrt{37} & = \sqrt{\frac{73^2-1}{12^2}} \\\\
& = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\\\
& = \sqrt{\frac{73^2}{12^2}}\sqrt{\frac{73^2-1}{73^2}} \\\\
& = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\\\
& \approx \frac{73}{12}\left(1 - \frac{1}{2\cdot73^2}\right)
\end{align}
$$

效果:
$$
\begin{align}
\sqrt{37} & = \sqrt{\frac{73^2-1}{12^2}} \\
& = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\
& = \sqrt{\frac{73^2}{12^2}}\sqrt{\frac{73^2-1}{73^2}} \\
& = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\
& \approx \frac{73}{12}\left(1 - \frac{1}{2\cdot73^2}\right)
\end{align}
$$

1
2
3
4
5
6
7
$$
f(n) =
\begin{cases}{l}
n/2, & \text{if $n$ is even} \\\\
3n+1, & \text{if $n$ is odd}
\end{cases}
$$

效果:
$$
f(n) =
\begin{cases}{l}
n/2, & \text{if $n$ is even} \\
3n+1, & \text{if $n$ is odd}
\end{cases}
$$

1
2
3
4
5
6
7
8
9
$$
\left.
\begin{array}{l}
\text{if $n$ is even:}&n/2\\\\
\text{if $n$ is odd:}&3n+1
\end{array}
\right\\}
=f(n)
$$

效果:
$$
\left.
\begin{array}{l}
\text{if $n$ is even:}&n/2\\
\text{if $n$ is odd:}&3n+1
\end{array}
\right\}
=f(n)
$$

1
2
3
4
5
6
7
$$
f(n) =
\begin{cases}
\frac{n}{2}, & \text{if $n$ is even} \\\\[2ex]
3n+1, & \text{if $n$ is odd}
\end{cases}
$$

效果:
$$
f(n) =
\begin{cases}
\frac{n}{2}, & \text{if $n$ is even} \\[2ex]
3n+1, & \text{if $n$ is odd}
\end{cases}
$$

参考

  1. MathJax.js库官网: https://www.mathjax.org/

  2. MathJax教程: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference

  3. short-math-guide: ftp://ftp.ams.org/pub/tex/doc/amsmath/short-math-guide.pdf