# CommonMark Math in Pluto

**URL:** https://discourse.julialang.org/t/commonmark-math-in-pluto/68104
**Category:** Pluto
**Created:** [September 13, 2021, 3:20pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104 "2021-09-13T15:20:44Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)
#### Post date: [September 13, 2021, 3:20pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/1 "2021-09-13T15:20:44Z")

</div>

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](https://github.com/MichaelHatherly/CommonMark.jl) I was expecting the latex interpolation to work, but it apprently does not:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/a/9af32e212ea02cc8d63b906693c0e35b8a836556.png)

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

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [September 13, 2021, 4:00pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/2 "2021-09-13T16:00:03Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

### Author: ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)
#### Post date: [September 13, 2021, 5:15pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/3 "2021-09-13T17:15:38Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [September 13, 2021, 6:22pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/4 "2021-09-13T18:22:37Z")

</div>

> [@disberd](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [September 23, 2021, 4:21pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/5 "2021-09-23T16:21:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [September 23, 2021, 7:12pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/6 "2021-09-23T19:12:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)
#### Post date: [September 23, 2021, 7:31pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/7 "2021-09-23T19:31:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [September 23, 2021, 7:38pm UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/8 "2021-09-23T19:38:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [September 24, 2021, 7:31am UTC](https://discourse.julialang.org/t/commonmark-math-in-pluto/68104/9 "2021-09-24T07:31:16Z")

</div>

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`.
