Markdown Math Support
dev notesAdding Math Support
Like all bad tech blogs, I’ve detoured into a customization yakshave. My problem: I want to support markdown-side input of TeX math with some level of accessibility on the reader side, i.e., I don’t want to render the math as images, I want to retain the semantics. So I went a googlin’. [^1]
MathML: Great, there’s a representational standard (1). Now the bad news: it’s not very concise – it is only ideal as an intermediate format – it is unsupported in Chrome, and it’s poorly supported in some other browsers. The upshot is you need to use js to do last-mile part of the render.
MathJax: The de facto standard for a js renderer seems to be MathJax (2). As it so happens, MathJax also supports TeX input, so the MathML intermediary is not even required. The problem is, mainly, that it’s apparently slow. I’m taking this on the word of wikipedia/mediawiki notes on the subject, and on the claimes of a competitor package: KaTeX.
KaTeX: I found KaTeX (from some good folks at Khan Academy) while looking at latex.js, which uses KaTeX as its math engine. Sounds good, since I don’t really want all of LaTeX, I just want concise math markup, not all of TeX. KaTeX claims to do this faster than MathJax and I’m inclined to believe them because I’m lazy. Going to try it here; if it renders some basic examples fast enough for my own taste, I’ll be done for the time being, at least until I hit a roadblock because something I need isn’t supported. If it’s too slow we could render it server-side, but I’m not psyched about the idea of adding another language on the pre-rendering side (maybe someday if I switch the thing to Node). KaTeX’s downside vs. MathJax appears to be symbol support, but that might just be an opportunity to contribute.
Examples:
(kinematics + classical mechanics notation)
$$ v = \frac{dr}{dt} ,; a = \frac{dv}{dt} \qquad\qquad [1] $$
$$ \pmb{\theta} = \theta\pmb{\hat{n}} ,; \pmb{\omega} = \frac{d\pmb{\theta}}{dt} ,; \pmb{\alpha} = \frac{d\pmb{\omega}}{dt} \qquad \qquad [2] $$
$$ \pmb v = \pmb \omega \times \pmb r \qquad \qquad [3] $$
$$ \pmb p = m \pmb{v}(t) \qquad \qquad [4] $$
$$ \pmb F = \frac{d\pmb{p}}{dt} \qquad \qquad [5] $$
Escaping: See (4) for a wider discussion on this, but I’m planning to use the sidecar KaTeX autoloader with these delimiters: $$ $$..$$ $$ A close second to this approach was using script tags with the “math/tex” type; see (5).
References
(2) MathJax
(4) Mathdown wiki