Hexo 数学公式渲染

Hexo 数学公式渲染方案:hexo-renderer-pandoc + hexo-filter-mathjax

1. 安装hexo-renderer-pandoc插件和pandoc

卸载默认的hexo渲染器,安装hexo-renderer-pandoc插件

1
2
3
# 先卸载原渲染器
npm uninstall hexo-renderer-marked --save
npm install hexo-renderer-pandoc --save

然后,进入pandoc下载页面,点击Download the latest installer,然后下载安装即可(pandoc支持Windows、mac、linux系统)。Debian系统执行apt install pandoc安装即可。

配置hexo根目录的配置文件_config.yml:

1
2
3
4
5
6
7
8
9
pandoc:
args:
- '-f'
- 'commonmark_x'
- '-t'
- 'html'
- '--mathjax'
extensions:
- '-implicit_figures'

2. 安装hexo-filter-mathjax插件

然后安装hexo-filter-mathjax插件,该插件用于后端渲染mathjax:

1
npm install hexo-filter-mathjax

配置hexo根目录的配置文件_config.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:
- physics
- mathtools
- color
- noerrors
- amsmath
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

将every_page设置为true,每个页面都会由mathjax呈现,如果你只需要某些页面使用mathjax,则需要这里设置为false,并对于某篇特定的文章中,添加:

1
2
3
4
5
6
---
title: xxxx
categories: xxx
date: xxxx-xx-xx xx:xx:xx
mathjax: true
---

就可以在单独的这篇文章中使用latex语法。

参考文章

为hexo博客更换pandoc渲染