Next Spaceship

Driving into future...

Write LaTeX in Octopress With MathJax

| Comments

The easiest method is to add by Mathjax CDN, but the drawback are it’s not always stable and it doesn’t work if you want to develop offline and want to preview via the rake preview command. So I have to embed a customized version of Mathjax into Octopress. With this method, I can preview equations offline and it’s more stable – it doesn’t depend on the public CDN of Mathjax. OK, DIY start.

Install Mathjax

Before doing this, it’s a good idea to fork Octopress to your own repositories, so that you can make changes on it and save the configuration.

With Octopress, it is easy to custom the header file. Open the file source/_includes/custom/head.html, and add the following lines:

1
<script src="/mathjax/unpacked/MathJax.js"></script>

Then cd to the source/ folder and add Mathjax as a submodule:

1
git submodule add git://github.com/leonsim/MathJax.git mathjax

Here I use my forked MathJax repository, because I did some hack job ;-) Of course you can use the official repository, it’s here.

Install rdiscount With LaTeX Support

The official rdiscount doesn’t support LaTeX, while there are some other MarkDown rendering engines like kramdown, Pandoc etc, but these soltions either don’t support the $$ and ` \(` LaTex delimiters, which is my favourite writing style, or they only support the `$` or `\)` delimiters, and don’t support the inline equation, which I can not endure, so I forked it and added LaTeX support. If you want to know how it works: https://github.com/leonsim/rdiscount/commits/master.

1
2
3
4
git clone git://github.com/leonsim/rdiscount.git
cd rdiscount
gem build rdiscount.gemspec
gem install rdiscount-*.gem

Open the Gemfile file and find this line

1
gem 'rdiscount', '~> 2.0.7'

Replace with this line

1
gem 'rdiscount', '9.0.0.0'

Add rsync-exclude

Once you have successfully rake deploy your files to server, which will take some time because the mathjax folder is big, you can add a rsync-exclude file to exclude the mathjax folder in following deployments. Add a file named rysnc-exclude in the top folder of Octopress and put the following lines in it:

.git
.gitignore
mathjax

This will make the deploying speed as fast as without MathJax.

Also edit the Rakefile file, find this line,

FileList["#{args.source}/**/.*"].exclude("**/.", "**/..", "**/.DS_Store", "**/._*").each do |file|

and replace with this line,

FileList["#{args.source}/**/.*"].exclude("**/.", "**/..", "**/.DS_Store", "**/._*", "**/.git", "**/.gitignore").each do |file|

Test

1
2
3
Write an equation in $$ \TeX $$ now: $$ \frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots} } } } $$, enjoy!

Write an equation in \(\TeX\) now: \(\frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} = 1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}} {1+\frac{e^{-8\pi}} {1+\ldots} } } }\), enjoy!

1
Another one: $$ \frac{\frac{a}{b}}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{b}}}}}}}}}}}} $$

Another one: \(\frac{\frac{a}{b}}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{\frac{a}{b}}}}}}}}}}}}\)

1
Maxwell's Equations: $$ \begin{split}\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\   \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{split} $$

Maxwell’s Equations: \(\begin{split}\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{split}\)

Comments