Include latex math equation within gt package table

I would like to include some latex math equations in a gt table. This will then be knit to pdf and html outputs from a Rmarkdown file.

In the reprex below, you can see what I am trying to achieve using the kable package, but would like to use gt for consistency.

Can this be done?

Copy this code into a Rmarkdown file and knit to reproduce:

---
title: "reprex"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(gt)
library(knitr)
```

```{r data}

df <- data.frame(key = 1, 
                 equation = c("$exp(1.6 - 3.327 \\times x^2)$"))
```

```{r gt}
gt(df)
```

```{r kableExtra}
kable(df, escape = FALSE)
```

You need to use double dollar sign (like for a display equation as opposed to an inline one)

---
title: "reprex"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(gt)
library(knitr)

df <- data.frame(key = 1, 
                 equation = c("$$exp(1.6 - 3.327 \\times x^2)$$"))
gt(df)
kable(df, escape = FALSE)
1 Like

Thanks for the solution. Are you aware of how to do this in a shiny app?
The following does not work:

library(shiny)
ui <- fluidPage(gt::gt_output("table"))
server <- function(input, output) {
    output$table <- render_gt({
        data.frame(key = 1, 
                   equation = c("$$exp(1.6 - 3.327 \\times x^2)$$"))
    })
}
shinyApp(ui = ui, server = server)

I can ask a new question if need be...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.