然后您可能会构建一个katex_htmls()
,用"katex html"的翻译替换每个$之间的tex:
library(dplyr)
library(DT)
library(katex)
library(purrr)
library(stringr)
# vectorized katex_html() with sensible settings for non-interactive use
katex_html_vec <- function(
tex,
displayMode = FALSE,
...,
include_css = TRUE,
preview = FALSE
) {
purrr::map_chr(tex, ~ {
katex::katex_html(
.x,
displayMode = displayMode,
...,
include_css = include_css,
preview = preview
)
})
}
# katex_html_vec() to process 0 or more $tex$s per string
katex_htmls <- function(
x,
displayMode = FALSE,
...,
include_css = TRUE,
preview = FALSE
) {
purrr::map_chr(x, ~ {
stringr::str_replace_all(.x, "\\$.*?\\$", function(m) {
m %>%
stringr::str_remove_all("\\$") %>%
katex_html_vec(
displayMode = displayMode,
...,
include_css = include_css,
preview = preview
)
})
})
}
data.frame(
var1 = c("A", "B"),
var2 = c("this string contains $\\alpha$", "this one $\\beta$")
) %>%
mutate(var2 = katex_htmls(var2)) %>%
datatable(
rownames = FALSE,
colnames = c("Col1", "Col2"),
filter = "top",
escape = FALSE
)
创建于2024年4月19日,共有reprex v2.1.0个