Rendering math symbols in datatable

How can I pass latex symbol code to a datatable in R? I found this post, but I don't know JS and have no idea how to implement something like this in R.

I have also tried the LaTeX solution from this post but got a blank table in return.

MWE

library(DT)

df <- data.frame(var1 = c("A", "B"),
                 var2 = c("this string contains $\\alpha$", "this one $\\beta$"))

df %>%
  datatable(
    rownames = FALSE, escape = TRUE,
    colnames = c("Col1", "Col2"), 
    filter = "top"
  )

Suggestions?

library(DT)
library(katex)

df <- 
  data.frame(
    var1 = c("A", "B"),
    var2 = c(katex_mathml('\\alpha'), katex_mathml('\\beta')))
 

df %>%
  datatable(
    rownames = FALSE, escape = FALSE,
    colnames = c("Col1", "Col2"), 
    filter = "top"
  )

Note, katex_mathml() is not vectorized, so has to be applied to a string, meaning a character vector of length 1.

1 Like

Thank you - I just got another hit referencing katex on SO, but the same issue arises in that I was too minimal in my MWE. The latex is embedded in part of a longer string of text. I will mark this as an answer since it technically does answer what I asked. (I did edit the OP to put the symbols in larger strings)

Looking at other examples and the docs from katex to make this would I would have to use something like str_locate to find the position of the symbol, apply katex_mathml() to it, then search the string again until no more symbols are found... then move to the next row /blech.

I hope you don't mind, but I "unsolved" myself since it wasn't really a solution to what you intended to ask, and I'm also curious to know what use-case you have in mind: From the statement I quoted, it sounds like you were hoping to be able to process existing TeX/LaTeX source content rather than compose it as you construct the table df — is that correct?