Hexo默认的渲染器是hexo-renderer-marked,不支持数学公式的渲染,经过多个方案的研究使用(包括了hexo-renderer-markdown-ithexo-renderer-katex等),最后使用下来,效果最好的就是hexo-renderer-pandochexo-filter-mathjax的组合,特此记录一下


移除原渲染器并安装Pandoc渲染器

想要使用hexo-renderer-pandoc,就需要先删除掉其他的渲染器,例如Hexo自带的hexo-renderer-marked

进入到Hexo的博客根目录下,执行以下命令删除原渲染器

1
npm uninstall hexo-renderer-marked --save

执行以下命令安装hexo-renderer-pandoc

1
npm install hexo-renderer-pandoc --save

安装Pandoc并进行验证

在Debian12下可以直接使用APT进行安装

1
apt install pandoc

如果是其他系统,可以到官网下载最新版进行安装

安装完成后在终端输入

1
pandoc -v

如果有类似以下的结果输出的话则是正常的

1
2
3
4
5
6
7
pandoc 2.17.1.1
Compiled with pandoc-types 1.22.2.1, texmath 0.12.4, skylighting 0.12.3.1,
citeproc 0.6.0.1, ipynb 0.2
User data directory: /root/.local/share/pandoc
Copyright (C) 2006-2022 John MacFarlane. Web: https://pandoc.org
This is free software; see the source for copying conditions. There is no
warranty, not even for merchantability or fitness for a particular purpose.

如果没有的话,还需要在_config.yml中指定下Pandoc可执行文件的位置,例如

1
2
pandoc:
pandocPath: C:/Program Files/Pandoc/pandoc.exe

安装Hexo Filter MathJax并开启数学公式渲染

1
npm install hexo-filter-mathjax --save

安装完之后,启用数学公式渲染有两种方法

第一种:按需启用

在需要渲染的文章Front-matter中添加mathjax: true,例如下方这样

1
2
3
4
5
6
---
title: On the Electrodynamics of Moving Bodies
categories: Physics
date: 1905-06-30 12:00:00
mathjax: true
---

第二种:全局启用

修改Hexo的配置文件_config.yml,添加以下这段(来源于官方),并将every_page修改为true,这样全部文章都会进行渲染,适合像我这样的懒人

1
2
3
4
5
6
7
8
9
10
11
mathjax:
tags: none # or 'ams' or 'all'
single_dollars: true # enable single dollar signs as in-line math delimiters
cjk_width: 0.9 # relative CJK char width
normal_width: 0.6 # relative normal (monospace) width
append_css: true # add CSS to pages rendered by MathJax
every_page: true # if true, every page will be rendered by MathJax regardless the `mathjax` setting in Front-matter
packages: # extra packages to load
extension_options: {}
# you can put your extension options here
# see http://docs.mathjax.org/en/latest/options/input/tex.html#tex-extension-options for more detail

效果展示

数学公式1

1
2
3
$$
i\hbar\frac{\partial}{\partial t}\psi=-\frac{\hbar^2}{2m}\nabla^2\psi+V\psi
$$

数学公式2

1
2
3
4
5
6
7
8
$$
\begin{eqnarray*}
\nabla\cdot\vec{E}&=&\frac{\rho}{\epsilon_0}\\
\nabla\cdot\vec{B}&=&0\\
\nabla\times\vec{E}&=&-\frac{\partial B}{\partial t}\\
\nabla\times\vec{B}&=&\mu_0\left(\vec{J}+\epsilon_0\frac{\partial E}{\partial t}\right)
\end{eqnarray*}
$$