说来也是无语,这几天在准备考试,从零开始预习信息安全数学基础。里面少不了大量的公式,就需要用到LaTex,当我发现我的LaTex没有渲染成功后,一直在找寻方法,然后…..然后就绕了一个大大圈

好了,不废话了。
对于有Mathjax选项的主题,可以直接在主题文件的_config.yml中选择开启,OK结束,就像这样

1
2
3
4
5
6

# MathJax
mathjax:
enable: true
per_page: true

我是用的是butterfly,是支持的。然而,被我一顿乱作,把hexo直接给整崩了。我TM…..欸

对于原本不支持的主题,可以有以下操作(我先说啊,我没试过,但看起来挺像那么回事,先自行备份,错了别找我
)

1.需要在 themes/YourThemeName/layout/ 下新建文件 mathjax.ejs 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<% if (theme.mathjax.enable){ %>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
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/javascript" src="<%- theme.mathjax.cdn %>"></script>
<% } %>

2.需要在 themes/YourThemeName/_config.yml 末尾追加:

1
2
3
4
# MathJax Support
mathjax:
enable: true
cdn: https://cdn.jsdelivr.net/npm/mathjax@2.7.8/MathJax.js?config=TeX-AMS-MML_HTMLorMML

3.修改 themes/YourThemeName/layout/post.ejs 文件,在中间添加:

1
2
3
<% if (theme.mathjax){ %>
<%- partial('mathjax') %>
<% } %>

4.最好你需要在你用到 LaTex 公式的文章顶部配置里追加 Mathjax 配置(如果你的主题原本就支持,可以不用加这个头):

1
2
3
4
5
---
title: 文章标题
date: 2020-09-02 20:11:42
mathjax: true
---

最后,简单测试一下:

$$1+1=2$$

OK

感谢大佬的博客:https://blog.xiangfa.org/2020/09/let-hexo-support-latex-formulas/

2021-12-24

⬆︎TOP