Eqn_helpers | R Documentation |
These functions extend Eqn
to facilitate composing LaTeX equations with desirable features
and pleasant typography.
Eqn_overset
and Eqn_underset
typesets a label over or under a LaTeX expression
or a "latexMatrix"
object
Eqn_overbrace
and Eqn_underbrace
typesets a brace, with an optional label over or under an object
Eqn_newline
, Eqn_hspace
and Eqn_vspace
facilitate spacing of parts of an equation.
Eqn_size
changes size of LaTeX text; Eqn_text
includes a literal string in equations.
Each of these (except Eqn_text
) have aliases without the Eqn_
prefix for brevity.
For example, given the matrix A = matrix(1:4), 2, 2)
, the call Eqn(overset(A, "A"))
generates:
\overset{\mathbf{A}} { \begin{pmatrix} 1 & 3 \ 2 & 4 \ \end{pmatrix} }
When rendered in LaTeX, this produces:
\overset{\mathbf{A}}
{ \begin{pmatrix}
1 & 3 \\
2 & 4 \\
\end{pmatrix}
}
You can also use these for straight LaTeX expressions, such this equation showing and labeling
the Hat matrix in regression. See the examples for the call to underbrace
for this.
\mathbf{\hat{y}} =
\underbrace{\mathbf{X}(\mathbf{X}^{\top}\mathbf{X})^{-1}
\mathbf{X}^{\top}}_{\mathbf{\mathbf{H}}}\mathbf{y}
Eqn_newline()
emits a newline (\
) in an equation, with
an optional increase to the padding following the newline.
Eqn_text()
inserts a literal string to be rendered in a text font in an equation.
Eqn_hspace()
is used to create (symmetric) equation spaces, most typically around
=
signs
Input to lhs
, rhs
can be a
numeric to increase the size of the space or a
character vector to be passed to the LaTeX macro \hspace{}
.
Eqn_vspace()
inserts vertical space between lines in an equation.
Typically used for aligned, multiline equations.
Eqn_size()
is used to increase or decrease the size of LaTeX text and equations. Can be applied
to a specific string or applied to all subsequent text until overwritten.
overset(
x,
label,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
underset(
x,
label,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
overbrace(
x,
label = NULL,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
underbrace(
x,
label = NULL,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
Eqn_overset(
x,
label,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
Eqn_underset(
x,
label,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
Eqn_overbrace(
x,
label = NULL,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
Eqn_underbrace(
x,
label = NULL,
label.style = c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
)
Eqn_newline(space = 0)
newline(space = 0)
Eqn_text(text)
Eqn_hspace(lhs = 5, mid = "", rhs = NULL, times = 1)
hspace(lhs = 5, mid = "", rhs = NULL, times = 1)
Eqn_vspace(space)
vspace(space)
Eqn_size(string, size = 0)
size(string, size = 0)
x |
a numeric or character matrix, or a character string LaTeX expression or
a |
label |
a character string used as the label above or below the object |
label.style |
The name of a math font used to to typeset the label. One of
|
space |
includes extra vertical space. Metric of the vertical space must be 'ex', 'pt', 'mm', 'cm', 'em', 'bp', 'dd', 'pc', or 'in' |
text |
argument to be used within |
lhs |
spacing size. Can be a number between -1 and 6. -1 provides negative
spaces and 0 gives no spacing. Input can also be a character vector, which will be
passed to |
mid |
character vector to place in the middle of the space specification. Most
commonly this will be operators like |
rhs |
see lhs for details. If left as |
times |
number of times to repeat the spacings |
string |
a string that should have its text size modified. If missing
the size modifier is returned, which applies the size modifier
to the remainder of the text until reset with |
size |
numeric size of LaTeX text modifier,
ranging from -3 ( |
Returns a character vector containing the LaTeX expressions for the given operation. You can pass
this to cat
to display the result on the console, or include it inside a call
to Eqn
to typeset it.
Michael Friendly, Phil Chalmers
library(matlib)
A <- matrix(1:4, 2, 2)
B <- matrix(4:1, 2, 2)
AB <- A + B
Eqn(overset(A, "A"))
# missing label: uses the name of the object
Eqn(overset(A))
# test just a character LaTeX expression
Eqn('a', overset('=', '?'), 'b')
# a labelled latexMatrix equation
Eqn(overset(A, "A"), "+",
overset(B, "B"), "=",
underset(AB, "A+B"))
# using a LaTeX expression as the label
Lambda <- latexMatrix("\\lambda", nrow=2, ncol=2,
diag=TRUE)
Eqn(overset(Lambda, "\\Lambda"))
# generate LaTeX expression for the Hat matrix, label as "H"
H <- "\\mathbf{X} (\\mathbf{X}^{\\top}\\mathbf{X})^{-1} \\mathbf{X}^{\\top}"
Eqn("\\mathbf{\\hat{y}} =", underbrace(H, "\\mathbf{H}"), "\\mathbf{y}")
# Combine this with overbrace
Eqn(overbrace(underbrace(H, "\\mathbf{H}"), "\\LARGE\\mathbf{\\hat{y}}"))
Eqn_newline()
Eqn_newline('10ex')
# more complete example
Eqn(underset("\\mathbf{X}", "(4 \\times 3)"), "& = \\mathbf{U} \\mathbf{\\Lambda} \\mathbf{V}^\\top",
Eqn_newline('1ex'),
' & =',
latexMatrix("u", 4, 3),
latexMatrix("\\lambda", 3, 3, diag=TRUE),
latexMatrix("v", 3, 3, transpose = TRUE),
align=TRUE)
Eqn_hspace()
Eqn_hspace(3) # smaller
Eqn_hspace(3, times=2)
Eqn_hspace('1cm')
# symmetric spacing around mid
Eqn_hspace(mid='=')
Eqn_hspace(mid='=', times=2)
Eqn_vspace('1.5ex')
Eqn_vspace('1cm')
# set size globally
Eqn_size(size=3)
Eqn_size() # reset
# locally for defined string
string <- 'e = mc^2'
Eqn_size(string, size=1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.