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.
Arguments
- input
A character vector containing LaTeX strings. Note that any backslashes must be escaped (e.g. "$\alpha").
- bold
Whether to make the entire label bold
- italic
Whether to make the entire label italic
- user_defined
Described in the "Adding New Commands" section.
- output
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).
Value
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
isx
and$arg2
isy
.$opt
is an optional argument in square brackets. E.g. for\sqrt[2]{x}
,$opt
is2
.$sub
and$sup
are arguments in the exponent (^
) or subscript (_
) following the current expression. E.g. for\sum^{x}
,$sup
isx
.$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.
Examples
TeX("$\\alpha$") # plots the greek alpha character
#> LaTeX: $\alpha$
#> plotmath: alpha
TeX("The ratio of 1 and 2 is $\\frac{1}{2}$")
#> LaTeX: The ratio of 1 and 2 is $\frac{1}{2}$
#> plotmath: '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"))
#> LaTeX: $\variance{X} = 10$
#> plotmath: sigma[X]^2 * {phantom() == phantom()} * 10