Hexo 顯示數學公式

問題: 無法在 Hexo 中直接顯示數學公式?

在 Markdown 編輯器中,一般都會支援 MathJax or LaTex 的數學公式語法。

但是推上 Blog 之後,顯示還是如原本一樣。

1
\alpha^3 + \beta^6 - \gamma^2 \times 3x \div 8y^5 + \omega

## 如何在 Hexo 中顯示數學公式

Google 後發現兩種解法:

  1. 修改 marked.js
    修改的原因是 Hexo 渲染時,數學公式會與預設的跳脫字元衝突,導致數學公式不完整,而顯示不出來。
    不過這個方法的缺點就是耗時修改,時間就是金錢,有簡單的方法就用唄。

  2. 安裝 Hexo MathJax plugin (推薦)
    只要在部落格路徑下的 git shell 中,執行下列安裝指令即可:

    1
    npm install hexo-math --save
  3. _config.yml 加入

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    math:
    engine: 'mathjax' # or 'katex'
    mathjax:
    src: custom_mathjax_source
    config:
    # MathJax config
    katex:
    css: custom_css_source
    js: custom_js_source # not used
    config:
    # KaTeX config

然後數學公式就可以正常顯示拉!

$$
\alpha^3 + \beta^6 - \gamma^2 \times 3x \div 8y^5 + \omega
$$

20180211 更新

最近換了 next 主題,發現 mathjax 好像失效了,又重新找方法
看了一下 next/_config.yml 似乎有自帶支援 mathjax

  1. next/_config.yml 開啟 mathjax 支援

    1
    2
    3
    # Math Equations Render Support
    math:
    enable: true
  2. 於 post 的 snippet 加入 mathjax

    1
    2
    3
    4
    5
    6
    7
    8
    ---
    title:
    author:
    date:
    tags:
    categories:
    mathjax: true
    ---

    結果就可以正常顯示了,收工。


## Reference

Hexo MathJax插件
搭建一个支持LaTEX的hexo博客
MathJax have no effect #1774

Author

Mo1cibo

Posted on

2017-01-01

Updated on

2020-10-31

Licensed under

Comments