Using MathJax in Blogger

I have added MathJax
support to this blog so that I can typeset mathematics using a large subset of (LaTeX) commands
. This functionality works in comments too, which has the nice side effect that it can be used to work around Blogger’s limited formatting. In particular, formatting source code in comments can be done as

(
  verb|int foo(void)| \
  verb|{| \
  verb|    return 0;| \
  verb|}| \
)

which is rendered with most of the formatting intact:

(

verb|int foo(void)| \

verb|{| \

verb| return 0;| \

verb|}|

)

One annoying thing is that the (LaTeX) commands are not processed when writing or previewing the comment, but they are rendered correctly when it is published…

How to enable MathJax in Blogger

MathJax need to be loaded and configured when the page is loaded. This is done by adding the following

<script type="text/javascript" async="async"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML,Safe">

in the
block by editing the blog’s template (choose “Template”, and click the “Edit HTML” button). This only adds the support to the desktop template, so you need to enable it separately for mobile by pressing the gear button, and choosing the “custom” mobile template (that generates a mobile template from your desktop template).

There are several configurations to choose between, with support for (LaTeX), MathML, and AsciiMath notation, and control over how much functionality is loaded up front, and how much is loaded on-demand. I have chosen the TeX-AMS_CHTML
which enables all (LaTeX) support, but avoids MathML and AsciiMath.

Note the ,Safe
modifier added after the configuration name. This disables unsafe constructs such as running javascript from (LaTeX) commands

[E href{javascript:alert("Einstein says so!")}{=} mc^2]

This is needed in order to prevent commenters messing up the blog by adding evil constructs in comments.

Testing the functionality

Below are some random formulas, just to verify that the rendering works as intended:

[

sigma = sqrt{frac{1}{N}sum_{i=1}^{N}(x_{i}-mu)^{2}}

]

[

left{

begin{aligned}

a_1x+b_1y+c_1z &=d_1+e_1 \

a_2x+b_2y&=d_2 \

a_3x+b_3y+c_3z &=d_3

end{aligned}

right.

]

[

require{AMScd}

begin{CD}

pi(X, x_{0}) @>phi_{*}>> pi(Y, phi(x_{0}))\

@VVuV @VVvV\

pi(X, x_{1}) @>phi_{*}>> pi(Y, phi(x_{1}))

end{CD}

]

Krister Walfridsson's blog稿源:Krister Walfridsson's blog (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Using MathJax in Blogger

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录