Getting math into the blog

MultiMarkdown

Since I am using ikiwiki for my blog I got happy to read about Math Syntax when reading the MultiMarkdown Syntax Guide. This would let me write

<< e=mc^2 >>

when I wanted to render

e=mc2

The implementation of MultiMarkdown in ikiwiki did not support this syntax and could have been the end of the happiness.

mathml directive for ikiwiki

Since Text::ASCIIMathML have been used for the Math Syntax and ikiwiki supports writing your own plugins I got the idea to write my own mathml directive.

This directive renders a ASCIIMathML formula into MathML. There is just one argument code and it is passed to Text::ASCIIMathML to get the MathML output.

Examples

Input

[[!mathml  code="x^2"]]

Output

<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle><msup><mi>x</mi><mn>2</mn></msup></mstyle></math>

Rendered result

x2
[[!mathml  code="a^2+b^2=c^2"]]
a2+b2=c2
[[!mathml  code="
sigma(A) =
[(sigma(a_(11)),...,sigma(a_(1n))),
 (,...,),
 (sigma(a_(m1)),...,sigma(a_(mn)))]
"]]
σ(A)=[σ(a11)...σ(a1n)...σ(am1)...σ(amn)]

Get the source

If you want the source as of today it is available in mathml.pm. I also have a Git repository at http://gustaf.thorslund.org/src/ikiwiki.git with a branch named mathml. If you are into using Git you might as well check out the ikiwiki source and after running the gitremotes script checkout the branch GustafThorslund/mathml.

Known issues

I am currently running Ubuntu Karmic Koala (9.10) with ikiwiki rebuilt from the Lucid Lynx (10.04) source for my blog. With this setup I have to disable scrubbing of the generated html. On my laptop running Lucid Lynx it works fine without disabling scrubbing. Since this is just a blog and directives are not allowed in comments anyway I can live with scrubbing being turned off.

Of the browsers I have triend the output looks good in:

While it looks bad in:

Some answer to getting better support might be in Supporting MathML. Since Math Syntax did not look any good in Konqueror or Chrome the hope on getting it any better might be limited.

Update 1

  1. I had to turn off scrubbing on my Lucid Lynx laptop too. I just had confused myself enough to not realize it.
  2. I have patched htmlscrubber.pm to not scrub away my MathML output and enabled scrubbing again.
Since this is just a blog and directives are not allowed in comments anyway I can live with scrubbing being turned off

If HTML is allowed, then I don't think you want to do that. Turn off scrubbing for individual pages that are locked (i.e. admin-edit-only), while leaving it on for pages like comments that can be created/edited by possibly-evil users like me.

Comment by smcv [pseudorandom.co.uk] 2010-04-04 19:28 +0200

Thanks for your comment smcv!

Just turning of scrubbing on some pages is kind of hard on a blog where same post is inlined on a number of pages and also have comments inlined on one page. I have turned on scrubbing again now after patching htmlscrubber.pm.

/Gustaf

Comment by gustaf [thorslund.org] 2010-04-06 09:31 +0200
Happened to have a look using Internet Explorer 7 and it did not even understand xhtml. After reading http://www.w3.org/MarkUp/2004/xhtml-faq#ie I have now changed the suffix from .xhtml to .html again. I might try something else some other day. Now this page looks bad even in Firefox :(
Comment by gustaf [thorslund.org] 2010-05-26 18:58 +0200

Besides the stylesheet trick on the w3 page, you might also consider playing with the served mime type. See Calvin Smith's simple notes and Jacques Distler's more detailed approach (some JavaScript). IE users have to install MathPlayer or another MathML plugin, but that's what they deserve ;).

I don't have comments on my ikiwiki blog, so I have also disabled scrubbing, but other than that it works for me (although I use mdwn_itex to generate the MathML).

Add a comment