| Title: | Translate 'LaTeX' Formulas to R Code |
| Version: | 0.3.0 |
| 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/. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.2 |
| Imports: | R6 (≥ 2.4.0), purrr (≥ 0.3.0) |
| Suggests: | testthat |
| URL: | https://github.com/rodrigoesborges/latexr |
| BugReports: | https://github.com/rodrigoesborges/latexr/issues |
| NeedsCompilation: | no |
| Packaged: | 2026-09-21 11:43:39 UTC; rodrigo |
| Author: | Rodrigo Borges [aut, cre], Tomas Capretto [aut, ctb] |
| Maintainer: | Rodrigo Borges <rodrigo@borges.net.br> |
| Repository: | CRAN |
| Date/Publication: | 2026-09-30 09:40:13 UTC |
Extract the AST of a LaTeX formula.
Description
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.
Usage
latex2ast(x)
Arguments
x |
string |
Value
An Expr object, or NULL (invisibly) when the formula can't be scanned.
Examples
latex2ast("\\beta_1^{2}")
Construct R function from LaTeX expression.
Description
Construct R function from LaTeX expression.
Usage
latex2fun(latex_string, envir = parent.frame(), na.rm = FALSE)
Arguments
latex_string |
string |
envir |
environment |
na.rm |
boolean; when |
Value
function
Examples
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")
Translate LaTeX formula to R code.
Description
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.
Usage
latex2r(x, interactive = FALSE, na.rm = FALSE)
Arguments
x |
string |
interactive |
boolean |
na.rm |
boolean; when |
Value
string
Examples
latex2r("\\beta_1^{\\frac{x+1}{x^2 \\cdot y}}")
latex2r("\\bar{x}", na.rm = TRUE)
Normalize Unicode characters to their LaTeX equivalents.
Description
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().
Usage
normalize_mathquill(x)
Arguments
x |
string |
Details
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.
Value
string
Examples
normalize_mathquill("×")
Print a Token object.
Description
Print a Token object.
Usage
## S3 method for class 'Token'
print(x, ...)
Arguments
x |
A |
... |
Further arguments passed to or from other methods. |
Value
The string representation of the token, invisibly.