Skip to content

Commit

Permalink
Detect Quarto and protect MathJax .
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurdoch committed May 6, 2024
1 parent 54ae27d commit afd9512
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: kableExtra
Type: Package
Title: Construct Complex Table with 'kable' and Pipe Syntax
Version: 1.4.0.4
Version: 1.4.0.5
Authors@R: c(
person('Hao', 'Zhu', email = 'haozhu233@gmail.com', role = c('aut', 'cre'),
comment = c(ORCID = '0000-0002-3386-6076')),
Expand Down
14 changes: 14 additions & 0 deletions R/kbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ kbl <- function(x, format, digits = getOption("digits"),
table.envir = table.envir, ...
)
} else if (format == "html") {
if (inQuarto()) {
for (i in seq_len(ncol(x))) {
col <- x[, i]
if (is.character(col)) {
hasMath <- grep("\\$.*\\$", col)
if (length(hasMath)) {
if (escape)
warning("Should use `escape = FALSE` in Quarto with math")
col[hasMath] <- make_data_qmd(col[hasMath])
x[, i] <- col
}
}
}
}
out <- knitr::kable(
x = x, format = format, digits = digits,
row.names = row.names, col.names = col.names, align = align,
Expand Down
12 changes: 12 additions & 0 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,15 @@ md_table_parser <- function(md_table) {
toprule_regexp <- "(\\\\toprule(\\[[^]]*])?)"
midrule_regexp <- "(\\\\midrule(\\[[^]]*])?)"
bottomrule_regexp <- "(\\\\bottomrule(\\[[^]]*])?)"

# Detect if we are running in Quarto

inQuarto <- function()
!is.null(knitr::opts_knit$get("quarto.version"))

# Modify math so Quarto handles it properly
# Written by Christophe Dervieux in issue #746

make_data_qmd <- function(content) {
sprintf('<span data-qmd="%s">%s</span>', content, content)
}
7 changes: 6 additions & 1 deletion inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
kableExtra 1.4.0.4
kableExtra 1.4.0.5
--------------------------------------------------------------------------------

New Features:

* Now attempts to handle MathJax in Quarto documents.
This requires you to call `kbl(escape = FALSE)` (#746).

Bug Fixes:

* Fixed a bug in `collapse_rows()`, which failed on tables
Expand Down

0 comments on commit afd9512

Please sign in to comment.