在hexo 中 输入公式

介绍 和 使用

著名的Stackoverflow网站上的漂亮公式,就是使用了MathJax插件的效果。添加MathJax插件也非常简单,只需要在markdown文件中,MathJax 的开关,如下:

1
2
3
4
5
6
---
title: index.html
date: 2016-12-28 21:01:30
tags:
mathjax: true
--

接下来 在 hexo 中 安装插件

1
npm install hexo-math --save

修改 站点配置文件

1
2
3
4
5
6
7
8
9
plugins: 
hexo-math

math:
engine: 'mathjax' # or 'katex'
mathjax:
# src: custom_mathjax_source
config:
# MathJax config

修改 主题的配置文件:

1
2
3
4
# MathJax Support
mathjax:
enable: true
per_page: true

修改mathjax.swig 文件 添加cdn 源
注意 网上的大多数教程 配置的cdn源都是 http的,如果 你的网站配置了 ssl 那么 http则会产生安全问题 这里我们配置 https的cdn

1
cd \themes\next\layout\_third-party

找到 mathjax.swig 文件 修改如下:

1
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

配置行内公式写法:在mathjax.swig 修改如下

1
2
3
4
5
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}

use case

下面就可以在 markdown 中 使用LaTex 以及 Tex 数学公式了

行间公式

1
$$ evidence_{i}=\sum_{j}W_{ij}x_{j}+b_{i} $$

效果如下:

$$ evidence_{i}=\sum_{j}W_{ij}x_{j}+b_{i} $$

行内公式:

写法1 :如果不配置 行内公式的写法 则 hexo 只会这渲染样的写法

1
\\(公式\\)

写法2:如果不配置 行内公式的写法 则 hexo 不会这渲染样的写法

1
$b_i$

效果如下:
写法一:
\(W_i\)
写法二:
$b_i$

上述问题的原因

根据 mathjax 的文档 $公式$ 是不推荐的方式 若要使用 则需声明如下:

1
2
3
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>

但是 使用 $则没有问题 如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</body>
</html>

效果见链接

如果觉得有用的话,打赏我吧~