Hexo 插入数学公式

安装配置hexo-math

hexo 中已经对mathjax、katex 进行了集成,这里使用的是mathjax,需要先下载 hexo-math

1
npm install hexo-math --save

然后在站点目录下_config.yml 中配置

1
2
3
math:
engine: 'mathjax' # or 'katex'
mathjax:

然后在主题目录下_config.yml 中配置,我这里使用的 Next主题,默认就有配置,只需要将 mathenable 改为true 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
math:
enable: true

# 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

engine: mathjax
#engine: katex

# hexo-renderer-pandoc (or hexo-renderer-kramed) needed to full MathJax support.
mathjax:
cdn: //cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML

其中 per_page 根据提示,表示默认不加载 mathjax/katex 脚本,如果设置为 false,则是每篇文章都会去加载 mathjax 脚本,这里我设置为 true,然后在需要加载的文章抬头添加 mathjax: true 即可,例如:

1
2
3
4
5
6
---
title: hexo 插入数学公式
date: 2020-04-14 16:35:11
mathjax: true
tags:
---

简单语法

行内公式

行内的公式用$公式内容$ 包裹起来,在公式中右键即可查看公式 $\TeX$ 代码

显示在行内的公式:$g(x)=8 \times ln \ x$,另外一个公式:$f(x)=ax^2+bx+c$

多行公式

多行公式用$$公式内容$$包裹起来,独占一行,同样右键可查看公式代码

  1. 矩阵
    $$\left[ \begin{array}{lll}{1} & {2} & {3} \\ {4} & {5} & {6} \\ {7} & {8} & {9}\end{array}\right]$$

  2. 指数与平方根
    $$\sqrt{x^{2a}+\sqrt{y}}$$

  3. 分数
    $$\frac{x^{2}}{k + 1}$$

  4. 常用符号
    $$+ - \times \div$$
    $$\pm \cdot \times \div \cup \cap \star \leq \ll \geq \gg$$
    $$\sim \simeq \approx$$

$\LaTeX$语法参考链接:一份不太简短的 $\LaTeX$ 2ε 介绍