在hexo博客中使用数学公式


解决方法一

第一步: 安装Kramed

1
2
yarn remove hexo-renderer-marked
yarn add hexo-renderer-kramed

第二步:更改文件配置

打开/node_modules/hexo-renderer-kramed/lib/renderer.js,更改:

1
2
3
4
5
// Change inline math rule
function formatText(text) {
// Fit kramed's rule: $$ + \1 + $$
return text.replace(/`\$(.*?)\$`/g, '$$$$$1$$$$');
}

1
2
3
4
// Change inline math rule
function formatText(text) {
return text;
}

第三步: 停止使用 hexo-math,并安装mathjax包

1
2
yarn remove hexo-math
yarn add hexo-renderer-mathjax

第四步: 更新 Mathjax 的 配置文件

打开/node_modules/hexo-renderer-mathjax/mathjax.html

<script>替换为:

1
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>

第五步: 更改默认转义规则

打开/node_modules\kramed\lib\rules\inline.js

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

改为

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

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

改为

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

第六步: 开启mathjax

config.yml文件 中开启 Mathjax

1
2
mathjax:
enable: true

解决方法二

将这段代码放到 HTML 的 header 里,解决方案来自liam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script type="text/javascript">
$(document).ready(function(){
$("code").map(function(){
match = /^\$(.*)\$$/.exec($(this).html());
if (match) {
$(this).replaceWith("<span class=hpl_mathjax_inline>" + $(this).html() + "</span>");
MathJax.Hub.Queue(["Typeset",MathJax.Hub,$(this).get(0)]);
}
match = /^\$\$(.*)\$\$$/.exec($(this).html());
if (match) {
$(this).replaceWith("<span class=hpl_mathjax_inline>" + $(this).html() + "</span>");
MathJax.Hub.Queue(["Typeset",MathJax.Hub,$(this).get(0)]);
}
match = /^\\begin/.exec($(this).html());
if (match) {
$(this).replaceWith("<span class=hpl_mathjax_inline>" + $(this).html() + "</span>");
MathJax.Hub.Queue(["Typeset",MathJax.Hub,$(this).get(0)]);
}
});
});
</script>

https://www.jianshu.com/p/7ab21c7f0674

https://www.jianshu.com/p/0c97df3a8491

https://blog.csdn.net/u014630987/article/details/78670258

https://liam.page/2015/09/09/fix-conflict-between-mathjax-and-markdown/