Fork me on GitHub
#nextjournal
<
2023-01-05
>
pmooser10:01:35

I'm trying the nextjournal markdown component. Has anyone had any luck getting LaTeX formulae to render?

Andrea13:01:25

Hi @U07VBK5CJ nextjournal markdown is more about the document data structure, but has also a transformation layer into hiccup (which can be used both on clj and on cljs) which can be customized on a node-type based, this should be explained in the README: https://nextjournal.github.io/markdown/#/README.md

pmooser14:01:51

Hi @andrea712 - thank you for your reply. I did check the readme, but I was confused since it says "we parse LaTeX formulas", but I never saw any rendering being done at all, and of course LaTeX wasn't being parsed - the delimited blocks were just skipped. Other things that contributed to my confusion: • there's a direct dependency on markdown-it-texmath (leading me to believe it should be using it) • the "try it online" link in the github repo links to something that does render LaTeX markup, but there's a layer of code added to make it do so Anyway, I appreciate the help, and I do have it working now. I just didn't understand whether what I was seeing was correct behavior - it was - and wasn't sure exactly how to get what I wanted. At the very bottom of the linked README page, the example showing the transform with augmented hiccup renderers eventually got me there, along with the source code to the clerk viewer.

Andrea15:01:09

right, the built-in hiccup renderers in the markdown library are pretty basic and the demo uses components from yet another library (which will probably be deprecated in the future) https://github.com/nextjournal/viewers/blob/main/modules/viewer/src/nextjournal/viewer/markdown.cljs#L25-L41 and it’s not mentioned in the try notebook. So it’s probably truly confusing. The markdown-it-texmath is used as a markdown-it plugin for the parsing side, but not for rendering. We’re not using markdown-it for rendering html. Glad, you got it working in the end.

pmooser10:01:53

The documentation is a little sparse on this matter, unless there's something I'm missing.

pmooser10:01:35

The component is clearly detecting $ and $$ but I don't see any actual rendered math. Maybe there's some preamble I'm missing ...

pmooser10:01:56

I can verify that it works as expected with their online demo.

pmooser10:01:32

I think I need to not just pass the hiccup to reagent, but to one of their renderers.

pmooser11:01:55

I see that clerk has some extra render-katex and render-mathjax stuff that was of course never mentioned in the markdown library.

mkvlr12:01:28

PR welcome, of course

pmooser12:01:48

I'm still trying to figure out how to make it work. I'll try to be as helpful as I can with feedback afterwards.

mkvlr12:01:38

the markdown lib just does the parsing, it doesn’t come formula rendering

mkvlr12:01:42

what are you doing with the result of the parse, how do you want to render it?

pmooser12:01:08

Yes, that's just difficult to understand given that the library does depend on markdown-it-texmath and indirectly on katex, and the "demo" is a demo using clerk, so of course it works with formulae. So I assumed it would be a bit more batteries included, but that is my mistake.

pmooser12:01:38

I was just trying to evaluate if it would be easier to use this markdown library instead of what I've done up util now, which is to use markdown-clj and integrate with MathJax directly.

pmooser12:01:35

I just need to understand the viewer code a little better (heh, maybe a lot better) in clerk and then that should provide me everything I need, I think.

mkvlr12:01:06

@andrea712 is just out for lunch

pmooser12:01:12

Sorry, and to answer your question directly - I want to render it in a browser. Like, have markdown which defines a document with embedded formulae.

pmooser12:01:44

I have that working now in markdown-clj, and it seems like it will be easy with the nextjournal library as well. I just have to learn it a bit better.

pmooser12:01:00

Yes, I did find that.

pmooser12:01:34

What I'm puzzling through now is clerk's viewers and in particular where in the pipeline we get to :nextjournal.markdown/formula ...

mkvlr12:01:32

yeah this indeed a bit magic and should be clarified

pmooser12:01:36

I'm probably being a bit unclear -

pmooser12:01:22

I'm looking at the very nice parse tree coming out of the library, but it doesn't consist of namespaced keywords for the different categories, but rather just outputs :formula (or :block-formula or whatever).

mkvlr12:01:57

why would you expect namespaced keywords?

pmooser12:01:01

Ah hm, thank you. I will check out how :renderers works.

pmooser12:01:30

I would expect namespaced keywords because they are present in this snippet from clerk's viewer:

;; formulas
   {:name :nextjournal.markdown/formula
    :transform-fn (comp :text ->value)
    :render-fn '(fn [tex] (nextjournal.clerk.render/render-katex tex {:inline? true}))}
   {:name :nextjournal.markdown/block-formula
    :transform-fn (comp :text ->value)
    :render-fn 'nextjournal.clerk.render/render-katex}

pmooser12:01:42

But I think that's related to what you just linked to, so I just have to wrap my head around it a bit more.

pmooser12:01:15

I think maybe if I add that katex handling to md.transform/default-hiccup-renderers that will work?

pmooser12:01:19

(with an entry for :formula)

mkvlr12:01:20

so the namespaced keywords are from clerk

mkvlr12:01:08

yes, overriding the transform for :formula and :block-formula should do

pmooser12:01:29

Ok - thank you for all your help. I need to digest this and give it a go and hopefully I will get it working.

pmooser12:01:42

One other thing I don't understand is that clerk's version of katex differs from the markdown library's version. (unless I'm mistaken)

pmooser12:01:38

Got it working!

🙌 8