最近将博客从octopress迁移到了hexo。 如果使用hexo默认的markdown解析器,然后在用mathjax进行公式显示,存在字符转义的问题。
默认的markdown解析器会将latex中的下划线和’'进行转义。公式不能正常显示,只有手动转义。
然后在网上找到一篇文章: Play MathJax with hexo-renderer-pandoc
介绍使用pandoc和hexo-renderer-pandoc
步骤
- Firstly, make sure you have installed pandoc.
- Secondly, cd into your hexo root folder and execute the following command:
1 | $ npm install hexo-renderer-pandoc --save |
- Create a file named
mathjax.ejs
in the folder liketheme_folder/layout/_partial/
, the code in mathjax.ejs are as follows:
1 | <!-- mathjax config similar to math.stackexchange --> |
The gist is here: mathjax.ejs gist
Add the snippits below in
theme_folder/layout/_partial/after_footer.ejs
1
2
3<% if (page.mathjax){ %>
<%- partial('mathjax') %>
<% } %>Add
mathjax: false
for_config.yml
in the root directory of your Hexo init folder. It is some thing like this:1
mathjax: false
For the post you want to load MathJax, add
mathjax: true
at the front-matter (文章开始的头部). By this method you can avoid unnecessary latency for loading Mathjax scripts. Just some thing as follows:1
mathjax: true
Delete the public folder and excute
hexo generate
again to make the modified Hexo theme work.
example
1 | The **characteristic polynomial** $\chi(\lambda)$ of the $3 \times 3$ matrix |
The characteristic polynomial \(\chi(\lambda)\) of the \(3 \times 3\) matrix
\[ \left( \begin{array}{ccc} a & b & c \\ d & e & f \\ g & h & i \end{array} \right) \]
is given by the formula
\[ \chi(\lambda) = \left| \begin{array}{ccc} \lambda - a & -b & -c \\ -d & \lambda - e & -f \\ -g & -h & \lambda - i \end{array} \right|. \]