3

How can I have LaTeX symbols like alpha or delta in column names of html output with gt package.

library(gt)

# Create a data frame
data <- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# Set the column names as LaTeX formulas
colnames(data) <- c("$\\alpha$", "$\\beta$")

# Create a gt table
gt(data)

enter image description here

Marco
  • 2,368
  • 6
  • 22
  • 48

2 Answers2

2

If you do not mind using unicode, this works but I appreciate it does not use LaTeX equation mode.

library(gt)

# Create a data frame
data <- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# Set the column names with unicode
colnames(data) <- c("\u03B1", "\u03B2")

# Create a gt table
gt(data)

enter image description here

Peter
  • 11,500
  • 5
  • 21
  • 31
  • This pretty much answers my question. Although I was hoping for a version with full formula support. – Marco Jul 03 '23 at 12:10
  • 1
    @Marco, using rmarkdown in a `.Rdm` file `colnames(data) <- c("$\\alpha$", "$\\beta$")` and `knitr::kable(data)` achieves the required appearance; but I'm not sure that would answer the question. – Peter Jul 03 '23 at 13:25
  • This approach also worked with the `modelsummary` package, but not with `gt`. Is there a way to convert `gt` to `kable`? There is a `as_kable()` function in `gtsummary`. – Marco Jul 03 '23 at 14:09
  • I'm afraid I do not know, It might be worth checking out the gt github site: https://github.com/rstudio/gt which includes related packages. – Peter Jul 03 '23 at 14:28
1

Since I could not find a solution with the gt package, I switched to modelsummary and datasummary_df(). Another great package that produces html output in markdown files. It can take full LaTeX syntax.

library(modelsummary)

# Create a data frame
data <- data.frame(
  x = c(1, 2, 3),
  y = c(4, 5, 6)
)

# Set the column names as LaTeX formulas
colnames(data) <- c("$\\alpha + \\overline{x}$", "$\\beta$")

datasummary_df(data)

enter image description here

Marco
  • 2,368
  • 6
  • 22
  • 48