教程来源

主要参考(98条消息) hexo下LaTeX无法显示的解决方案_zealscott的博客-CSDN博客

略有不同,主要在主题的_config.yml

安装插件

首先需要安装mathjax:

1
npm install hexo-math -save

然后需要把hexo默认的md渲染引擎hexo-renderer-marked换成hexo-renderer-kramed

1
2
npm uinstall hexo-renderer-marked -save
npm install hexo-renderer-kramed -save

修改转义

LaTeX与markdown语法有语义冲突,在markdown中,斜体和加粗可以用*或者_表示,在这里我们修改变量,将_用于LaTeX,而使用*表示markdown中的斜体和加粗

找到博客下node_modules\kramed\lib\rules\inline.js修改如下

1
2
//escape: /^\\([\\`*{}\[\]()#$+\-.!_>])/,
escape: /^\\([`*\[\]()#$+\-.!_>])/,
1
2
//  em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
em: /^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

修改配置

原文中说需要找到主题下的_config.yml修改如下:

1
2
3
4
5
6
# MathJax Support
mathjax:
enable: true
per_page: true
#cdn: //cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML
cdn: //cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML

但是我用的Chic主题,进去以后发现config.yml有关部分是这样的:

1
2
3
4
5
6
7
8
# plugin functions
## Mathjax: Math Formula Support
## https://www.mathjax.org
mathjax:
enable: true
import: demand # global or demand
## global: all pages will load mathjax,this will degrade performance and some grammers may be parsed wrong.
## demand: Recommend option,if your post need fomula, you can declare 'mathjax: true' in Front-matter

就没修改了

测试

在博客开头加上 mathjax: true

然后测试效果如下:

$\Sigma _0 ^n \frac{1}{n}$