MyNixOS website logo
Description

Using 'Mathjax' in Rd Files.

Provides 'MathJax' and macros to enable its use within Rd files for rendering equations in the HTML help files.

mathjaxr: Using Mathjax in Rd Files

R build status CRAN Version devel Version

Description

The mathjaxr package allows for easy inclusion of MathJax equations in Rd files. Package authors wanting to make use of the package and its functionality need to:

  1. install the mathjaxr package,
  2. add mathjaxr to Imports in the DESCRIPTION file of their package,
  3. add mathjaxr to RdMacros in the DESCRIPTION file of their package (or add RdMacros: mathjaxr if the DESCRIPTION file does not yet contain a RdMacros entry),
  4. add import(mathjaxr) in the NAMESPACE file of their package.

One can then enable the use of MathJax by calling the \loadmathjax macro (that is provided by the mathjaxr package) within the \description{} section of an Rd file (or within the @description section when using roxygen2).

An inline equation can then be added with the \mjeqn{latex}{ascii} macro, with the LaTeX commands for the equation given between the first set of curly brackets (which will be rendered in the HTML and PDF help pages) and the plain-text version of the equation given between the second set of curly brackets (which will be shown in the plain text help). With the \mjdeqn{latex}{ascii} macro, one can add 'displayed equations' (as in LaTeX's displaymath environment).

Single argument versions of these macros, \mjseqn{latexascii} and \mjsdeqn{latexascii}, are also available. For the relatively rare case that one must specify different LaTeX commands for the PDF and HTML pages, there are also triple argument versions of these macros, namely \mjteqn{pdflatex}{htmllatex}{ascii} and \mjtdeqn{pdflatex}{htmllatex}{ascii}.

Details

The Javascript code for MathJax is contained in this package. If a user viewing a help page has mathjaxr installed, it will be retrieved from there, otherwise it will be retrieved from the CDN site https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js. To force use of the CDN site, the user can set the environment variable MATHJAXR_USECDN to any non-blank value (e.g., Sys.setenv(MATHJAXR_USECDN=TRUE)). The URL for a diferent CDN can be specified via the environment variable MATHJAXR_CDN.

Notes/Issues

  • Care must be taken when using the less-than and greater-than symbols in equations as these might get interpreted by the browser as HTML tags. See here for further details. Adding space around these symbols should solve this problem (e.g., instead of writing \mjseqn{i<j}, write \mjseqn{i < j}). Do not use the \lt and \gt macros provided by MathJax as these will cause problems when rendering the PDF help pages.
  • Curly braces/brackets in equations also cause problems. Using \lbrace and \rbrace (possibly in combination with \left and \right to make them sufficiently large) is a solution (e.g., \mjeqn{\left\lbrace ... \right\rbrace}{\{...\}} should render nicely in the PDF/HTML help pages and the plain-text version).
  • Using the percent symbol (i.e., %) inside of equations is also problematic. The percent symbol needs to be 'escaped' by using a backslash, but backslashes need to be escaped as well. For this to work, we need to use the correct number of backslashes, which works slightly differently when producing the PDF, HTML, and plain-text help pages. The equation \mjteqn{100\\\%}{100\\\\\\\%}{100\\%} should be rendered correctly in all three help pages.
  • While MathJax supports a large number of LaTeX commands, only the math-mode commands are implemented. See here for a list of the supported commands.
  • When using R via an RStudio Server, MathJax equations in the help files are not automatically rendered (this is not a problem on RStudio Desktop). A solution to get the rendering to work in RStudio Server is to add server-add-header=X-Content-Type-Options: to /etc/rstudio/rserver.conf (and then restarting the server). This of course requires admin rights on the machine running the server.

Example

The probability density function of a normal distribution is given by

where \mu denotes the mean of the distribution and \sigma its standard deviation.

Installation

The current official (i.e., CRAN) release can be installed within R with:

install.packages("mathjaxr")

The development version of the package can be installed with:

install.packages("remotes")
remotes::install_github("wviechtb/mathjaxr")

This builds the package from source based on the current version on GitHub. Note that Windows users must have Rtools installed for this to work.

Meta

The mathjaxr package is licensed under the GNU General Public License Version 3, while MathJax itself is licensed under the Apache License, Version 2.0. To report any issues or bugs, please go here.

Metadata

Version

1.6-0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows