equation: Get a 'LaTeX' equation from a model or from 'LaTeX' code in a...

View source: R/equation.R

equationR Documentation

Get a 'LaTeX' equation from a model or from 'LaTeX' code in a R Markdown or Quarto document

Description

Extract or create a 'LaTeX' equation to describe a model, or directly from 'LaTeX' code. All objects supported by extract_eq() are also supported by the default method description. If labels (and units) are defined as attributes to the data, they are automatically used in the equation in place of the variable names.

Usage

equation(object, ...)

## Default S3 method:
equation(
  object,
  auto.labs = TRUE,
  origdata = NULL,
  labs = NULL,
  swap_var_names = NULL,
  ...
)

## S3 method for class 'character'
equation(object, ...)

eq__(object, ...)

eq_(object, ...)

## S3 method for class 'inline_equation'
print(x, ...)

Arguments

object

An object with a model whose the equation is constructed. If a character object is provided, it is concatenated into a single character string and the equation class, otherwise it is not transformed (it is supposed to be a valid 'LaTeX' equation). Remember that backslashes must be doubled in regular R strings, or use the r"(...)" notation, which may be more comfortable here, see examples.

...

Further parameters passed to extract_eq().

auto.labs

If TRUE (by default), use labels (and units) automatically from data or ⁠origdata=⁠.

origdata

The original data set this model was fitted to. By default it is NULL and no label is used.

labs

Labels to change the names of elements in the term column of the table. By default it is NULL and no term is changed.

swap_var_names

Change the variable names for these values, regardless of the values in ⁠auto.labs=⁠ or ⁠labs=⁠ that are ignored if this argument is used. Provide a named character string with name being the variables and strings the new names. You can use ^ or ⁠_⁠ to indicate next character, or next integer should be super- or subscript in the equation.

x

An inline_equation object generated by eq_() or eq__().

Details

There are slight differences between equation(), eq_() and eq__():

  • equation() returns a string with 'LaTeX' code and prints 'LaTeX' code at the R console.

  • eq_() returns the 'LaTeX' code surrounded by a dollar sign ⁠$...$⁠ and is suitable to build inline equations in R Markdown/Quarto documents by using inline R code. It prints the rendered inline equation in the RStudio Viewer or in the browser. So it is advised to rapidly preview the resulting equation.

  • eq__() returns the 'LaTeX' code not surrounded by dollar signs in a single character string. It also prints the rendered inline equation in the RStudio Viewer or in the browser. It should be used in an R inline chunk inside a ⁠$$...$$⁠ construct in a Markdown text. The result is a display equation that can also be cross referenced in Quarto in the usual way if you label it, e.g., you use ⁠$$...$$ {#eq-label}⁠.

Value

An object of class c("equation", "character").

Examples

iris_lm <- lm(data = iris, Petal.Length ~ Sepal.Length + Species)
summary(iris_lm)
equation(iris_lm)
# Providing directly the LaTeX code of the equation (variance of a sample)
equation("S^2_y = \\sum_{i=1}^{n-1} \\frac{(y_i - \\bar{Y})^2}{n}")
# Easier to write like this (avoiding the double backslashes):
eq1 <- equation(r"(S^2_y = \sum_{i=1}^{n-1} \frac{(y_i - \bar{Y})^2}{n})")
# Print raw text:
eq1
# Get a preview of the equation
if (interactive())
  eq__(eq1)
# The same function can be used inside a `$$...$$ {#eq-label}` construct in
# R Markdown or Quarto to calcule a display equation that is also recognized
# by the cross referencing system of Quarto.
# Get a string suitable for inclusion inline in R Markdown with `r eq__(eq1)`
# (inside Markdown text and without the dollar signs around it)
# For inline equations in a Markdown text, you are better to use `r eq_(eq1)`
if (interactive())
  eq_(eq1)

equatiomatic documentation built on Aug. 26, 2025, 9:08 a.m.