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() {}
CodePudding user response:
Just to demonstrate that it's possible once you've integrated
HTML output
mathjaxr::preview_rd("add.Rd", type = "html")
Plain text output
mathjaxr::preview_rd("add.Rd", type = "txt")
A few remarks
- We are only escaping back slashes because we are typing the contents of
add.R
into a string. The text file created bycat
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.- We are using macro
\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. - In the PDF field, you do not have access to AMS extensions, so you have to implement
cases
yourself witharray
. - In both the PDF and HTML fields, you need to use
\cr
in place of the usual\\
in multiline environments. I'm not sure why. - I haven't figured out how to encode line breaks in the plain text field (yet). It's going to be hard to translate multiline equations to plain text without line breaks...
- I disabled Markdown support for this header with
@noMd
because, in my experience, the Markdown parser doesn't always play nicely with themathjaxr
macros. In my own packages, I disable Markdown support globally viaDESCRIPTION
. - You need
@importFrom mathjaxr preview_rd
somewhere in your package to circumventR CMD check
warnings about havingmathjaxr
in yourImports
without using any functions exported bymathjaxr
. - To preview help pages that use MathJax without installing your package, you need to use
mathjaxr::preview_rd
. Thedevtools
preview will show you unrendered LaTeX code. - You'll have to decide whether MathJax support is worth having
mathjaxr
in yourImports
. Anyone who tries to install your package is going to have to installmathjaxr
, too.
Cleaning up
setwd(cwd)
unlink(tmp, recursive = TRUE)
CodePudding user response:
Base on the solution of Mikael, a cleaner version:
#' A title
#'
#' @param a,b Arguments.
#'
#' @section options:
#' - `item1`:
#' \mjtdeqn{
#' F(t) = \left\lbrace\begin{array}{ll}
#' \alpha(t) f_{L}(t) (1 - \alpha(t)) f_{C}(t), & t_{L} < t < t_{C}, \cr
#' \beta(t) f_{C}(t) (1 - \beta(t)) f_{R}(t), & t_{C} < t < t_{R}. \end{array}\right.}{%
#' F(t) = \begin{cases}
#' \alpha(t) f_{L}(t) (1 - \alpha(t)) f_{C}(t), & t_{L} < t < t_{C}, \cr
#' \beta(t) f_{C}(t) (1 - \beta(t)) f_{R}(t), & t_{C} < t < t_{R}. \end{cases}}{}
#'
#' - item2
#'
#' \loadmathjax{}
#' @export
#' @importFrom mathjaxr preview_rd
add <- function(a, b) a b
Markdown
is compatible withmathjaxr
in this example
mathjaxr::preview_rd("add.Rd", type = "pdf")
mathjaxr::preview_rd("add.Rd", type = "html")
Above scripts returns the same result as Mikael's.