CommonMark Math in Pluto

I would like to use CommonMark as drop-in replacement for Markdown in some pluto cells but I can’t seem to understand how to make Latex math work with the cm"" string literal.

Based on the help from the @cm_str macro on Pluto and on the Readme of CommonMark.jl I was expecting the latex interpolation to work, but it apprently does not:

Can someone help me understand how to use latex parsing in the example above?
Maybe @mike can help me out on this.

Pluto renders to math when class="tex" and the content is wrapped in $ or $$ as far as I can tell, while CommonMark generates \(...\) and \[...\] wrappers with class="math" and class="display-math". If you redefine the write_html methods to follow pluto’s requirements then it should work:

function CommonMark.write_html(::CommonMark.Math, rend, node, enter)
    CommonMark.tag(rend, "span", CommonMark.attributes(rend, node, ["class" => "tex"]))
    print(rend.buffer, '$', node.literal, '$')
    CommonMark.tag(rend, "/span")
end

function CommonMark.write_html(::CommonMark.DisplayMath, rend, node, enter)
    CommonMark.tag(rend, "p", CommonMark.attributes(rend, node, ["class" => "tex"]))
    print(rend.buffer, "\$\$", node.literal, "\$\$")
    CommonMark.tag(rend, "/p")
end

I’ve not looked into what would need to be adjusted within Pluto to make the current output from CommonMark “just work”, but it may not be too difficult. The other option is to adjust what CommonMark outputs instead. Not sure which approach is best, open to opinions there. Either way this should be made to work out-of-the-box and not need any kind of configuring from users.

I confirm that the output renders correctly after (re-)defining the functions as suggested.

I also don’t know what would be the way on Pluto side to make this work, nor do I know if this will break existing functionality.

Probably @fonsp knows better.

I suppose changing this on CommonMark would be a breaking change?

Is the current behavior something coming out of the CommonMark reference standard? (Don’t know if it’s a standard or something else)

Yes, technically it would be a breaking change, but the package is still pre-1.0 so we can just do a minor version bump. I’m open to that option if it’s the easiest route.

There’s no reference for CommonMark’s (the standard itself) behaviour with regards to maths content since it’s not specified part of the spec. The closest would probably be pandoc’s output, so just following that may be an option, not sure off the top of my head what it gives as output.

Version 0.8.3 of CommonMark now supports displaying LaTeX in Pluto out of the box, no hacks or breaking changes required.

4 Likes

Do you need a package, or to know Java to do this?

You need the CommonMark package (updated to the latest version) of course but apart from that nothing else.

You already could do the same with the standard Markdown though.

CommonMark is simply better as latex somehow broke Markdown parsing in Pluto

OK, I thought it looked interesting, but I see no reason to learn if Markdown works.

If you’ve not run into any issues with the md" macro for markdown then there’s no need for you to install CommonMark.

There’s a number of corner cases and odd behaviors in Markdown that don’t match the markdown that you’ll find in other places on the web (GitHub, discourse, etc.) so if you need more consistent parsing then it’s worth trying it out in place of Markdown.