Wednesday, October 28, 2015

Configuring MathJax for Blogger

The first step to enabling TeX formatting on your Blogger page is to include the MathJax Javascript file in your Blogger template. Under Template -> Edit HTML you should add the following just after the opening <head> tag:


    <script src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript">    

    MathJax.Hub.Config({
        HTML: ['input/TeX','output/HTML-CSS'],
        TeX: { extensions: ['AMSmath.js','AMSsymbols.js'], equationNumbers: { autoNumber: 'AMS' } },
        extensions: ['tex2jax.js'],
        jax: ['input/TeX','output/HTML-CSS'],
        tex2jax: { inlineMath: [ ['$','$'], ['\\(','\\)'] ],
                   displayMath: [ ['$$','$$'], ['\\[','\\]'] ],
                   skipTags: ["script","noscript","style","textarea","code"],
                   processEscapes: true },
        'HTML-CSS': { availableFonts: ['TeX'], linebreaks: { automatic: true } }
    });
    </script>

In this case I have removed 'pre' from the list of HTML tags skipped over by MathJax. If you are using the Compose tab to post in Blogger rather than raw HTML this is necessary as it uses the <pre> tag throughout. If you want to show TeX intermingled with <code> blocks you can remove that tag from skipTags as well.

MathJax will use the color defined under Page -> Text Color in your template customization so make sure that is set correctly as the Blogger editor will explicitly set the text color in some cases.

This should be all you need to do to enable TeX processing in all posts and comments on your blog. I have listed a few examples along with their raw text below for you to test with.

\(P(E) = {n \choose k} p^k (1-p)^{n-k}\)
\[P(E) = {n \choose k} p^k (1-p)^{n-k}\]

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


\(P(E) = {n \choose k} p^k (1-p)^{n-k}\)
\[P(E) = {n \choose k} p^k (1-p)^{n-k}\]



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