It took me some time to finally get latex math formulas working in Octopress. If you googled ‘Octopress latex’, you can get quite a few online resources about how to support latex in octopress, with various levels of complexity. In this post, I will write down how I achieve this.
The initial attempt
As I installed the jekyll-rst plugin to use rst to write my posts, I thought it should be easy to write latex math because docutils has native support for it since version 0.8 (A :math: role and also a .. math: directive introduced for that). However, after I tried to use these in a octopress post, I found that the post will be rendered to empty. For example, if I insert the following rst code into my post, the whole post becomes empty; but after removing this line, everything is fine.
1
|
|
I also verified that the exact same code can be successfully converted to valid html using the ‘rst2html.py’ script on my system, so I guess maybe something is wrong in ‘RbST’. I found that in RbST, it has its own copies of rst2html and rst2latex tools under /gems/RbST-0.1.3/lib/rst2parts ,
1 2 3 4 |
|
which will be used in rbst.rb. I have even tried to change rbst.rb to use the rst2html.py installed on my system, but this also didn’t get any luck.
1 2 3 4 5 6 7 8 9 |
|
Finally, I gave up on this and opened an issue on this for the jekyll-rst plugin. Hope the author can fix this.
Switch back to markdown and using kramdown
After a google search about this issue, seems that the simplest solution is to use kramdown, which has built-in support for latex.
First, install kramdown:
gem install karmdown
Then, add mathjax configs into
tag, in octopress, just add the below code into/source/_includes/custom/head.html
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
See here for more details. After this, we are ready to test latex math in our post. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
will render as
$$ \begin{align} & \phi(x,y) = \phi \left(\sum{i=1}^n x_ie_i, \sum{j=1}^n y_je_j \right) = \sum{i=1}^n \sum{j=1}^n x_i y_j \phi(e_i, e_j) = \ & (x_1, \ldots, x_n) \left( \begin{array}{ccc} \phi(e_1, e_1) & \cdots & \phi(e_1, e_n) \ \vdots & \ddots & \vdots \ \phi(e_n, e_1) & \cdots & \phi(e_n, e_n) \end{array} \right) \left( \begin{array}{c} y_1 \ \vdots \ y_n \end{array} \right) \end{align} $$
And for inline latex code, just use $\exp(-\frac{x^2}{2})$
, which will give
$\exp(-\frac{x2}{2})$.