Skip to contents

This function is used to insert equations into flextable.

It is used to add it to the content of a cell of the flextable with the functions compose(), append_chunks() or prepend_chunks().

To use this function, package 'equatags' is required.

Usage

as_equation(x, width = NULL, height = NULL, unit = "in", props = NULL)

Arguments

x

values containing the 'MathJax' equations

width, height

size of the resulting equation. When they are not provided (NULL, the default), the size is computed from the equation and the font size of the chunk with equatags::equation_size() (equatags >= 0.4.0), so that autofit(), dim_pretty() and gen_grob() know the size of the equation; with an older version of 'equatags', 1 x 0.2 inch.

unit

unit for width and height, one of "in", "cm", "mm".

props

an fp_text_default() or officer::fp_text() object to be used to format the text. If not specified, it will be the default value corresponding to the cell.

Size and drawing of equations

The style of equations follows the rule of Word, in every output: an equation is in display style (large fractions, limits above and below sums and products) when its paragraph only contains equations, and in inline style as soon as the paragraph contains something else, even a space. HTML and LaTeX outputs use \displaystyle for the display style, and the size and gen_grob() use the same style.

The size computed by equatags::equation_size() is the one of the equation laid out with the math font 'STIX Two Math'. Word and browsers display equations with their own math font, so the displayed size may differ slightly. With gen_grob() (R >= 4.3), equations are drawn with equatags::equation_grob(), at their computed size.

In RTF documents, equations are images placed on the baseline of the text (R >= 4.3). 'Word' does not leave room for the part of these images below the baseline: in a cell with several lines, an equation larger than the text, or with a fraction, can touch the next line. A larger line spacing avoids it (line_spacing(), for example space = 1.3).

Examples

library(flextable)
if (require("equatags")) {
  eqs <- c(
    "(ax^2 + bx + c = 0)",
    "a \\ne 0",
    "x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}"
  )
  df <- data.frame(formula = eqs)
  df


  ft <- flextable(df)
  ft <- mk_par(
    x = ft, j = "formula",
    value = as_paragraph(as_equation(formula))
  )
  ft <- align(ft, align = "center", part = "all")
  ft <- autofit(ft)
  ft
}
#> Loading required package: equatags

formula

(ax2+bx+c=0)\displaystyle (ax^2 + bx + c = 0)

a≠0\displaystyle a \ne 0

x=−b±b2−4ac2a\displaystyle x = {-b \pm \sqrt{b^2-4ac} \over 2a}