02 February 2014

让github博客可以显示公式

作者:Moose W. Oler

markdown , latex , mathjax , github


1.什么是MathJax

MathJax是一款用来显示公式的开源的JavaScript引擎。

2.如何操作

详细操作步骤请看MathJax手册

简要叙述一下,分两步:

第一步:在_layout/default.html头部添加如下代码:

<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>

即可。完成后的网页代码应该是这样子的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
...
<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>
    ...
</body>
</html>

第二步:在写markdown文档的时候,用分割符把TeX公式括起来即可。

例如:

"
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}.$$
"

显示出来是这个样子的:

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}.$$