TeX: Converts LaTeX to a 'plotmath' expression.

View source: R/latex2exp.R

TeXR Documentation

Converts LaTeX to a plotmath expression.


TeX converts a string comprising LaTeX commands (such as a math equation) to a plotmath expression. Plotmath expressions can be used throught R's graphic system to represent formatted text and equations.


  bold = FALSE,
  italic = FALSE,
  user_defined = list(),
  output = c("expression", "character", "ast")



A character vector containing LaTeX strings. Note that any backslashes must be escaped (e.g. "$\alpha").


Whether to make the entire label bold


Whether to make the entire label italic


Described in the "Adding New Commands" section.


The returned object, one of "expression" (default, returns a plotmath expression ready for plotting), "character" (returns the expression as a string), and "ast" (returns the tree used to generate the expression).


Returns a plotmath expression by default. The output parameter can modify the type of the returned value.

If more than one string is specified in the input parameter, returns a list of expressions.

Adding new commands

New LaTeX commands can be defined by supplying the user_defined parameter. The user_defined parameter is a list that contains LaTeX commands as names, and template strings as values. A LaTeX command that matches one of the names is translated into the corresponding string and included in the final plotmath expression. The file symbols.R in the source code of this package contains one such table that can be used as a reference.

The template string can include one of the following special template parameters:

  • $arg1, $arg2, ... represent the first, second, ... brace argument. E.g. for \frac{x}{y}, $arg1 is x and $arg2 is y.

  • $opt is an optional argument in square brackets. E.g. for \sqrt[2]{x}, $opt is 2.

  • $sub and $sup are arguments in the exponent (^) or subscript (_) following the current expression. E.g. for \sum^{x}, $sup is x.

  • $LEFT and $RIGHT are substituted the previous and following LaTeX expression relative to the current token.

See the Examples section for an example of using the user_defined option.


TeX("$\\alpha$") # plots the greek alpha character
TeX("The ratio of 1 and 2 is $\\frac{1}{2}$")

a <- 1:100
plot(a, a^2, xlab=TeX("$\\alpha$"), ylab=TeX("$\\alpha^2$"))

# create a \variance command that takes a single argument
TeX("$\\variance{X} = 10$", user_defined=list("\\variance"="sigma[$arg1]^2"))

latex2exp documentation built on March 18, 2022, 6:36 p.m.