gt icon indicating copy to clipboard operation
gt copied to clipboard

latex in gt cols_label in revealjs

Open xtimbeau opened this issue 9 months ago • 5 comments

Latex does not work in gt table in a revealjs presentation, whereas it is working when in html.

Here is a reprex:

---
---
title: "latex gt and revealjs"
format: revealjs
---

```{r}
library(gt)
gt(mtcars[1,]) |> cols_label(mpg=md("$\\sigma$")) 
```

(and thanks for everything gt is great)

xtimbeau avatar Mar 26 '25 17:03 xtimbeau

Thanks for the report!

This is probably related to #1957.

Unfortunately, it seems that the PR in #1958 breaks this both for reveal.js and html... With the current dev version of gt and Quarto 1.6.42, I only see opening and closing brackets with both revealjs and html...

@cderv if you have insight, please let us know!

olivroy avatar Mar 26 '25 18:03 olivroy

There is known issue with Math in gt and Quarto

  • https://github.com/rstudio/gt/issues/1822

And I think it could be the same issue.

With the current dev version of gt and Quarto 1.6.42, I only see opening and closing brackets with both revealjs and html...

So maybe #1822 is now even more broken.

cderv avatar Mar 27 '25 11:03 cderv

Based on what I shared in https://github.com/rstudio/gt/issues/1822#issuecomment-2391489570 I try adding html-table-processing: none

---
format: revealjs
html-table-processing: none
keep-md: true
---

```{r}
library(gt)
gt(mtcars[1,]) |> cols_label(mpg=md("$\\sigma$")) 
```

And it works. Image

So there is something to check between

  • How md() content is handled in the context of Quarto table processing ?
  • How math is handled by gt when used in md() ?

From adding keep-md: true and looking at the HTML from gt it seems the we encode this

> rawToChar(base64enc::base64decode("XChcc2lnbWFcKQ=="))
[1] "\\(\\sigma\\)"

So it looks like processed markdown - maybe katex processing is already applied, and this is encoded to pass to Quarto but Quarto does not load Katex by default ?

Based on this assumption I tried

---
format: revealjs
keep-md: true
html-math-method: katex
---

```{r}
library(gt)
gt(mtcars[1,]) |> cols_label(mpg=md("$\\sigma$")) 
```

And guess what

Image

So I think gt needs to make some choices here.

  • If html-table-processing: none is not set, then md() content passed to data-qmd-base64 needs to always be unprocessed Markdown. This includes math, so that Quarto handles it with the rest of the document.

  • Or gt handles all the math to render using katex as a server side rendering using the R package, and in that case it needs to pass the libraries necessary for Quarto to add in the HTML doc.

We could also try to be more clever, and pass the math information somehow to R environment so that gt can decide what to do if html-math-method: katex is set or not.

Hope it helps clarifies the situation. Happy to help review PR if needed!

cderv avatar Mar 27 '25 11:03 cderv

Or gt handles all the math to render using katex as a server side rendering using the R package, and in that case it needs to pass the libraries necessary for Quarto to add in the HTML doc.

How can we do that? this seems like the simplest option?

olivroy avatar Mar 27 '25 13:03 olivroy

How can we do that? this seems like the simplest option?

The simplest solution seems to me to defer to quarto handling for processing markdown in HTML table when html-table-processing: none is not set. This would make md("$1+1$") works the same as md("**bold**") -- both would be handled by Quarto. AFAIK, outside of quarto, gt uses commonmark to render md() content to html. So probably katex should be used also only outside of quarto.

I think there is a order of processing here, where gt handles math in md(), and then see that this is quarto, base64 encode the result.

Anyhow, if you prefer that math would always be handled by katex R package, gt should have HTML dependencies for katex passed to knit_meta. Usual way to do that in knitr. However, I can't be sure how this would work in Quarto that uses Mathjax as default. For example,


---
---
format: html
---

```{r}
library(gt)
gt(mtcars[1,]) |> cols_label(mpg=md("$\\sigma$")) 
```

$1 + 1$

The math in gt would be handled using katex, while the one outside of gt would be handled using Mathajx by default, unless html_math_method: katex is set. But in that case, there could also be a conflict between gt deps, and quarto deps.

Hope this is clear.

cderv avatar Mar 27 '25 16:03 cderv