blog.textfile.org結城浩のブログ

blog.textfile.org

Mathプラグインのテスト

Octopress + MathJax で数式を書く。

{% math %} を使う。

入力例

{% math %}
\begin{align*}
  & \dfrac{1}{2n+1}\binom{2n+1}{n} \\
  &= \dfrac{1}{2n+1} \cdot \dfrac{(2n+1)!}{(2n+1-n)!\,\,n!} \\
  &= \dfrac{1}{2n+1} \cdot \dfrac{(2n+1)!}{(n+1)!\,\,n!} \\
  &= \dfrac{1}{2n+1} \cdot \dfrac{(2n+1)\cdot (2n)!}{(n+1)!\,\,n!} \\
  &= \dfrac{(2n)!}{(n+1)!\,\,n!} \\
  &= \dfrac{(2n)!}{(n+1) \cdot n!\,\, n!} \\
  &= \dfrac{1}{n+1} \dfrac{(2n)!}{n!\,\, n!} \\
  &= \dfrac{1}{n+1} \binom{2n}{n} \\
\end{align*}
{% endmath %}

出力例

詳細

_config.yml(抜粋)
1
2
3
4
5
markdown: rdiscount
rdiscount:
  extensions:
    - strict
    - autolink
source/_includes/custom/head.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
      }
    });
</script>

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
plugins/mathjax.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Jekyll
  class MathJaxBlockTag < Liquid::Tag
    def render(context)
      '<script type="math/tex; mode=display">'
    end
  end
class MathJaxInlineTag < Liquid::Tag
    def render(context)
      '<script type="math/tex">'
    end
  end
class MathJaxEndTag < Liquid::Tag
    def render(context)
      '</script>'
    end
  end
end

Liquid::Template.register_tag('math', Jekyll::MathJaxBlockTag)
Liquid::Template.register_tag('m', Jekyll::MathJaxInlineTag)
Liquid::Template.register_tag('endmath', Jekyll::MathJaxEndTag)
Liquid::Template.register_tag('em', Jekyll::MathJaxEndTag)

参照

MathJax

http://www.mathjax.org

mathjax.rb

https://gist.github.com/jessykate/834610

Octopress

http://octopress.org