Math 数学

不要在标题里使用 mathjax 语法,toc 目录不一定能正确显示 mathjax,可能显示 mathjax 代码

建议使用 KaTex 获得更好的效果,下文有介绍!

修改 主题配置文件:

1
2
3
4
5
mathjax:
enable: true
# true 表示每一页都加载mathjax.js
# false 需要时加载,须在使用的Markdown Front-matter 加上 mathjax: true
per_page: false

如果 per_page 设为 true,则每一页都会加载 Mathjax 服务。
设为 false,则需要在文章 Front-matter 添加 mathjax: true,对应的文章才会加载 Mathjax 服务。

然后你需要修改一下默认的 markdown 渲染引擎来实现 MathJax 的效果。

查看: hexo-renderer-kramed

以下操作在你 hexo 博客的目录下 (不是 Butterfly 的目录):

安装插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
npm uninstall hexo-renderer-marked --save
npm install hexo-renderer-kramed --save
````
配置 `hexo 根目录的配置文件`

```YAML
kramed:
gfm: true
pedantic: false
sanitize: false
tables: true
breaks: true
smartLists: true
smartypants: true

效果:

不要在标题里使用 KaTeX 语法,toc 目录不能正确显示 KaTeX

首先禁用MathJax(如果你配置过 MathJax 的话),然后修改你的主题配置文件以便加载katex.min.css:

1
2
3
4
5
6
katex:
enable: true
# true 表示每一页都加载katex.js
# false 需要时加载,须在使用的Markdown Front-matter 加上 katex: true
per_page: false
hide_scrollbar: true

你不需要添加 katex.min.js 来渲染数学方程。
相应的你需要卸载你之前的 hexomarkdown 渲染器,然后安装其它插件。

卸载掉 marked 插件,安装 hexo-renderer-markdown-it

1
2
3
4
5
npm un hexo-renderer-marked --save # 如果有安装这个的话,卸载
npm un hexo-renderer-kramed --save # 如果有安装这个的话,卸载

npm i hexo-renderer-markdown-it --save # 需要安装这个渲染插件
npm install katex @renbaoshuo/markdown-it-katex #需要安装这个katex插件

在 hexo 的根目录的 _config.yml 中配置

1
2
3
markdown:
plugins:
- '@renbaoshuo/markdown-it-katex'

如需配置其它参数,请参考 katex 官网

注意,此方法生成的 katex 没有斜体

卸载掉 marked 插件,然后安装新的hexo-renderer-markdown-it-plus:

1
2
3
4
5
6
7
8
9
10
11
# 替换 `hexo-renderer-kramed` 或者 `hexo-renderer-marked` 等hexo的markdown渲染器
# 你可以在你的package.json里找到hexo的markdwon渲染器,并将其卸载
npm un hexo-renderer-marked --save

# or

npm un hexo-renderer-kramed --save


# 然后安装 `hexo-renderer-markdown-it-plus`
npm i @upupming/hexo-renderer-markdown-it-plus --save

注意到 hexo-renderer-markdown-it-plus已经无人持续维护,
所以我们使用 @upupming/hexo-renderer-markdown-it-plus。
这份 fork 的代码使用了 @neilsustc/markdown-it-katex
同时它也是 VSCode 的插件 Markdown All in One所使用的,
所以我们可以获得最新的 KaTex 功能例如 \tag{}。

你还可以通过 @neilsustc/markdown-it-katex控制 KaTeX 的设置,
所有可配置的选项参见 https://katex.org/docs/options.html
比如你想要禁用掉 KaTeX 在命令行上输出的宂长的警告信息,
你可以在根目录的 _config.yml 中使用下面的配置将 strict 设置为 false:

1
2
3
4
5
6
7
markdown_it_plus:
plugins:
- plugin:
name: '@neilsustc/markdown-it-katex'
enable: true
options:
strict: false

当然,你还可以利用这个特性来定义一些自己常用的 macros。

因为 KaTeX 更快更轻量,因此没有 MathJax 的功能多(比如右键菜单)。
为那些使用 MathJax 的用户,主题也内置了 katex 的 复制 功能。