我们用hexo+github/gitee搭建个人博客,会发现无法显示markdown文件里的latex公式,这是因为hexo默认支持的hexo-renderer-marked渲染器不支持latex公式。那我们应该怎样做才能支持显示latex公式呢?以next主题为例,目前Next支持两种数学公式渲染引擎,分别为Mathjax和Katex。由于Mathjax支持的更为全面,因此我们选择Mathjax。
具体步骤如下:
- 卸载hexo-math和hexo-renderer-marked。在git bash中输入如下命令:
npm un hexo-math
npm un hexo-renderer-marked
2、安装hexo-renderer-pandoc渲染器,命令如下:
npm i hexo-renderer-pandoc
3、修改配置文件,修改next/_config.yml
中mathjax
的设置,如下:
# Math Formulas Render Support
math:
# Default (true) will load mathjax / katex script on demand.
# That is it only render those page which has `mathjax: true` in Front-matter.
# If you set it to false, it will load mathjax / katex srcipt EVERY PAGE.
per_page: true
# hexo-renderer-pandoc (or hexo-renderer-kramed) required for full MathJax support.
mathjax:
enable: true
# See: https://mhchem.github.io/MathJax-mhchem/
mhchem: false
# hexo-renderer-markdown-it-plus (or hexo-renderer-markdown-it with markdown-it-katex plugin) required for full Katex support.
katex:
enable: false
# See: https://github.com/KaTeX/KaTeX/tree/master/contrib/copy-tex
copy_tex: false
将mathjax下的enable设置为true,需要注意的是mathjax和katex两者同时只能使能一个。
另外,per_page建议设置为true(表示为所有文章进行渲染), 否则,需要在每个md文件前面都要加上mathjax:true。
在这里我遇到一个坑,当我设置
per_page为true后, 通过hexo s预览,发现还是无法正确显示latex公式。于是,我在我的markdown文档前面加了mathjax:true就正常了,如下:
---
title: XXX
categories:
tags: XXX
mathjax: true
---
不知道为什么per_page设置为true后还是要在文档前加mathjax:true。总之,如果跟我一样出现per_page:true未生效,在文档前加mathjax:true应该能解决。
4、本地安装pandoc
这一步很重要,这是我遇到的一个坑,如果没有安装,hexo g的时候就会报错:pandoc exited with code null。至于pandoc安装很简单,只要在官网上下载pandoc,直接安装即可,注意安装完要重启电脑。
完成以上步骤后,hexo clean && hexo g && hexo s,本地预览就可以看到公式能正常显示了。