Title: | Use LaTeX Expressions in Plots |
---|---|
Description: | Parses and converts LaTeX math formulas to R's plotmath expressions, used to enter mathematical formulas and symbols to be rendered as text, axis labels, etc. throughout R's plotting system. |
Authors: | Stefano Meschiari [aut, cre] |
Maintainer: | Stefano Meschiari <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.9.8 |
Built: | 2024-11-14 04:59:38 UTC |
Source: | https://github.com/stefano-meschiari/latex2exp |
TeX
instead.Deprecated; use TeX
instead.
latex2exp(string, output = c("expression", "character", "ast"))
latex2exp(string, output = c("expression", "character", "ast"))
string |
A character vector containing LaTeX expressions. Note that any backslashes must be escaped (e.g. "$\alpha"). |
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). |
Returns an expression (see the output
parameter).
Plots a number of example LaTeX string, as parsed
by TeX
.
latex2exp_examples(cex = 1)
latex2exp_examples(cex = 1)
cex |
Multiplier for font size |
If show
is TRUE, also show a searchable table of symbols.
latex2exp_supported(show = FALSE, ...)
latex2exp_supported(show = FALSE, ...)
show |
Show a searchable table of symbols |
... |
Other parameters (not used) |
A data frame containing a table of supported LaTeX commands.
Plots the result of a call to [TeX] on the current graphical device. This is useful to preview the output before placing it on a plot.
## S3 method for class 'expression' plot(x, ..., main = NULL)
## S3 method for class 'expression' plot(x, ..., main = NULL)
x |
|
... |
Parameters to be passed to the |
main |
Title of the plot |
plot(TeX("Example equation: $a \\geq b$"))
plot(TeX("Example equation: $a \\geq b$"))
Prints out the plotmath expression generated by TeX
and the original TeX string.
## S3 method for class 'latexexpression' print(x, ...)
## S3 method for class 'latexexpression' print(x, ...)
x |
Object to print |
... |
Ignored |
Prints out a parsed LaTeX object, as returned by TeX(..., output = 'ast'). This is primarily used for debugging.
## S3 method for class 'latextoken2' print(x, depth = 0, ...)
## S3 method for class 'latextoken2' print(x, depth = 0, ...)
x |
The object |
depth |
Increases padding when recursing down the parsed structure |
... |
(Ignored) |
Returns a string that is a valid plotmath expression, given a LaTeX tree
returned by parse_latex
.
render_latex(tokens, user_defined = list(), hack_parentheses = FALSE)
render_latex(tokens, user_defined = list(), hack_parentheses = FALSE)
tokens |
tree of tokens |
user_defined |
any custom definitions of commands passed to
|
hack_parentheses |
render parentheses using
|
String that should be parseable as a valid plotmath expression
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.
TeX( input, bold = FALSE, italic = FALSE, user_defined = list(), output = c("expression", "character", "ast") )
TeX( input, bold = FALSE, italic = FALSE, user_defined = list(), output = c("expression", "character", "ast") )
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). |
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.
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"))
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"))