Skip to content

Commit 33d7e68

Browse files
committed
Add mathjax support via cdn
1 parent 1dab5e4 commit 33d7e68

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

R/layout.R

+8-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ rangeslider <- function(p, start = NULL, end = NULL, ...) {
117117
#' config(p, locale = "zh-CN")
118118
#'
119119

120-
config <- function(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL) {
120+
config <- function(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL, mathjax = FALSE) {
121121

122122
if (!is.null(locale)) {
123123
p$dependencies <- c(
@@ -127,6 +127,13 @@ config <- function(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL) {
127127
p$x$config$locale <- locale
128128
}
129129

130+
if (!identical(mathjax, FALSE)) {
131+
p$dependencies <- c(
132+
list(mathjax_dependency()),
133+
p$dependencies
134+
)
135+
}
136+
130137
p$x$config <- modify_list(p$x$config, list(...))
131138

132139
nms <- sapply(p$x$config[["modeBarButtonsToAdd"]], "[[", "name")

R/mathjax.R

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
mathjax_cdn <- function() {
2+
htmltools::htmlDependency(
3+
name = "mathjax",
4+
version = "2.7.4",
5+
src = c(file = depPath("mathjax")),
6+
script = "cdn.js"
7+
)
8+
}
9+
10+
# TODO: wait until there is a more official way to include query parameters
11+
# https://github.com/rstudio/htmltools/issues/98
12+
mathjax_local <- function() {
13+
htmltools::htmlDependency(
14+
name = "mathjax",
15+
version = "2.7.4",
16+
src = c(file = mathjax_path()),
17+
script = "MathJax.js?config=TeX-AMS-MML_SVG"
18+
)
19+
}
20+
21+
22+
mathjax_path <- function() {
23+
path <- Sys.getenv("PLOTLY_MATHJAX_PATH", NA)
24+
if (!is.na(path)) {
25+
mj <- file.path(path, "MathJax.js")
26+
if (!file.exists(mj)) stop("Couldn't find 'MathJax.js' file in local directory")
27+
return(path)
28+
}
29+
30+
if (system.file(package = "rmarkdown") != "") {
31+
# Note, this should throw an informative error if the path isn't found
32+
return(getFromNamespace("pandoc_mathjax_local_path", "rmarkdown")())
33+
}
34+
35+
stop(
36+
"To use a local version of MathJax with plotly, either set the PLOTLY_MATHJAX_PATH",
37+
"environment variable to the location of MathJax or install rmarkdown.",
38+
call. = FALSE
39+
)
40+
}

R/orca.R

-16
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,3 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file),
7171
try_library("processx", "orca")
7272
invisible(processx::run("orca", args, echo = TRUE, spinner = TRUE))
7373
}
74-
75-
76-
mathjax_path <- function() {
77-
if (is_rstudio()) {
78-
try_library("rmarkdown", "orca")
79-
return(getFromNamespace("pandoc_mathjax_local_path", "rmarkdown")())
80-
}
81-
path <- Sys.getenv("PLOTLY_MATHJAX_PATH", Sys.getenv("RMARKDOWN_MATHJAX_PATH", NA))
82-
if (!is.na(path)) return(normalizePath(path, mustWork = TRUE))
83-
stop(
84-
"Please set either the RMARKDOWN_MATHJAX_PATH or PLOTLY_MATHJAX_PATH ",
85-
"environment variable to the location of MathJax. ",
86-
"On Linux systems you can also install MathJax using your system package manager.",
87-
call. = FALSE
88-
)
89-
}

inst/htmlwidgets/lib/mathjax/cdn.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var script = document.createElement("script");
2+
script.type = "text/javascript";
3+
script.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS-MML_SVG";
4+
document.getElementsByTagName("head")[0].appendChild(script);

0 commit comments

Comments
 (0)