Helper function to insert tex math expressions into R documentation (.rd) files. Uses Katex rendering for documentation in html format, and the appropriate latex macros for documentation rendered in pdf or plain-text.

math_to_rd(tex, ascii = tex, displayMode = TRUE, ..., include_css = TRUE)

## Arguments

tex input string with tex math expression. alternate text-only representation of the input math to show in documentation rendered to plain text format. render math in centered 2D layout, similar to  in tex. Set to FALSE to render (non-centered) inline layout for use in text. For pdf output, this corresponds to the \deqn{} and \eqn{} macros, see WRE 2.6: Mathematics additional html rendering options passed to katex.render adds the katex css file to the output. This is only required once per html webpage. Set to FALSE if you include css files into the your html head some other way.

## Value

a string with an rd fragment to be included in R documentation

## Details

Use math_to_rd() inside \Sexpr to embed math in your R package documentation pages. For example the code below can be inserted in your rd (or roxygen) source code:

\Sexpr[results=rd, stage=build]{
katex::math_to_rd(katex::example_math())
}


Which results in the following output:

$f(x)= {\frac{1}{\sigma\sqrt{2\pi}}}e^{- {\frac {1}{2}} (\frac {x-\mu}{\sigma})^2}$

Optionally you can specify an alternate ascii representation that will be shown in the plain-text format rendering of the documentation:

\Sexpr[results=rd, stage=build]{
katex::math_to_rd('E=MC^2', 'E=mc²')
}


$E=MC^2$

If no ascii representation is given, the input tex in displayed verbatim into the plain-text documentation.

## Note for Windows

R versions before 4.1.1 had a bug on Windows which could lead to incorrect HTML characters for \Sexpr{} output. This bug hits when the source package gets built on Windows (i.e. when html manual pages are generated for \Sexpr{} with stage=build). Therefore, package developers on Windows should preferably use R 4.1.1 or later to build and release source packages containing \Sexpr{} code. Linux and MacOS are unaffected.

## See also

Other katex: katex