These functions extend Eqn
to facilitate composing LaTeX equations with desirable features
and pleasant typography.
Eqn_overset
andEqn_underset
typesets a label over or under a LaTeX expression or a"latexMatrix"
objectEqn_overbrace
andEqn_underbrace
typesets a brace, with an optional label over or under an objectEqn_newline
,Eqn_hspace
andEqn_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.
Usage
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)
Arguments
- x
a numeric or character matrix, or a character string LaTeX expression or a
"latexMatrix"
object- label
a character string used as the label above or below the object
x
. If missing, and a"matrix"
object was passed, it's name is used as the label. In LaTeX, these are rendered in a size appropriate for superscripts and subscripts, but you can use a size modifier to change this, for example'\Large{"A"}'
.- label.style
The name of a math font used to to typeset the label. One of
c("mathbf", "mathrm", "mathit", "mathsf", "mathcal", "mathtt", " ")
. The default,"mathbf"
wraps the label inside"\mathbf{ }"
commonly used for the name of a matrix.- 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
\text{}
- 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
\hspace{}
(e.g.,'1cm'
; seespace
argument for supported metrics). Default is 5, resulting in a\quad
space.- 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
NULL
andmid
is specified the this will be set torhs
to create symmetric spaces aroundmid
- 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
Eqn_size()
- size
numeric size of LaTeX text modifier, ranging from -3 (
\tiny
) to 5 (\HUGE
), with 0 defining the normal test size (\normalsize
; default)
Value
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.
Examples
library(matlib)
A <- matrix(1:4, 2, 2)
B <- matrix(4:1, 2, 2)
AB <- A + B
Eqn(overset(A, "A"))
#>
#> \begin{equation*}
#> \overset{\mathbf{A}}
#> { \begin{pmatrix}
#> 1 & 3 \\
#> 2 & 4 \\
#> \end{pmatrix}
#> }
#> \end{equation*}
# missing label: uses the name of the object
Eqn(overset(A))
#>
#> \begin{equation*}
#> \overset{\mathbf{A}}
#> { \begin{pmatrix}
#> 1 & 3 \\
#> 2 & 4 \\
#> \end{pmatrix}
#> }
#> \end{equation*}
# test just a character LaTeX expression
Eqn('a', overset('=', '?'), 'b')
#>
#> \begin{equation*}
#> a\overset{\mathbf{?}}
#> { = }
#> b\end{equation*}
# a labelled latexMatrix equation
Eqn(overset(A, "A"), "+",
overset(B, "B"), "=",
underset(AB, "A+B"))
#>
#> \begin{equation*}
#> \overset{\mathbf{A}}
#> { \begin{pmatrix}
#> 1 & 3 \\
#> 2 & 4 \\
#> \end{pmatrix}
#> }
#> +\overset{\mathbf{B}}
#> { \begin{pmatrix}
#> 4 & 2 \\
#> 3 & 1 \\
#> \end{pmatrix}
#> }
#> =\underset{\mathbf{A+B}}
#> { \begin{pmatrix}
#> 5 & 5 \\
#> 5 & 5 \\
#> \end{pmatrix}
#> }
#> \end{equation*}
# using a LaTeX expression as the label
Lambda <- latexMatrix("\\lambda", nrow=2, ncol=2,
diag=TRUE)
Eqn(overset(Lambda, "\\Lambda"))
#>
#> \begin{equation*}
#> \overset{\mathbf{\Lambda}}
#> { \begin{pmatrix}
#> \lambda_{1} & 0 \\
#> 0 & \lambda_{2} \\
#> \end{pmatrix}
#> }
#> \end{equation*}
# 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}")
#>
#> \begin{equation*}
#> \mathbf{\hat{y}} =\underbrace{\mathbf{X} (\mathbf{X}^{\top}\mathbf{X})^{-1} \mathbf{X}^{\top}}_{\mathbf{\mathbf{H}}}\mathbf{y}\end{equation*}
# Combine this with overbrace
Eqn(overbrace(underbrace(H, "\\mathbf{H}"), "\\LARGE\\mathbf{\\hat{y}}"))
#>
#> \begin{equation*}
#> \overbrace{\underbrace{\mathbf{X} (\mathbf{X}^{\top}\mathbf{X})^{-1} \mathbf{X}^{\top}}_{\mathbf{\mathbf{H}}}}^{\mathbf{\LARGE\mathbf{\hat{y}}}}\end{equation*}
Eqn_newline()
#> [1] " \\\\ \n"
Eqn_newline('10ex')
#> [1] " \\\\[10ex] \n"
# 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)
#>
#> \begin{align*}
#> \underset{\mathbf{(4 \times 3)}}
#> { \mathbf{X} }
#> & = \mathbf{U} \mathbf{\Lambda} \mathbf{V}^\top \\[1ex]
#> & =\begin{pmatrix}
#> u_{11} & u_{12} & u_{13} \\
#> u_{21} & u_{22} & u_{23} \\
#> u_{31} & u_{32} & u_{33} \\
#> u_{41} & u_{42} & u_{43} \\
#> \end{pmatrix}
#> \begin{pmatrix}
#> \lambda_{1} & 0 & 0 \\
#> 0 & \lambda_{2} & 0 \\
#> 0 & 0 & \lambda_{3} \\
#> \end{pmatrix}
#> \begin{pmatrix}
#> v_{11} & v_{12} & v_{13} \\
#> v_{21} & v_{22} & v_{23} \\
#> v_{31} & v_{32} & v_{33} \\
#> \end{pmatrix}^\top
#> \end{align*}
Eqn_hspace()
#> [1] "\\quad"
Eqn_hspace(3) # smaller
#> [1] "\\;"
Eqn_hspace(3, times=2)
#> [1] "\\;\\;"
Eqn_hspace('1cm')
#> [1] "\\hspace{1cm}"
# symmetric spacing around mid
Eqn_hspace(mid='=')
#> [1] "\\quad=\\quad"
Eqn_hspace(mid='=', times=2)
#> [1] "\\quad\\quad=\\quad\\quad"
Eqn_vspace('1.5ex')
#> [1] " \\vspace{1.5ex} \n"
Eqn_vspace('1cm')
#> [1] " \\vspace{1cm} \n"
# set size globally
Eqn_size(size=3)
#> [1] "\\LARGE"
Eqn_size() # reset
#> [1] "\\normalsize"
# locally for defined string
string <- 'e = mc^2'
Eqn_size(string, size=1)
#> [1] "{\\large e = mc^2}"