[IPython-dev] ipython html notebook: markdown in math mode and backslashes

Bob McElrath bob+ipython at mcelrath.org
Thu Aug 23 15:03:09 EDT 2012

I hate to be a downer, but having implemented latex + wiki several times (ZWiki,
jsMath+TiddlyWiki, etc), I can tell you that this solution is a hack that will
fail eventually.  For that matter, it appears that Pagedown itself is a hack
too, that will have bad edge cases.  (In other words it is a pile of
successively applied regular expressions, in which one hopes there are not
overlapping cases -- rather than a grammar which has no overlapping cases by

One has two grammars that are mixed: markdown and latex.  You will always have a
problem of markdown-looking syntax inside latex or latex-looking syntax inside
markdown.  The only workable solution is to be able to entirely *escape* one
inside the other, which makes the document unambiguous.  The best ways to find
such bugs is usually to try to write a document that demonstrates how to write
math, or to write text in which one parser overlaps with the other like *$*$, or
put markup inside comments.

The correct solution to this problem is therefore to use a true grammar that
involves an escape.  Since latex uses the backslash its escape character, and so
does markdown, this seems naively easy, but one must hook into to markdown's
processing stream, and call MathJax from it, rather than invoking two
independent processing sessions (markdown followed by MathJax or v/v).

Markdown-js: https://github.com/evilstreak/markdown-js appears to be a much
better written parser than Pagedown, in which inserting an extra rule for math,
and calling MathJax would be straightforward (disabling MathJax's document

Correct me if I'm wrong, but this penchant for writing functions that generate
classes that everyone is using basically makes it impossible to use inheritance
to extend a Markdown parser.  (I don't understand why iPython, Markdown-js, and
everyone in the known universe has decided this is The Way to write everything
-- maybe someone can enlighten me)  The method I'm proposing requires modifying
the Markdown parser's internals.  So iPython would have to fork or Markdown-js
to do this.  It's MIT license, I don't think that would be a problem.

Fernando Perez [fperez.net at gmail.com] wrote:
> On Thu, Aug 23, 2012 at 9:41 AM, MinRK <benjaminrk at gmail.com> wrote:
> >
> > Yes, absolutely!  If they have already solved the problem, then let's use an
> > existing and proven solution.
> Major +1 from me on this; Aron, it would be awesome if you could make
> this happen!  The latex bugs have been driving many people nuts as of
> late, so this would be a phenomenal fix to have (and candidate for a
> 0.13.1 backport).
> Cheers,
> f
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
Cheers, Bob McElrath

"The individual has always had to struggle to keep from being overwhelmed by
the tribe.  If you try it, you will be lonely often, and sometimes frightened.
But no price is too high to pay for the privilege of owning yourself." 
    -- Friedrich Nietzsche

More information about the IPython-dev mailing list