| Title: | Translate R Expressions to 'MathML' and 'LaTeX'/'MathJax' |
|---|---|
| Description: | Translate R expressions to 'MathML' or 'MathJax'/'LaTeX' so that they can be rendered in R markdown documents and shiny apps. This package depends on R package 'rolog', which requires an installation of the 'SWI'-'Prolog' runtime either from 'swi-prolog.org' or from R package 'rswipl'. |
| Authors: | Matthias Gondan [aut, cre, cph] (University of Innsbruck), Irene Alfarone [aut] (University of Innsbruck), European Commission [fnd] (Erasmus+ Programme, 2019-1-EE01-KA203-051708) |
| Maintainer: | Matthias Gondan <[email protected]> |
| License: | FreeBSD |
| Version: | 1.8 |
| Built: | 2026-05-22 10:47:05 UTC |
| Source: | https://github.com/mgondan/mathml |
Right arrow, presented as x -> y
x %->% yx %->% y
x |
first element |
y |
second element |
NA, it produces a right arrow
Product x * y, shown as x dot y
x %.% yx %.% y
x |
first factor |
y |
second factor |
x * y
Plus Minus, it shows x and calculates x +- y
x %+-% yx %+-% y
x |
first term |
y |
second term |
c(x - y, x + y) x plus min y
Left arrow, presented as x <- y
x %<-% yx %<-% y
x |
first element |
y |
second element |
NA, it produces a left arrow
Double sided arrow, presented as x <-> y
x %<->% yx %<->% y
x |
first element |
y |
second element |
NA, it produces a double sided arrow
Right double arrow, displayed as x => y
x %<=% yx %<=% y
x |
first element |
y |
second element |
NA, it produces a right double arrow
If and only if condition, displayed as x <=> y
x %<=>% yx %<=>% y
x |
first element |
y |
second element |
NA, it produces a double arrow double-sided
Equivalence, shown as x == y
x %==% yx %==% y
x |
first argument |
y |
second argument |
x == y
Left double arrow, displayed as x <= y
x %=>% yx %=>% y
x |
first element |
y |
second element |
NA, it produces a left double arrow
Congruence, shown as x =~ y
x %=~% yx %=~% y
x |
first argument |
y |
second argument |
x == y, e.g., a cong b
Approximate equality, shown as x ~~ y
x %~~% yx %~~% y
x |
first argument |
y |
second argument |
The result of isTRUE(all.equal(x, y))
Down double arrow, displayed as x dArr y
x %dbldown% yx %dbldown% y
x |
first element |
y |
second element |
NA, it produces a downward double arrow
Up double arrow, displayed as x uArr y
x %dblup% yx %dblup% y
x |
first element |
y |
second element |
NA, it produces a upward double arrow
Down arrow, presented as x downarrow y
x %down% yx %down% y
x |
first element |
y |
second element |
NA, it produces a downward arrow
Proportional, shown as x o< y
x %prop% yx %prop% y
x |
first argument |
y |
second argument |
NA
Up arrow, presented as x up y
x %up% yx %up% y
x |
first element |
y |
second element |
NA, it produces an upward arrow
This is a function that allows the user to highlight the mistakes, in particular an extra element in a list
add(expr)add(expr)
expr |
expression |
expr , e.g., highlights a + b from a + b
This is a function that allows the user to highlight the mistakes, in particular the redundancies in the left-hand side of the expression.
add_left(expr)add_left(expr)
expr |
expression |
expr e.g., highlights a + from a + b
This is a function that allows the user to highlight the mistakes, in particular the redundancies in the right-hand side of the expression.
add_right(expr)add_right(expr)
expr |
expression |
expr , e.g., highlights + b from a + b
Calligraphic font
cal(x)cal(x)
x |
an R symbol. This function is used to render the content in calligraphic font in MathJax. In MathML, script font is used. |
The function cal is a wrapper for the identity function.
mathjax(quote(K %in% cal(K)))mathjax(quote(K %in% cal(K)))
Canonicalize an R call: Reorder the function arguments
canonical(term = quote(`%in%`(table = Table, x = X)), drop = TRUE)canonical(term = quote(`%in%`(table = Table, x = X)), drop = TRUE)
term |
an R call. |
drop |
whether to drop the argument names or not |
The R function, with arguments rearranged
canonical(term=quote(`%in%`(table=Table, x=X)))canonical(term=quote(`%in%`(table=Table, x=X)))
Identity functions for different decorations
roof(x) boxed(x) cancel(x) phantom(x) prime(x) tilde(x) over(x) under(x) underover(x) hyph(x) color(x)roof(x) boxed(x) cancel(x) phantom(x) prime(x) tilde(x) over(x) under(x) underover(x) hyph(x) color(x)
x |
the expression to render |
x
roof(1) + mean(2) + boxed(3) + cancel(4) + phantom(5) + prime(6) + tilde(7) mathml(quote(roof(b) + mean(X) + boxed(3) + cancel(4) + phantom(5)))roof(1) + mean(2) + boxed(3) + cancel(4) + phantom(5) + prime(6) + tilde(7) mathml(quote(roof(b) + mean(X) + boxed(3) + cancel(4) + phantom(5)))
denote This is a function that allows the user to insert abbreviations in the formula, explain them and make the needed computations
denote(abbr, expr, info)denote(abbr, expr, info)
abbr |
Abbreviation used in the text to refer to the calculation, for example 's_p' for the pooled variance. |
expr |
Expression: calculations to be made in order to obtain the value to which the abbreviation refers to. |
info |
Information: Explanation of the formula used to provide the value of the abbreviation. e.g. 'the pooled variance' |
expr e.g., x denotes a^2 + b
Division displayed as large fraction
dfrac(e1, e2)dfrac(e1, e2)
e1 |
numerator |
e2 |
denominator |
e1 / e2
Multiplication
dot(e1, e2) nodot(e1, e2) times(e1, e2)dot(e1, e2) nodot(e1, e2) times(e1, e2)
e1 |
numerator |
e2 |
denominator |
e1 * e2
Return function body
fname(fname, body)fname(fname, body)
fname |
not clear |
body |
not clear |
body
Identity functions for different font styles
plain(x) italic(x) bold(x)plain(x) italic(x) bold(x)
x |
the expression to render |
x
plain(1) + bold(2) + italic(3) mathml(term=quote(plain(abc) + bold(def) + italic(ghi)))plain(1) + bold(2) + italic(3) mathml(term=quote(plain(abc) + bold(def) + italic(ghi)))
Division displayed as fraction
frac(e1, e2)frac(e1, e2)
e1 |
numerator |
e2 |
denominator |
e1 / e2
hook(term, display) hook_fn(fn) unhook(term) hooked(term)
hook(term, display = NULL, quote = TRUE, as.rolog = TRUE) unhook(term, quote = TRUE, as.rolog = TRUE) hooked(term) hook_fn(fn)hook(term, display = NULL, quote = TRUE, as.rolog = TRUE) unhook(term, quote = TRUE, as.rolog = TRUE) hooked(term) hook_fn(fn)
term |
an R call or symbol/number. This is the expression to replace. |
display |
an R call or symbol/number. This is shown instead of term. |
quote |
(default is TRUE) indicates that term and display should be quoted. |
as.rolog |
(default is TRUE) indicates that simplified quasi-quotation is to be used. |
fn |
a custom function. The name of fn is replaced by its function body. |
hook and unhook return TRUE on success. hooked returns the hooked expression or FALSE on failure.
hook(t0, subscript(t, 0)) hooked(quote(t0)) mathml(quote(t0)) hook(term=quote(t0), display=quote(superscript(t, 0)), quote=FALSE) mathml(quote(t0)) unhook(t0) mathml(quote(t0)) square <- function(x) {x^2} ; hook_fn(square)hook(t0, subscript(t, 0)) hooked(quote(t0)) mathml(quote(t0)) hook(term=quote(t0), display=quote(superscript(t, 0)), quote=FALSE) mathml(quote(t0)) unhook(t0) mathml(quote(t0)) square <- function(x) {x^2} ; hook_fn(square)
This is a function that allows the user to highlight the mistakes, in particular adds a curly bracket under the wrong term and it provides the correct solutions.
instead(inst, of)instead(inst, of)
inst |
the wrong term |
of |
the correct term |
inst
1 + instead(2, 3) mathml(term=quote(1 + instead(2, 3)))1 + instead(2, 3) mathml(term=quote(1 + instead(2, 3)))
mathout()
Adds the class "math" to the object for knitr output via mathout()
math(term, flags = NULL)math(term, flags = NULL)
term |
an R call or symbol/number. This function translates term into a LaTeX/MathJax string. |
flags |
(default NULL) list of flags that control the translation |
term with additional class "math" and flags as attributes.
mathml(), mathjax(), mathout()
math(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L))math(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L))
Mathjax output
mathjax( term = quote((a + b)^2L == a^2L + 2L * a * b + b^2L), flags = NULL, env = globalenv() )mathjax( term = quote((a + b)^2L == a^2L + 2L * a * b + b^2L), flags = NULL, env = globalenv() )
term |
an R call or symbol/number. This function translates term into a LaTeX/MathJax string. |
flags |
(default NULL) list of flags that control the translation |
env |
(default globalenv()) The R environment in which r_eval is being executed (see vignette for details, "Ringing back to R"). |
In some functions, the Prolog code may ring back R, for example, to
find the names of function arguments. For example (see vignette), when
rendering the call integrate(g, lower=0L, upper=Inf) as Int_0^Inf g(x) dx,
Prolog needs to know that the function g is a function of x. The Prolog rule
then searches for the formalArgs of g in the environment env.
A string with the MathJax representation of term.
mathjax(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L))mathjax(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L))
MathML output
mathml( term = quote((a + b)^2L == a^2L + 2L * a * b + b^2L), flags = NULL, env = globalenv() )mathml( term = quote((a + b)^2L == a^2L + 2L * a * b + b^2L), flags = NULL, env = globalenv() )
term |
an R call or symbol/number. This function translates term into a MathML string. |
flags |
(default NULL) list of flags that control the translation. This includes "context" settings such as error("ignore"), or a default number of decimal places for numeric output. |
env |
(default globalenv()) The R environment in which r_eval is being executed. |
In some functions, the Prolog code may ring back R, for example, to
find the names of function arguments. For example (see vignette), when
rendering the call integrate(g, lower=0L, upper=Inf) as Int_0^Inf g(x) dx,
Prolog needs to know that the function g is a function of x. The Prolog rule
then searches for the formalArgs of g in the environment env.
A string with the MathML representation of term.
mathml(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L)) mathml(term=3.14159265, flags=list(round=3L)) mathml(term=3.14159265, flags=list(quote(round(3L))))mathml(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L)) mathml(term=3.14159265, flags=list(round=3L)) mathml(term=3.14159265, flags=list(quote(round(3L))))
Map R operators to their respective Prolog counterparts
mathml_preproc(query = quote(5%%2))mathml_preproc(query = quote(5%%2))
query |
an R call or symbol/number. This function translates components of query into their respective counterparts from Prolog |
The translated query
mathml_preproc(quote(5 %% 2))mathml_preproc(quote(5 %% 2))
MathML or MathJax output, depending on the knitr context
mathout(term, flags = NULL, env = parent.frame()) inline(term, flags = NULL, env = parent.frame())mathout(term, flags = NULL, env = parent.frame()) inline(term, flags = NULL, env = parent.frame())
term |
an R call or symbol/number. This function translates term into a LaTeX/MathJax string. |
flags |
(default NULL) list of flags that control the translation |
env |
(default parent.frame()) The R environment in which r_eval is being executed (see vignette for details, "Ringing back to R"). |
This function checks knitr::is_html_output() and knitr::is_html_output() and invokes the respective function mathml() or mathjax(). Outside of knitr context, MathML is returned, and a warning is given.
A string with the MathML or MathJax representation of term.
mathout(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L)) inline(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L))mathout(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L)) inline(term=quote((a + b)^2L == a^2L + 2L*a*b + b^2L))
Add a name attribute to an element (most often, an R function)
name(x, name)name(x, name)
x |
an R object, e.g., an R function |
name |
the name of the object/function |
The object with the name attribute
f <- function(x) {sin(x)} mathjax(call("integrate", name(f, "sin"), 0L, 2L*pi))f <- function(x) {sin(x)} mathjax(call("integrate", name(f, "sin"), 0L, 2L*pi))
This is a function that allows the user to highlight the mistakes, in particular the omission of an element from a list.
omit(expr)omit(expr)
expr |
expression |
NULL e.g., remove a + b from a + b
omit_left This is a function that allows the user to highlight the mistakes, in particular the omissions in the left-hand side of the expression
omit_left(expr)omit_left(expr)
expr |
The expression, e.g. a + b |
substitute(expr)[[3]], e.g., b from a + b
omit_right This is a function that allows the user to highlight the mistakes, in particular the omissions in the right-hand side of the expression
omit_right(expr)omit_right(expr)
expr |
expression |
substitute(expr)[[2]], e.g., a from a + b
product over a range. On the R side, this function just returns the product of the first argument, but allows for decorations.
prod_over(x, from, to)prod_over(x, from, to)
x |
the object to be multiplied |
from |
decoration for prod_from^to x_i |
to |
decoration for prod_from^to x_i |
The function returns prod(x)
mathjax(quote(prod_over(x[i], i=1L, N)))mathjax(quote(prod_over(x[i], i=1L, N)))
sum over a range. On the R side, this function just returns the first argument, but allows for decorations.
sum_over(x, from, to)sum_over(x, from, to)
x |
the object to be summed |
from |
decoration for sum_from^to x_i |
to |
decoration for sum_from^to x_i |
The function returns sum(x)
mathjax(quote(sum_over(x[i], i=1L, N)))mathjax(quote(sum_over(x[i], i=1L, N)))