rstudio / blastula

Easily send great-looking HTML email messages from R
https://pkgs.rstudio.com/blastula
Other
547 stars 85 forks source link

Add support for math in blastula_email rmarkdown format #298

Open cderv opened 1 year ago

cderv commented 1 year ago

This is a POC to test how we could support math easily in blastula rmardown fomat following the improved support in rmarkdown

This first test is using webtex method which allow to use an online service to insert math as SVG (default) or PNG in HTML document. Hopefully self-contained image like SVG or PNG are ok to be included in email.

Another possible method would be using the [katex]() R package https://docs.ropensci.org/katex/index.html that we support through math_method: r-katex in rmarkdown but this will require a specific CSS file.

I don't how this CSS would behave in email client etc... This needs to be studied.

If none of this two solutions are possible for email format, this is harder and needs possibly specific adjustement somehow is possible.

Testing

Using this PR you can try rendering this document

---
title: "Math in email"
output: blastula::blastula_email
---

Testing math

$$
\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)
$$

Math formula will be inserted in email with a img and SVG data. It looks ok but require probably some CSS tweak. image

HTML code looks like

<p><img style="vertical-align:middle" src="data:image/svg+xml;base64<longencodeddatastring>" alt="\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)" title="\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)" class="math display"></p>

I don't think anything else is inserted in the document - to check.

@rich-iannone sharing that so you see what is possible. Happy to discuss this with you

rich-iannone commented 1 year ago

Christoph this is great! I've been wanting the same thing in gt as well (SVG-based formulas in non-LaTeX, non-Word output) but I'm really happy to see this work here in this package. We can definitely discuss this a bit more :)

cderv commented 1 year ago

As you want. I really don't know how this behave from email. I just wanted to show here how this could be done in a R Markdown output format.

Maybe you want to make this feature opt-in, and not opt-out in case of issues ? I used the default to webtex here - you could still deactivate but let user opt-in webtex math is they agree ?

I am thinking about this because it requires internet connection for webtex to work and generate the svg.

Anyhow, if you feel good about this without testing some email client, feel free to add a news bullet and merge.

dmi3kno commented 1 year ago

Dear RMD team, anything is happening on this front? We would really, really appreciate mathjax in emails. Please? :)

image

I think the images get sanitized by google