I am writing R package documentation with roxygen2
. I want to insert the following multiline equation into a help page, but my LaTeX code is not being rendered.
#' hello2
#'
#' @description
#' \deqn{
#' F(t)= \begin{cases}\alpha(t) f_{L}(t)+[1-\alpha(t)] f_{C}(t) & t_{L}<t<t_{C} \\ \beta(t) f_{C}(t)+[1-\beta(t)] f_{R}(t) & t_{C}<t<t_{R}\end{cases}
#' }
#'
#' @export
hello2 <- function() {}
Support for KaTeX in HTML rendering of Rd files was added in R 4.2.0. Support for amsmath in PDF rendering of Rd files was added in R 4.2.2. Hence packages with Depends: R (>= 4.3)
can safely specify multiline equations using \deqn
. No need for mathjaxr.
Just to demonstrate that it's possible once you've integrated mathjaxr correctly:
tmp <- tempfile()
dir.create(tmp)
cwd <- setwd(tmp)
pkgname <- "foo"
usethis::create_package(pkgname, rstudio = FALSE, open = FALSE,
fields = list(Imports = "mathjaxr", RdMacros = "mathjaxr"))
setwd(pkgname)
text <- "#' A title
#'
#' \\loadmathjax{}
#' A description.
#'
#' @param a,b Arguments.
#'
#' @details
#' An irrelevant equation:
#' \\mjtdeqn{F(t) = \\left\\lbrace\\begin{array}{ll} \\alpha(t) f_{L}(t) + \\lbrack 1 - \\alpha(t) \\rbrack f_{C}(t)\\,, & t_{L} < t < t_{C}\\,, \\cr \\beta(t) f_{C}(t) + \\lbrack 1 - \\beta(t) \\rbrack f_{R}(t)\\,, & t_{C} < t < t_{R}\\,. \\end{array}\\right.}{%
#' F(t) = \\begin{cases} \\alpha(t) f_{L}(t) + \\lbrack 1 - \\alpha(t) \\rbrack f_{C}(t)\\,, & t_{L} < t < t_{C}\\,, \\cr \\beta(t) f_{C}(t) + \\lbrack 1 - \\beta(t) \\rbrack f_{R}(t)\\,, & t_{C} < t < t_{R}\\,. \\end{cases}}{%
#' ... a plain text translation ...}
#'
#' @noMd
#' @export
#' @importFrom mathjaxr preview_rd
add <- function(a, b) a + b
"
cat(text, file = file.path("R", "add.R"))
roxygen2::roxygenize(".")
mathjaxr::preview_rd("add.Rd", type = "pdf")
mathjaxr::preview_rd("add.Rd", type = "html")
mathjaxr::preview_rd("add.Rd", type = "txt")
add.R
into a string. The text file created by cat
does not contain the escapes.\loadmathjax{}
injects a MathJax script into the HTML file. It is usually placed at the top of the description, so that you can typeset math everywhere after that.\mjtdeqn
to supply LaTeX for the PDF manual, LaTeX for the HTML help page, and plain text for the plain text help page, in that order. There are other macros that you can use in simpler cases. They are all documented in the README, which you should read carefully.cases
yourself with array
.\cr
in place of the usual \\
in multiline environments. I'm not sure why.@noMd
because, in my experience, the Markdown parser doesn't always play nicely with the mathjaxr macros. In my own packages, I disable Markdown support globally via DESCRIPTION
.@importFrom mathjaxr preview_rd
somewhere in your package to circumvent R CMD check
warnings about having mathjaxr in your Imports
without using any functions exported by mathjaxr.mathjaxr::preview_rd
. The devtools preview will show you unrendered LaTeX code.Imports
. Anyone who tries to install your package is going to have to install mathjaxr, too.setwd(cwd)
unlink(tmp, recursive = TRUE)