Title: | Rendering Math to HTML, 'MathML', or R-Documentation Format |
---|---|
Description: | Convert latex math expressions to HTML and 'MathML' for use in markdown documents or package manual pages. The rendering is done in R using the V8 engine (i.e. server-side), which eliminates the need for embedding the 'MathJax' library into your web pages. In addition a 'math-to-rd' wrapper is provided to automatically render beautiful math in R documentation files. |
Authors: | Jeroen Ooms [aut, cre] , Khan Academy and other contributors [cph] (KaTeX JavaScript library) |
Maintainer: | Jeroen Ooms <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.5.0 |
Built: | 2024-11-27 03:54:08 UTC |
Source: | https://github.com/ropensci/katex |
Converts tex-style math expressions to html and mathml for use in manual pages or
markdown documents.
The conversion is done in R using V8 ("server-side"), hence the resulting fragment can
be inserted into an HTML document without the need for a JavaScript library like MathJax.
Only the katex.css
style file is required in the final html document.
Use math_to_rd for embedding math into R documentation (.rd
) pages.
katex_html( tex, displayMode = TRUE, ..., include_css = FALSE, preview = interactive() ) katex_mathml(tex, displayMode = TRUE, ...) example_math()
katex_html( tex, displayMode = TRUE, ..., include_css = FALSE, preview = interactive() ) katex_mathml(tex, displayMode = TRUE, ...) example_math()
tex |
input string with tex math expression. |
displayMode |
render math in centered 2D layout, similar to |
... |
additional html rendering options passed to katex.render |
include_css |
adds the katex css file to the output.
This is only required once per html webpage. Set to |
preview |
open an HTML preview page showing the snipped in the browser |
Refer to the upstream katex support table for the full list of supported tex functions that can be rendered to html using katex.
By default, katex_html returns a mix of HTML for visual rendering and includes
MathML for accessibility. To only get html, pass output="html"
in the extra options,
see also the katex documentation.
a string with a html/mathml fragment
Other katex:
math_to_rd()
,
pandoc
# Basic examples html <- katex_html(example_math()) mathml <- katex_mathml(example_math()) # Example from katex.org homepage: macros <- list("\\f" = "#1f(#2)") math <- "\\f\\relax{x} = \\int_{-\\infty}^\\infty \\f\\hat\\xi\\,e^{2 \\pi i \\xi x} \\,d\\xi" html <- katex_html(math, macros = macros) mathml <- katex_mathml(math, macros = macros)
# Basic examples html <- katex_html(example_math()) mathml <- katex_mathml(example_math()) # Example from katex.org homepage: macros <- list("\\f" = "#1f(#2)") math <- "\\f\\relax{x} = \\int_{-\\infty}^\\infty \\f\\hat\\xi\\,e^{2 \\pi i \\xi x} \\,d\\xi" html <- katex_html(math, macros = macros) mathml <- katex_mathml(math, macros = macros)
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)
math_to_rd(tex, ascii = tex, displayMode = TRUE, ..., include_css = TRUE)
tex |
input string with tex math expression. |
ascii |
alternate text-only representation of the input math to show in documentation rendered to plain text format. |
displayMode |
render math in centered 2D layout, similar to |
... |
additional html rendering options passed to katex.render |
include_css |
adds the katex css file to the output.
This is only required once per html webpage. Set to |
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:
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²') }
If no ascii representation is given, the input tex in displayed verbatim into the plain-text documentation.
a string with an rd fragment to be included in R documentation
On Windows, R versions before 4.1.2 had a bug
which could lead to incorrect HTML encoding for \Sexpr{}
output.
As a workaround, we automatically escape non-ascii html characters
on these versions of R. Linux and MacOS are unaffected.
Reads an html file and substitutes elements of class "math display"
and
"math inline"
with rendered html math. This is mainly intended as a
post-processing step for pandoc, which generates such html for equations.
As a result the math can be displayed without the need for including the
mathjax library in the html document.
render_math_in_html( input, output = NULL, ..., throwOnError = FALSE, include_css = TRUE )
render_math_in_html( input, output = NULL, ..., throwOnError = FALSE, include_css = TRUE )
input |
path to the html input file |
output |
path to the output html file, or NULL to return as string |
... |
additional html rendering options passed to katex.render |
throwOnError |
should invalid math raise an error in R? See katex options |
include_css |
automatically inject the required katex css in the html head |
Other katex:
katex
,
math_to_rd()