Markdown与Latex共存

记录下在hexo中让markdown与latex共存的方法

方法一 math block

可以用math block使latex代码与markdown完美共存
下面是行间代码$\sum_{i=0}^{\min \{a,b-1\}} C_{a}^i C_b^{i+1}$

{% math %}\sum_{i=0}^{\min \{a,b-1\}} C_{a}^i  C_b^{i+1} {% endmath %}

下面是行中代码
$$\begin{align*} \sum_{i=0}^{\min \{a,b-1\}} C_{a}^i C_b^{i+1} &= \sum_{i=0}^{\min \{a,b-1\}} (C_{a+1}^{i+1}-C_{a}^{i+1})C_b^{i+1} \\\\ &= \sum_{i=0}^{\min \{a,b-1\}} C_{a+1}^{i+1}C_b^{i+1}-\sum_{i=0}^{\min \{a,b-1\}} C_{a}^{i+1}C_b^{i+1} \\\\ &= \sum_{i=0}^{\min \{a+1,b\}} C_{a+1}^{i}C_b^{i}-\sum_{i=0}^{\min \{a,b\}} C_{a}^{i}C_b^{i} \\\\ &= C_{a+1+b}^{\min \{a+1,b\}}-C_{a+b}^{\min \{a,b\}} \end{align*}$$

{% math %}
\begin{align*} 
\sum_{i=0}^{\min \{a,b-1\}} C_{a}^i  C_b^{i+1} &= \sum_{i=0}^{\min \{a,b-1\}} (C_{a+1}^{i+1}-C_{a}^{i+1})C_b^{i+1} \\\\
&= \sum_{i=0}^{\min \{a,b-1\}} C_{a+1}^{i+1}C_b^{i+1}-\sum_{i=0}^{\min \{a,b-1\}} C_{a}^{i+1}C_b^{i+1} \\\\
&= \sum_{i=0}^{\min \{a+1,b\}} C_{a+1}^{i}C_b^{i}-\sum_{i=0}^{\min \{a,b\}} C_{a}^{i}C_b^{i} \\\\
&= C_{a+1+b}^{\min \{a+1,b\}}-C_{a+b}^{\min \{a,b\}}
\end{align*}
{% endmath %}

方法二 修改渲染文件

然而一直用math block并不利于markdown文件和latex文件间的转换,我们还是想尽量都用同一份latex代码写所有的公式。
所以我们干脆直接修改markdown的渲染文件。
修改node_modules/marked/lib/marked.js

取消markdown中的\\ \{ \}转义

escape: /^\\([\\`*{}\[\]()# +\-.!_>])/,

修改为:

escape: /^\\([`*\[\]()# +\-.!_>])/,

取消markdown中的_渲染

em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

修改为:

em: /^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

测试

$$\sum_{i=0}^{\min \{a,b-1\}} C_{a}^i C_b^{i+1}$$
$$
\begin{align}
\sum_{i=0}^{\min \{a,b-1\}} C_{a}^i C_b^{i+1} &= \sum_{i=0}^{\min \{a,b-1\}} (C_{a+1}^{i+1}-C_{a}^{i+1})C_b^{i+1} \\
&= \sum_{i=0}^{\min \{a,b-1\}} C_{a+1}^{i+1}C_b^{i+1}-\sum_{i=0}^{\min \{a,b-1\}} C_{a}^{i+1}C_b^{i+1} \\
&= \sum_{i=0}^{\min \{a+1,b\}} C_{a+1}^{i}C_b^{i}-\sum_{i=0}^{\min \{a,b\}} C_{a}^{i}C_b^{i} \\
&= C_{a+1+b}^{\min \{a+1,b\}}-C_{a+b}^{\min \{a,b\}} \\
\end{align}
$$