Eqn | R Documentation |
The Eqn
function is designed to produce LaTeX expressions of mathematical
equations for writing.
The output can be copied/pasted into documents or
used directly in chunks in .Rmd
, .Rnw
, or .qmd
documents to compile to equations.
It wraps the equations generated by its arguments
in either a \begin{equation} ...\end{equation}
or
\begin{align} ...\end{align}
LaTeX environment. See also
ref
for consistent inline referencing of numbered equations.
In a code chunk, use the chunk options results='asis', echo=FALSE
to show only
the result of compiling the LaTeX expressions.
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.
ref{}
provides inline references to equations in R
markdown and Quarto documents.
Depending on the output type this function will provide the correct
inline wrapper for MathJax or LaTeX equations. This provides more
consistent referencing when switching between HTML and PDF outputs as
well as documentation types (e.g., .Rmd
vs .qmd
).
Eqn(
...,
label = NULL,
align = FALSE,
preview = getOption("previewEqn"),
html_output = knitr::is_html_output(),
quarto = getOption("quartoEqn"),
mat_args = list(),
preview.pdf = FALSE,
preview.packages = NULL
)
Eqn_newline(space = 0)
Eqn_text(text)
Eqn_hspace(lhs = 5, mid = "", rhs = NULL, times = 1)
Eqn_vspace(space)
Eqn_size(string, size = 0)
ref(
label,
parentheses = TRUE,
html_output = knitr::is_html_output(),
quarto = getOption("quartoEqn")
)
... |
comma separated LaTeX expressions that are either a) a Note that user defined functions that use |
label |
character vector specifying the label to use (e.g., For compiled documents if an HTML output is detected (see |
align |
logical; use the |
preview |
logical; render an HTML version of the equation and display? This is intended for
testing purposes and is only applicable to interactive R sessions, though
for code testing purposes can be set globally
via |
html_output |
logical; use labels for HTML outputs instead of the LaTeX? Automatically
changed for compiled documents that support |
quarto |
logical; use Quarto referencing syntax? When |
mat_args |
list of arguments to be passed to |
preview.pdf |
logical; build a PDF of the preview equation? Generally not require unless additional LaTeX packages are required that are not supported by MathJax |
preview.packages |
character vector for adding additional LaTeX package information to the
equation preview. Only used when |
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 ( |
parentheses |
logical; include parentheses around the referenced equation? |
Phil Chalmers
latexMatrix
, matrix2latex
, ref
# character input
Eqn('e=mc^2')
# show only the LaTeX code
Eqn('e=mc^2', preview=FALSE)
# Equation numbers & labels
Eqn('e=mc^2', label = 'eq:einstein')
Eqn("X=U \\lambda V", label='eq:svd')
# html_output and quarto outputs only show code
# (both auto detected in compiled documents)
Eqn('e=mc^2', label = 'eq:einstein', html_output = TRUE)
# Quarto output
Eqn('e=mc^2', label = 'eq-einstein', quarto = TRUE)
## Not run:
# The following requires LaTeX compilers to be pre-installed
# View PDF instead of HTML
Eqn('e=mc^2', preview.pdf=TRUE)
# Add extra LaTeX dependencies for PDF build
Eqn('\\bm{e}=mc^2', preview.pdf=TRUE,
preview.packages=c('amsmath', 'bm'))
## End(Not run)
# Multiple expressions
Eqn("e=mc^2",
Eqn_newline(),
"X=U \\lambda V", label='eq:svd')
# expressions that use cat() within their calls
Eqn('SVD = ',
latexMatrix("u", "n", "k"),
latexMatrix("\\lambda", "k", "k", diag=TRUE),
latexMatrix("v", "k", "p", transpose = TRUE),
label='eq:svd')
# align equations using & operator
Eqn("X &= U \\lambda V", Eqn_newline(),
"& = ", latexMatrix("u", "n", "k"),
latexMatrix("\\lambda", "k", "k", diag=TRUE),
latexMatrix("v", "k", "p", transpose = TRUE),
align=TRUE)
# numeric/character matrix example
A <- matrix(c(2, 1, -1,
-3, -1, 2,
-2, 1, 2), 3, 3, byrow=TRUE)
b <- matrix(c(8, -11, -3))
# numeric matrix wrapped internally
cbind(A,b) |> Eqn()
cbind(A,b) |> latexMatrix() |> Eqn()
# change numeric matrix brackets globally
cbind(A,b) |> Eqn(mat_args=list(matrix='bmatrix'))
# greater flexibility when using latexMatrix()
cbind(A, b) |> latexMatrix() |> partition(columns=3) |> Eqn()
# with showEqn()
showEqn(A, b, latex=TRUE) |> Eqn()
Eqn_newline()
Eqn_newline('10ex')
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)
# used inside of Eqn() or manually defined labels in the document
Eqn('e = mc^2', label='eq:einstein')
# use within inline block via `r ref()`
ref('eq:einstein')
ref('eq:einstein', parentheses=FALSE)
ref('eq:einstein', html_output=TRUE)
# With Quarto
Eqn('e = mc^2', label='eq-einstein', quarto=TRUE)
ref('eq:einstein', quarto=TRUE)
ref('eq:einstein', quarto=TRUE, parentheses=FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.