Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ce99527
add math argument to html_document and _base
atusy Oct 29, 2020
f42112f
further generalization
atusy Oct 30, 2020
a5c6f38
doc
atusy Oct 30, 2020
d0b833c
fix mathjax:local not working
atusy Oct 30, 2020
267a7f5
add NEWS
atusy Oct 30, 2020
de7e21b
remove a comment
atusy Oct 31, 2020
f8b1b5e
improve support for default templates
atusy Oct 31, 2020
02005e8
fix math not working when non-default template is used
atusy Oct 31, 2020
15c6cbc
Merge branch 'main' into math
cderv Jan 18, 2022
3e0b728
Merge branch 'main' into math
cderv Jan 20, 2022
caf4eb1
Merge branch 'main' into atusy-math
cderv Jan 25, 2022
41294e5
keep the trick for now to load mathajx using JS
cderv Jan 25, 2022
da30e77
Use the same katex script than the one inserted by pandoc
cderv Jan 25, 2022
58abe7d
Refactoring of new math argument
cderv Jan 25, 2022
aef9e12
Re document
cderv Jan 25, 2022
bb3043c
Add some test for new math support
cderv Jan 25, 2022
fb49810
Missing test file
cderv Jan 25, 2022
499f7d1
mathjax local only works in RStudio IDE
cderv Jan 25, 2022
47523b4
Mathjax can also be NULL
cderv Jan 26, 2022
19fa506
Better check supported math argument
cderv Jan 26, 2022
847c68a
refactor to test backward compatibilaty
cderv Jan 26, 2022
d69bb7a
Transform math argument to list once in each format
cderv Jan 26, 2022
84e2f64
Order of argument is really important
cderv Jan 26, 2022
bb6e515
document()
cderv Jan 26, 2022
4bbaae3
Add Katex as a htmlDependency
cderv Jan 26, 2022
48c9182
handle extras dependency for katex
cderv Jan 26, 2022
1e92073
Remove KaTeX header from template as it should be included as HTML de…
cderv Jan 26, 2022
6f7c1dd
Use the HTML dependency when inserting KaTeX
cderv Jan 26, 2022
d769b42
Try adding support in `slidy_presentation()`
cderv Jan 26, 2022
db94a31
Adapt test
cderv Jan 26, 2022
043bb9a
Insure math is not set to something else than Mathjax in `ioslide_pre…
cderv Jan 26, 2022
645c684
Same as 84e2f640091c4d818aafb8a7a448c7ae0f2f2801
cderv Jan 26, 2022
d3c16e1
Rename new argument to `math_method`
cderv Jan 26, 2022
01c0784
re document()
cderv Jan 26, 2022
1bf45fe
Update NEWS and bump version
cderv Jan 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rmarkdown
Title: Dynamic Documents for R
Version: 2.11.12
Version: 2.11.13
Authors@R: c(
person("JJ", "Allaire", , "[email protected]", role = "aut"),
person("Yihui", "Xie", , "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-0645-5666")),
Expand Down
21 changes: 20 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
rmarkdown 2.12
================================================================================

- Fixed broken links to section headers when `number_sections = TRUE` is specified in `md_document` and `github_document` (thanks, #2093, @atusy).
- `html_document()` and `html_document_base()` gains the `math_method` argument to support [all the math rendering engines from Pandoc](https://pandoc.org/MANUAL.html#math-rendering-in-html): "mathjax", "katex", "mathml", "webtex", and "gladtex". For backward compatibility, `mathjax` argument is still working when `math_method = "default"` which is the default, and will take precedence is `mathjax` is different that `"default"`.
From now on, use `math_method` to customize the engine
```yaml
output:
html_document:
math_method: katex
```
or its url (for `mathjax`, `katex` and `webtex`)
```yaml
output:
html_document:
math_method:
engine: mathjax
url: https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js
```

Most HTML output format using `html_document()` or `html_document_base()` as based format should benefit from this new feature.
See `?rmarkdown::html_document()` for details (thanks, @atusy, #1940).

- Fixed broken links to section headers when `number_sections = TRUE` is specified in `md_document` and `github_document` (thanks, @atusy, #2093).

- Added `available_templates()` to list all the templates from a specific package that can be used with `rmarkdown::draft()`.

Expand Down
36 changes: 36 additions & 0 deletions R/html_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,39 @@ html_dependency_header_attrs <- function() {
)
}
}

# Store KaTeX as a html dependency to include in our template
html_dependency_katex <- function() {
cdn <- "https://cdn.jsdelivr.net/npm/[email protected]/dist/"
htmlDependency(
name = "katex", version = "0.15.2",
src = c(href = cdn),
script = list(src = "katex.min.js",
integrity = "sha256-bgI9WhLOPSUlPrdgHUg3rPIB2vq+D+mYkUTM3q0U8fw=",
crossorigin = 'anonymous',
'data-external' = "1",
defer = NA),
# TODO: reactivate when we can set data-external = 1 in htmltools
#
# stylesheet = c("katex.min.css"),
# from Pandoc
# https://github.com/jgm/pandoc/blob/7ddb609aae48ec10ef9899b25c52a4abecfbc176/src/Text/Pandoc/Writers/HTML.hs#L313
head = c(
'<script>document.addEventListener("DOMContentLoaded", function () {',
' var mathElements = document.getElementsByClassName("math");',
' var macros = [];',
' for (var i = 0; i < mathElements.length; i++) {',
' var texText = mathElements[i].firstChild;',
' if (mathElements[i].tagName == "SPAN") {',
' katex.render(texText.data, mathElements[i], {',
' displayMode: mathElements[i].classList.contains("display"),',
' throwOnError: false,',
' macros: macros,',
' fleqn: false',
' });',
' }}});',
'</script>"',
sprintf('<link rel="stylesheet" href="%skatex.min.css" data-external="1">', cdn)
)
)
}
38 changes: 28 additions & 10 deletions R/html_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@
#' MathJax CDN. The "local" option uses a local version of MathJax (which is
#' copied into the output directory). You can pass an alternate URL or pass
#' \code{NULL} to exclude MathJax entirely.
#'@param math_method Math rendering engine to use. This will define the math method to use with Pandoc.
#'
#' * It can be a string for the engine, one of
#' `r knitr::combine_words(pandoc_math_engines(), and = "or ", before = "'")`,
#' or `default` for `mathjax`.
#' * It can be a list of
#' * `engine`: one of
#' `r knitr::combine_words(pandoc_math_engines(), and = "or ", before = "'")`.
#' * `url`: A specific url to use with `mathjax`, `katex` or `webtex`.
#' Note that for `engine = "mathjax"`, `url = "local"` will use a local version of MathJax (which is
#' copied into the output directory).
#'
#' For example,
#' ```yaml
#' output:
#' html_document:
#' math_method:
#' engine: katex
#' url: https://cdn.jsdelivr.net/npm/[email protected]/dist
#' ```
#'
#' See [Pandoc's Manual about Math in HTML](https://pandoc.org/MANUAL.html#math-rendering-in-html) for the details.
#' Note that `c(mathjax = "local")` is equivalent to specifying "local" to
#' `mathjax`.
#'@param section_divs Wrap sections in \code{<div>} tags, and attach identifiers to the
#' enclosing \code{<div>} rather than the header itself.
#'@param template Pandoc template to use for rendering. Pass "default" to use
Expand Down Expand Up @@ -333,6 +357,7 @@ html_document <- function(toc = FALSE,
theme = "default",
highlight = "default",
highlight_downlit = FALSE,
math_method = "default",
mathjax = "default",
template = "default",
extra_dependencies = NULL,
Expand Down Expand Up @@ -612,7 +637,9 @@ html_document <- function(toc = FALSE,
on_exit = on_exit,
base_format = html_document_base(theme = theme,
self_contained = self_contained,
lib_dir = lib_dir, mathjax = mathjax,
lib_dir = lib_dir,
math_method = math_method,
mathjax = mathjax,
template = template,
pandoc_args = pandoc_args,
extra_dependencies = extra_dependencies,
Expand Down Expand Up @@ -673,15 +700,6 @@ html_highlighters <- function() {
c(highlighters(), "textmate")
}

default_mathjax <- function() {
paste0("https://mathjax.rstudio.com/latest/", mathjax_config())
Comment thread
cderv marked this conversation as resolved.
Resolved
}

mathjax_config <- function() {
"MathJax.js?config=TeX-AMS-MML_HTMLorMML"
}


navbar_html_from_yaml <- function(navbar_yaml) {

# parse the yaml
Expand Down
138 changes: 130 additions & 8 deletions R/html_document_base.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
html_document_base <- function(theme = NULL,
self_contained = TRUE,
lib_dir = NULL,
math_method = "default",
mathjax = "default",
pandoc_args = NULL,
template = "default",
Expand All @@ -35,14 +36,25 @@ html_document_base <- function(theme = NULL,

args <- c()

# backward compatibility for math /mathjax argument
# math is used only when `mathjax = "default"`
math <- mathjax_to_math(mathjax, math_method)
# any math to list(engine = , url = )
math <- check_math_argument(math)

# self contained document
if (self_contained) {
if (copy_resources)
stop("Local resource copying is incompatible with self-contained documents.")
validate_self_contained(mathjax)
validate_self_contained(math)
args <- c(args, "--self-contained")
}

# math support
math_support <- add_math_support(math, template, lib_dir, output_dir)
args <- c(args, math_support$args)
extra_dependencies <- c(extra_dependencies, math_support$extra_dependencies)

# custom args
args <- c(args, pandoc_args)

Expand Down Expand Up @@ -139,13 +151,6 @@ html_document_base <- function(theme = NULL,
args <- c(args, pandoc_html_extras_args(extras, self_contained, lib_dir,
output_dir))

# mathjax
args <- c(args, pandoc_mathjax_args(mathjax,
template,
self_contained,
lib_dir,
output_dir))

preserved_chunks <<- extract_preserve_chunks(input_file)

args
Expand Down Expand Up @@ -245,3 +250,120 @@ extract_preserve_chunks <- function(input_file, extract = extractPreserveChunks)
if (!identical(preserve$value, input_str)) write_utf8(preserve$value, input_file)
preserve$chunks
}


# Math support ------------------------------------------------------------

default_mathjax <- function() {
paste0("https://mathjax.rstudio.com/latest/", mathjax_config())
}

mathjax_config <- function() {
"MathJax.js?config=TeX-AMS-MML_HTMLorMML"
}

add_math_support <- function(math, template, files_dir, output_dir) {

# check math argument should be already or NULL
# (list(engine = "", url = "")

extras <- NULL

if (is.null(math)) return(NULL)

# handle different engines
# TODO: engine to use r-katex package

# Default
if (identical(math$engine, "default")) math$engine <- "mathjax"

# Check supported
if (!math$engine %in% c(pandoc_math_engines())) {
stop2(sprintf("`math_method='%s'` is not supported.", math$engine))
}

# support only mathjax for Pandoc before 2.0+
if (!pandoc2.0() && !identical(math$engine, "mathjax")) {
stop2("only `math_method = 'mathjax'` is supported with earlier version than Pandoc 2.0 ")
}

# No special handling needed for most engine
if (math$engine %in% setdiff(pandoc_math_engines(), c("mathjax", "katex"))) {
return(list(args = pandoc_math_args(math$engine, math$url)))
}

# MATHJAX OR KATEX needs special handling

if (identical(math$engine, "katex")) {
if (identical(template, "default")) {
args <- pandoc_math_args(math$engine)
extras <- list(html_dependency_katex())
} else {
args <- c(pandoc_math_args(math$engine, math$url))
}
} else if (identical(math$engine, "mathjax")) {
if (identical(math$url, "local")) {
# local supported for mathjax only
mathjax_path <- render_supporting_files(
pandoc_mathjax_local_path(),
files_dir,
"mathjax-local")
math$url <- paste(normalized_relative_to(output_dir, mathjax_path), "/",
mathjax_config(), sep = "")
}
if (identical(template, "default")) {
args <- c(
pandoc_math_args(math$engine),
pandoc_variable_arg("mathjax-url", math$url %||% default_mathjax())
)
} else {
args <- c(pandoc_math_args(math$engine, math$url))
}
}

list(args = args, extra_dependencies = extras)
}

check_math_argument <- function(math) {
url <- NULL
engine <- NULL
# math is deactivated
if (is.null(math)) return(NULL)
# otherwise
if (is.list(math) && !is.null(names(math))) {
# can be a named list
engine <- if (is.character(math$engine)) math$engine
url <- math$url
} else if (length(math) == 1L && is.character(math[[1L]])) {
# can be a string
engine <- math[[1L]]
}

# if no engine found, incorrect value must have been provided
if (is.null(engine)) {
stop2("'math' can be the engine name (a string) or a list with engine and optionnaly the url to use.")
}

list(engine = engine, url = url)
}

mathjax_to_math <- function(mathjax, math) {
if (is.null(mathjax)) {
# deactivate math
return(NULL)
} else if (identical(mathjax, "default")) {
# default to other argument now
return(math)
} else if (identical(mathjax, "local")) {
# use local mathajx
return(list(engine = "mathjax", url = "local"))
} else if (is.logical(mathjax)) {
# let mathjax = FALSE deactivate
return(if(isTRUE(mathjax)) math)
} else if (is.character(mathjax)) {
# any other string should be a mathjax url
return(list(engine = "mathjax", url = mathjax))
}
# just return math for any other value
math
}
2 changes: 2 additions & 0 deletions R/html_notebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ html_notebook <- function(toc = FALSE,
theme = "default",
highlight = "textmate",
highlight_downlit = FALSE,
math_method = "default",
mathjax = "default",
extra_dependencies = NULL,
css = NULL,
Expand Down Expand Up @@ -206,6 +207,7 @@ html_notebook <- function(toc = FALSE,
theme = theme,
highlight = highlight,
highlight_downlit = highlight_downlit,
math_method = math_method,
mathjax = mathjax,
extra_dependencies = extra_dependencies,
css = css,
Expand Down
8 changes: 8 additions & 0 deletions R/ioslides_presentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ ioslides_presentation <- function(number_sections = FALSE,
widescreen = FALSE,
smaller = FALSE,
transition = "default",
math_method = "mathjax",
mathjax = "default",
analytics = NULL,
template = NULL,
Expand All @@ -276,6 +277,13 @@ ioslides_presentation <- function(number_sections = FALSE,
# base pandoc options for all output
args <- c()

# math
math <- mathjax_to_math(mathjax, math_method)
math <- check_math_argument(math)
if (!identical(math$engine, "mathjax")) {
stop2("Only mathjax is supported for `ioslide_presentation()` for 'math'.")
}

# widescreen
if (widescreen)
args <- c(args, "--variable", "widescreen");
Expand Down
Loading