Insert equations

cell-content compose

This example shows how to add ‘MathJax’ equations into a flextable.


Packages

library(flextable)
library(magrittr)
library(data.table)
library(equatiomatic)

The main flextable

mod <- lm(mpg ~ cyl + disp, mtcars)

ft <- as_flextable(mod)
ft

Add an equation

ft <- add_header_lines(ft, "", top = TRUE)

eq <- extract_eq(mod)
print(eq)
## $$
## \operatorname{mpg} = \alpha + \beta_{1}(\operatorname{cyl}) + \beta_{2}(\operatorname{disp}) + \epsilon
## $$

ft <- compose(
  x = ft, j = 1, i = 1, part = "header",
  value = as_paragraph(
    as_equation(eq, 
    width = 2, height = .5)
  )
)

ft <- align(ft, i = 1, part = "header", align = "center")
ft

Dummy example

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
##                                   formula
## 1                     (ax^2 + bx + c = 0)
## 2                                a \\ne 0
## 3 x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}


ft <- flextable(df)
ft <- compose(
  x = ft, j = "formula",
  value = as_paragraph(as_equation(formula, width = 2, height = .5)))
ft <- align(ft, align = "center", part = "all")
ft <- width(ft, width = 2)
ft
Copyright © ArData 2021  -  Template by Bootstrapious.com & ported to Hugo by Kishan B