Learn R Programming

mathml. Translate R expressions to MathML

mathml allows rendering R terms in pretty mathematical equations, bridging the gap between computational needs, presentation of results, and their reproducibility.

Researchers or teachers can already use RMarkdown to conduct analyses and show results, mathml smoothes this process and allows for integrated calculations and output. The package mathml can help in fact to improve data analyses and statistical reports from an aesthetical perspective, as well as regarding reproducibility of research, by allowing also for a better detection of possible mistakes in R programs.

The package supports both MathML and Latex/MathJax for use in RMarkdown documents, presentations and Shiny Apps.

License

This R package is distributed under a BSD-2 simplified license (see the file LICENSE).

Installation

  1. Download and install a recent R from https://www.r-project.org/

  2. Download and install a recent RStudio from https://www.rstudio.com/

  3. R> install.packages("mathml")

The package depends on R package rolog [https://cran.r-project.org/package=rolog], which itself needs the SWI-Prolog runtime on the system. The latter can be installed either from [https://swi-prolog.org] or by installing R package rswipl [https://cran.r-project.org/package=rswipl].

Example

library(mathml)
term <- quote(a^b + c * (d + 3) - a^2L * (a + d))
mathout(term)

$a^b + c \cdot (d+3.00) - a^2 \cdot (a+d)$

term <- call("^", quote(x), 2L)
mathout(term)

$x^2$

For package developers

If you use mathml for your own package, please do not "Import" mathml in your DESCRIPTION, but "Depend" on it.

Package: onmathml
Type: Package
Title: A package that uses mathml
...
Depends: 
    R (>= 4.3),
    mathml (>= 1.3)

It's not entirely clear why this is needed.

Copy Link

Version

Install

install.packages('mathml')

Monthly Downloads

412

Version

1.5

License

FreeBSD

Issues

Pull Requests

Stars

Forks

Maintainer

Matthias Gondan

Last Published

June 13th, 2025

Functions in mathml (1.5)

%=~%

Congruence, shown as x =~ y
add_left

add_left
add

add
%->%

Right arrow, presented as x -> y
%~~%

Approximate equality, shown as x ~~ y
%+-%

Plus Minus, it shows x and calculates x +- y
fontstyles

Identity functions for different font styles
sum_over

sum over a range. On the R side, this function just returns the first argument, but allows for decorations.
%<=>%

If and only if condition, displayed as x <=> y
omit_left

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
mathout

MathML or MathJax output, depending on the knitr context
instead

instead
hook

Hook for custom symbols
omit

omit
%<=%

Right double arrow, displayed as x => y
frac

Division displayed as fraction
name

Add a name attribute to an element (most often, an R function)
%<-%

Left arrow, presented as x <- y
omit_right

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
%<->%

Double sided arrow, presented as x <-> y
%prop%

Proportional, shown as x o< y
prod_over

product over a range. On the R side, this function just returns the product of the first argument, but allows for decorations.
mathml_preproc

Map R operators to their respective Prolog counterparts
mathml

MathML output
%up%

Up arrow, presented as x up y
math

Adds the class "math" to the object for knitr output via mathout()
mathjax

Mathjax output
canonical

Canonicalize an R call: Reorder the function arguments
dot

Multiplication
fname

Return function body
add_right

add_right
cal

Calligraphic font
%=>%

Left double arrow, displayed as x <= y
dfrac

Division displayed as large fraction
%dbldown%

Down double arrow, displayed as x dArr y
%dblup%

Up double arrow, displayed as x uArr y
denote

denote This is a function that allows the user to insert abbreviations in the formula, explain them and make the needed computations
%==%

Equivalence, shown as x == y
%.%

Product x * y, shown as x dot y
decorations

Identity functions for different decorations
%down%

Down arrow, presented as x downarrow y