Columns names with Katex

Packages

library(flextable)
flextable::set_flextable_defaults(digits = 2)

Package equatags

You will need package equatags.

A solution with equatags

ft <- data.frame(
  `\\beta_0` = rep(-1, 4),
  `\\beta_1` = rep(1, 4),
  `X_i` = c(2, 1, 0, -1),
  `p` = runif(4), check.names = FALSE) |> 
  flextable() |> 
  colformat_double(j = 4, digits = 3) |> 
  mk_par(i = 1, part = "header",
         value = as_paragraph(
           as_equation(.,width = .1, height = .2)),
         use_dot = TRUE) |> 
  autofit()
ft

A solution without equatags

If you can’t use “equatags” because of the “node.js” dependency…

This solution is only viable when hard-coding the values and is not suitable for automation needs.

data.frame(
  a = rep(-1, 4),
  b = rep(1, 4),
  c = c(2, 1, 0, -1),
  d = runif(4), check.names = FALSE) |> 
  flextable() |> 
  colformat_double(j = 4, digits = 3) |> 
  mk_par(j = 1, part = "header", value = as_paragraph(as_i("β"), as_sub("0"))) |> 
  mk_par(j = 2, part = "header", value = as_paragraph(as_i("β"), as_sub("1"))) |> 
  mk_par(j = 3, part = "header", value = as_paragraph(as_i("X"), as_sub("i"))) |> 
  mk_par(j = 4, part = "header", value = as_paragraph(as_i("p"))) |> 
  autofit()