Hexo中的数学公式

0. 官方文档

Next 介绍了两种方案: mathjax / katex 。

https://theme-next.js.org/docs/third-party-services/math-equations

这里用的是 mathjax。

1. mathjax配置

1.1 正文 Front-matter

1
2
3
4
5
---
title: Hexo中的数学公式
date: 2020-04-25 01:46:39
mathjax: true
---

1.2 Next主题配置

1
2
3
4
5
6
7
8
9
10
11
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

1.3 安装插件(貌似可以不装,默认的也能用)

先卸载 hexo-renderer-marked, 再安装 hexo-renderer-pandoc

1
2
npm un hexo-renderer-marked
npm i hexo-renderer-pandoc

2. 坑

遇到了公式无法正常显示的问题。

  • 公式:
    $RMSE = 1 - \dfrac{\sum_{i=1}^n (y_i - \hat{y}_i)^2} {\sum_{i=1}^n (y_i - \bar{y})^2}$

  • Hexo显示如下:

hexo-math-error

  • 安装hexo-renderer-pandoc插件也无法解决;

  • 换 katex 方案也不行。


最后发现是 $\hat{y}_i$ 这里出了问题,原因不明。

在 $\hat{y}_i$ 下标符号 _ 前加了一个反斜杠 \ ,搞定。

1
2
3
$RMSE = 1 - \dfrac{\sum_{i=1}^n (y_i - \hat{y}_i)^2} {\sum_{i=1}^n (y_i - \bar{y})^2}$

$RMSE = 1 - \dfrac{\sum_{i=1}^n (y_i - \hat{y}\_i)^2} {\sum_{i=1}^n (y_i - \bar{y})^2}$