| Title: | Translate 'LaTeX' Formulas to R Code |
|---|---|
| Description: | Implements a minimal 'LaTeX' parser that translates mathematical formulas into R code strings. Supports arithmetic operators, implicit multiplication, fractions, Greek letters, common mathematical functions, and statistical notation for means, medians, and rolling sums. Input from visual formula editors such as 'MathQuill' is normalized automatically, and the resulting string can be evaluated with parse() and eval(), or converted into an R function with latex2fun(). The implementation follows the tree-walking interpreter design of Nystrom (2021) <https://craftinginterpreters.com/>. |
| Authors: | Rodrigo Borges [aut, cre], Tomas Capretto [aut, ctb] |
| Maintainer: | Rodrigo Borges <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-10-01 14:15:54 UTC |
| Source: | https://github.com/rodrigoesborges/latexr |
Runs the same scanning and parsing pipeline used by latex2r(), but
returns the parsed abstract syntax tree instead of the R translation.
Useful for debugging formulas and for tooling that wants to inspect the
structure recognized by the parser.
latex2ast(x)latex2ast(x)
x |
string |
An Expr object, or NULL (invisibly) when the formula can't be scanned.
latex2ast("\\beta_1^{2}")latex2ast("\\beta_1^{2}")
Construct R function from LaTeX expression.
latex2fun(latex_string, envir = parent.frame(), na.rm = FALSE)latex2fun(latex_string, envir = parent.frame(), na.rm = FALSE)
latex_string |
string |
envir |
environment |
na.rm |
boolean; when |
function
x = seq(-2 * pi, 2 * pi, length.out = 500) f = latex2fun("\\sin{x * a}") f_x = f(x = x, a = 2) plot(x, f_x, type = "l")x = seq(-2 * pi, 2 * pi, length.out = 500) f = latex2fun("\\sin{x * a}") f_x = f(x = x, a = 2) plot(x, f_x, type = "l")
Take a LaTeX formula and attempts to find an equivalent representation in R.
If interactive=TRUE it pops up an interactive REPL that does the same, but interactively.
latex2r(x, interactive = FALSE, na.rm = FALSE)latex2r(x, interactive = FALSE, na.rm = FALSE)
x |
string |
interactive |
boolean |
na.rm |
boolean; when |
string
latex2r("\\beta_1^{\\frac{x+1}{x^2 \\cdot y}}") latex2r("\\bar{x}", na.rm = TRUE)latex2r("\\beta_1^{\\frac{x+1}{x^2 \\cdot y}}") latex2r("\\bar{x}", na.rm = TRUE)
Converts the Unicode characters commonly emitted by visual formula editors
(MathQuill, embedded in many learning platforms) or by plain keyboard input
into the LaTeX commands understood by latex2r().
normalize_mathquill(x)normalize_mathquill(x)
x |
string |
Minus signs and dashes become -, the multiplication and division symbols
become \times, \cdot and /, Greek letters become the corresponding
LaTeX commands, and accented vowels become \bar{}, \tilde{} or
\hat{} (or their base letter when the accent has no meaning here).
This function is applied automatically by latex2r() before scanning, but
it is exported so callers can inspect the normalized version of a formula.
string
normalize_mathquill("×")normalize_mathquill("×")
Print a Token object.
## S3 method for class 'Token' print(x, ...)## S3 method for class 'Token' print(x, ...)
x |
A |
... |
Further arguments passed to or from other methods. |
The string representation of the token, invisibly.