$$heading_1$$ LaTeX for TiddlyWiki – A MathJax Plugin

Update: The MathJax Plugin for TiddlyWiki has a new home: https://github.com/guyru/tiddlywiki-mathjax
Some time ago I came across MathJax, a nifty, Javascript based engine for displaying TeX and LaTeX equations. It works by “translating” the equation to MathML or HTML+CSS, so it works on all modern browsers. The result isn’t a raster image, like in most LaTeX solutions $$extract_itex$$e.g. MediaWiki$$/extract_itex$$, so it’s scales with the text around it. Furthermore, it’s quite easy to integrate as it doesn’t require any real installation, and you could always use MathJax’s own CDN, which makes things even simpler.

A tiddler with LaTeX equations.

I quickly realized MathJax will be a perfect fit for TiddlyWiki which is also based on pure Javascript. It will allow me to enter complex formulas in tiddlers and still be able to carry my wiki anywhere with me, independent of a real TeX installation. I searched the web for an existing MathJaX plugin for TiddlyWiki but I came up empty handed $$extract_itex$$I did find some links, but they referenced pages that no longer exist$$/extract_itex$$. So I regarded it as a nice opportunity to begin writing some plugins for TiddlyWiki and created the MathJaxPlugin which integrates MathJax with TiddlyWiki.

As I don’t have an online TiddlyWiki, you’ll won’t be able to import the plugin, instead you’ll have to install it manually $$extract_itex$$which is pretty simple$$/extract_itex$$.

Start by creating a new tiddler named MathJaxPlugin, and tag with systemConfig $$extract_itex$$this tag will tell TiddlyWiki to execute the JS code in the tiddler, thus making it a plugin. Now copy the following code to the tiddler content: /*** |''Name:''|MathJaxPlugin| |''Description:''|Enable LaTeX formulas for TiddlyWiki| |''Version:''|1.0.1| |''Date:''|Feb 11, 2012| |''Source:''|http://www.guyrutenberg.com/2011/06/25/latex-for-tiddlywiki-a-mathjax-plugin| |''Author:''|Guy Rutenberg| |''License:''|$$[BSD open source license$$]| |''~CoreVersion:''|2.5.0| !! Changelog !!! 1.0.1 Feb 11, 2012 * Fixed interoperability with TiddlerBarPlugin !! How to Use Currently the plugin supports the following delemiters: * """$$extract_tex$$""".."""$$/extract_tex$$""" - Inline equations * """$$extract_tex$$""".."""$$/extract_tex$$""" - Displayed equations * """$$extract_itex$$""".."""$$/extract_itex$$""" - Displayed equations !! Demo This is an inline equation $$extract_tex$$P(E$$/extract_itex$$ = {n \choose k} p^k $$extract_itex$$1-p$$/extract_itex$$^{ n-k}$$/extract_tex$$ and this is a displayed equation: $$extract_itex$$J_\alpha$$extract_itex$$x$$/extract_itex$$ = \sum_{m=0}^\infty \frac{$$extract_itex$$-1$$/extract_itex$$^m}{m! \, \Gamma$$extract_itex$$m + \alpha + 1$$/extract_itex$$}{\left$$extract_itex$${\frac{x}{2}}\right$$/extract_itex$$}^{2 m + \alpha}$$/extract_itex$$ This is another displayed equation $$extract_tex$$e=mc^2$$/extract_tex$$ !! Code ***/ //{{{ config.extensions.MathJax = { mathJaxScript : "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML", // uncomment the following line if you want to access MathJax using SSL // mathJaxScript : "https://d3eoax9i5htok0.cloudfront.net/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML", displayTiddler: function$$extract_itex$$TiddlerName$$/extract_itex$$ { config.extensions.MathJax.displayTiddler_old.apply$$extract_itex$$this, arguments$$/extract_itex$$; MathJax.Hub.Queue$$extract_itex$$$$"Typeset", MathJax.Hub$$$$/extract_itex$$; } };   jQuery.getScript$$extract_itex$$config.extensions.MathJax.mathJaxScript, function($$/extract_itex$${ MathJax.Hub.Config$$extract_itex$${ extensions: $$"tex2jax.js"$$, "HTML-CSS": { scale: 100 } }$$/extract_itex$$;   MathJax.Hub.Startup.onload; config.extensions.MathJax.displayTiddler_old = story.displayTiddler; story.displayTiddler = config.extensions.MathJax.displayTiddler; });   config.formatters.push$$extract_itex$${ name: "mathJaxFormula", match: "\\\\\$$extract_itex$$|\\\\|\\\\\$$extract_tex$$", //lookaheadRegExp: /(?:\\\$$|\[/extract_tex$$((?:.|\n$$/extract_itex$$*?)$$extract_itex$$?:\\$$/extract_itex$$|$$extract_tex$$$$/extract_itex$$/mg, handler: function$$extract_itex$$w$$/extract_itex$$ { switch$$extract_itex$$w.matchText$$/extract_itex$$ { case "\$$extract_itex$$": // displayed equations this.lookaheadRegExp = /\\\$$[extract_itex$$(?:.|\n$$/extract_itex$$*?)$$extract_itex$$\\$$/extract_itex$$$$/extract_itex$$/mg; break; case "$$/extract_tex$$": // inline equations this.lookaheadRegExp = /\$$extract_itex$$\$$/extract_itex$$$$extract_itex$$(?:.|\n$$/extract_itex$$*?)$$extract_itex$$\$$extract_itex$$\$$/extract_itex$$$$/extract_itex$$/mg; break; case "\$$extract_tex$$": // inline equations this.lookaheadRegExp = /\\\$$extract_itex$$((?:.|\n$$/extract_itex$$*?)$$extract_itex$$\\$$/extract_tex$$$$/extract_itex$$/mg; break; default: break; } this.lookaheadRegExp.lastIndex = w.matchStart; var lookaheadMatch = this.lookaheadRegExp.exec$$extract_itex$$w.source$$/extract_itex$$; if$$extract_itex$$lookaheadMatch && lookaheadMatch.index == w.matchStart$$/extract_itex$$ { createTiddlyElement$$extract_itex$$w.output,"span",null,null,lookaheadMatch$$0$$$$/extract_itex$$; w.nextMatch = this.lookaheadRegExp.lastIndex; } } }); //}}}

After saving the tiddler, reload the wiki and the MathJaxPlugin should be active. You can test it by creating a new tiddler with some equations in it:

This is an inline equation $$extract_tex$$P$$extract_itex$$E$$/extract_itex$$ = {n \choose k} p^k $$extract_itex$$1-p$$/extract_itex$$^{ n-k}$$/extract_tex$$ and this is a displayed equation: $$extract_itex$$J_\alpha$$extract_itex$$x$$/extract_itex$$ = \sum_{m=0}^\infty \frac{$$extract_itex$$-1$$/extract_itex$$^m}{m! \, \Gamma$$extract_itex$$m + \alpha + 1$$/extract_itex$$}{\left$$extract_itex$${\frac{x}{2}}\right$$/extract_itex$$}^{2 m + \alpha}$$/extract_itex$$

Which should result in the tiddler that appears in the above image.

Update 2011-08-19: Removed debugging code from the plugin.

$$heading_3$$ Changelog 1.0.1 $$extract_itex$$Feb 11, 2012 • Applied Winter Young’s fix for interoperability with other plugins (mainly TiddlerBarPlugin $$heading_2$$ 61 thoughts on “LaTeX for TiddlyWiki – A MathJax Plugin” 1. zelhar Guy, I tweaked your script a little bit and so far it seems to be working ok. Basically I removed the whole “config.formatters.push” section, and edited the config.extension.MathJax section. config.extensions.MathJax = { mathJaxScript : "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML-full", // uncomment the following line if you want to access MathJax using SSL // mathJaxScript : "https://d3eoax9i5htok0.cloudfront.net/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML", displayTiddler: function(TiddlerName$$/extract_itex$$ { config.extensions.MathJax.displayTiddler_old.apply$$extract_itex$$this, arguments$$/extract_itex$$; MathJax.Hub.Queue$$extract_itex$$$$"Typeset", MathJax.Hub$$$$/extract_itex$$; } };   jQuery.getScript$$extract_itex$$config.extensions.MathJax.mathJaxScript, function($$/extract_itex$${ MathJax.Hub.Config$$extract_itex$${ extensions: $$"tex2jax.js"$$, "HTML-CSS": { scale: 100 }, tex2jax: { inlineMath: $$['(','[/extract_itex$$'], $$'&amp;&amp;','&amp;&amp;'$$ ], displayMath: $$['[extract_tex$$','$$/extract_tex$$'], $$'[','$$'] ], processEscapes: true }, asciimath2jax: { delimiters: $$['', ''$$, $$'',''$$ ] }, styles: { ".MathJax": { color: "blue" } } });   MathJax.Hub.Startup.onload; config.extensions.MathJax.displayTiddler_old = story.displayTiddler; story.displayTiddler = config.extensions.MathJax.displayTiddler; });
2. Jason

Well done! Works very well.
There is a bug in the example above
“This is an inline equation $$extract_tex$$P$$extract_itex$$E$$/extract_itex$$ = {n \choose k} p^k $$extract_itex$$1-p$$/extract_itex$$^{ n-k}$$/extract_tex$$ and this is a displayed equation:
$$extract_itex$$J_\alpha$$extract_itex$$x$$/extract_itex$$ = \sum_{m=0}^\infty \frac{$$extract_itex$$-1$$/extract_itex$$^m}{m! \, \Gamma$$extract_itex$$m + \alpha + 1$$/extract_itex$$}{\left$$extract_itex$${\frac{x}{2}}\right$$/extract_itex$$}^{2 m + \alpha}$$/extract_itex$$”

should be

“This is an inline equation $$extract_tex$$P$$extract_itex$$E$$/extract_itex$$ = {n \choose k} p^k $$extract_itex$$1-p$$/extract_itex$$^{ n-k}$$/extract_tex$$ and this is a displayed equation:
$$extract_itex$$J_\alpha$$extract_itex$$x$$/extract_itex$$ = \sum_{m=0}^\infty \frac{$$extract_itex$$-1$$/extract_itex$$^m}{m! \, \Gamma$$extract_itex$$m + \alpha + 1$$/extract_itex$$}{\left$$extract_itex$${\frac{x}{2}}\right$$/extract_itex$$}^{2 m + \alpha}$$/extract_itex$$”

Changed ‘’ to ‘$$extract_tex$$‘ and ‘$$/extract_tex$$’ to enable inline equation formatting.

3. David Bakin

Excellent! Very easy to install and use, thanks!

Is there any way to tell if I’m using web fonts or installed STIX fonts?

Thanks again! — David

4. Veky

Any chance of supporting single-dollar  for inline equations? I tried to add them manually to switch, but it still doesn’t recognize them.

5. Brindy

Hello,

Thank you for this very useful plugin.
I noticed some problem to see equations when I am offline. This is certainly due to the activation of distant js in
mathJaxScript : “http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML”
Is there is a way to keep a correct equation viewing even when network is lost?
Brindy

6. Guy Post author

Hi Brindy,

The solution is to have a local copy of mathjax next to your TiddlyWiki. See my answer to a question regarding this issue.

7. Website Design Oakville