reagent

Bobbi Towers 2023-05-29T21:14:10.630529Z

I have a reagent app that uses katex, and I'm having trouble including a formula as an SVG element

Bobbi Towers 2023-05-29T21:15:20.574699Z

this is my tex rendering function

(defn tex [text]
  [:span {:ref (fn [el]
                 (when el
                   (try
                     (katex/render text el (clj->js
                                            {:throwOnError false}))
                     (catch :default e
                       (js/console.warn "Unexpected KaTeX error" e)
                       (aset el "innerHTML" text)))))}])

Bobbi Towers 2023-05-29T21:15:55.803949Z

it works if I put it in an h1 or whatever

Bobbi Towers 2023-05-29T21:17:32.754329Z

but fails inside an SVG text element:

[:text {:transform (str "scale(1) translate(" (+ x 150) "," (+ y 150) ")")
           :fill      "#ffcc00"} (tex "z=z\\cdot 1")]

Bobbi Towers 2023-05-29T21:18:15.489479Z

I suppose that's not how refs work

Sam Ritchie 2023-06-03T21:09:00.986699Z

@porkostomus love the theme!

❤️ 1
p-himik 2023-05-29T21:28:34.167349Z

This component will have its :ref changed on each re-render. Don't know for sure whether it's the culprit or even a problem, but I'd turn the tex function in a form-2 component and use it as a proper component - i.e. [tex ...] instead of (tex ...).

Bobbi Towers 2023-05-29T21:29:15.803489Z

ah, that makes sense

p-himik 2023-05-29T21:29:38.292879Z

And if you need to react to the changes of text, you'll have to use a form-3 component and its :component-did-update lifecycle function.

p-himik 2023-05-29T21:31:48.688109Z

Also, don't use aset for anything but arrays, as its docstring says. For setting JS fields that aren't supposed to be renamed in an optimized build, use externs inference. In this case, you can write (fn [^js el] ... (set! el -innerHTML text) ...).

Bobbi Towers 2023-05-29T21:33:02.366159Z

oh ok, I just ripped that function from some other app so I don't fully understand how it works

p-himik 2023-05-29T21:33:11.057579Z

Finally, for shallow objects and arrays with a static shape and keys you can replace (clj->js ...) with #js ..., i.e. here you can use #js {:throwOnError false} - that object will be created at compile time as opposed to transforming a CLJS map in run time.

p-himik 2023-05-29T21:33:58.012669Z

But those two things have nothing to do with your issue at hand - just a general advice.

Bobbi Towers 2023-05-29T21:35:12.913159Z

that's helpful, thanks

Bobbi Towers 2023-05-29T21:48:37.696319Z

this is the basic thing I'm trying to do, so I can probably figure it out from that https://observablehq.com/@mcmcclur/overlay-katex-on-top-of-an-svg

Bobbi Towers 2023-05-30T00:23:21.948779Z

I got it working! I used a foreignObject

👍 1
Bobbi Towers 2023-05-30T00:31:24.283239Z

p-himik 2023-05-30T03:10:03.811139Z

"SynthWave math". :)