# 在 Hexo 中完美使用 Mathjax 输出数学公式

## 常规方法

<script type="text/x-mathjax-config">
MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
tex2jax: { inlineMath: [ ["$", "$"], ["\$","\$"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
messageStyle: "none"
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i  = 1) {
all[i].SourceElement().parentNode.className  = ' has-jax';
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

PS: mathsize: "90%" 这里可以适当修改 Mathjax 公式的大小. 如果需要给公式编号的话, 则需要如下

    MathJax.Hub.Config({
TeX: {equationNumbers: {autoNumber: ["AMS"], useLabelIds: true}},
"HTML-CSS": {linebreaks: {automatic: true}},
SVG: {linebreaks: {automatic: true}}
});

<script type="text/x-mathjax-config">
MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"],scale: 90, linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50), },
tex2jax: { inlineMath: [ ["$", "$"], ["\$","\$"] ], displayMath: [ ["$$","$$"], ["\$", "\$"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno" },
TeX: {  equationNumbers: {autoNumber: ["AMS"], useLabelIds: true},noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}",beq:"\\begin{eqnarray}",eeq:"\",sln:"{\\mathfrak{sl}}",h:"{\\mathfrak{h}}",g:"{\\mathfrak{g}}",gl:"{\\mathfrak{gl}}",la:"\\lambda",La:"\\Lambda",mc:"\\mathcal",m:"\\mathrm",mf:"\\mathbf",lle:"\\leqslant",gge:"\\geqslant",mb:"\\mathbb",N:"{\\mathbb{N}}",Z:"{\\mathbb{Z}}",ch:"{\\mathrm{ch}}",R:"{\\mathbb{R}}",C:"{\\mathbb{C}}",vp:"{\\varphi}",e:"{\\epsilon}" } },
messageStyle: "none"
});
</script>

Macros: { href: "{}",beq:"\\begin{eqnarray}",eeq:"\",sln:"{\\mathfrak{sl}}",h:"{\\mathfrak{h}}",g:"{\\mathfrak{g}}",gl:"{\\mathfrak{gl}}",la:"\\lambda",La:"\\Lambda",mc:"\\mathcal",m:"\\mathrm",mf:"\\mathbf",lle:"\\leqslant",gge:"\\geqslant",mb:"\\mathbb",N:"{\\mathbb{N}}",Z:"{\\mathbb{Z}}",ch:"{\\mathrm{ch}}",R:"{\\mathbb{R}}",C:"{\\mathbb{C}}",vp:"{\\varphi}",e:"{\\epsilon}" } }

## 进阶版

<% if (page.mathjax){ %>
<%- partial('mathjax') %>
<% } %>

title: 测试Mathjax
date: 2014-2-14 23:25:23
tags: Mathmatics
categories: Mathjax
mathjax: true
---

## 缺点

Markdown 里使用 Mathjax 有一个很大的缺点, 比如下面这个问题.

博主你好，你这样用mathjax，在用markdown写博客时能正常书写带下标的公式吗？

This is an example for $x_mu$ and $y_mu$.

This is an example for $x\_mu$ and $y\_mu$.

## 解决办法

### 插件

$npm install hexo-math --save Hexo 文件夹中执行： $ hexo math install

_config.yml 文件中添加：

plugins:
- hexo-math

### 更换Hexo的markdown渲染引擎

npm uninstall hexo-renderer-marked --save
npm install hexo-renderer-markdown-it-plus --save

markdown_it_plus:
highlight: true
html: true
xhtmlOut: true
breaks: true
langPrefix:
typographer:
quotes: “”‘’
plugins:
- plugin:
name: markdown-it-katex
enable: true
- plugin:
name: markdown-it-mark
enable: false

### 修改 Markdown

hexo\node_modules\marked\lib\marked.js

escape: /^\\([\\*{}()#+\-.!_>])/,


escape: /^\\([*()#+\-.!_>])/,

 return '' + text + '';

 return '_' + text + '_';

• 暂无文章

### 评论 2

1. #1

你好，请问一下你的代码高亮，行号格式是怎么弄的？

轻云10个月前 (08-17)回复
• 你说的是 WordPress 还是 Hexo? 我用的不是 Hexo, 是 WordPress. 是通过插件 Crayon Syntax Highlighter 实现的.

Clanzd10个月前 (08-21)回复