4

I have a footnote in a table formatted with kable and kableExtra for an Rmarkdown-generated pdf. I need NYC to have the superscript (NYC^1) to point to the footnote, but can't figure out how to do it. Any suggestions?

df <- data.frame(city=c("NYC","LA","CHI","MIA"),
                 score=sample(1:100, 4, replace=T))

library(kableExtra)
library(kable)

kable(df, 
      digits = 2,
      format = "latex",
      align="c",
      row.names = FALSE,
      booktabs=T) %>%
  kable_styling(bootstrap_options = c("hover"),
                full_width = F,
                font_size = 12,
                position = "left") %>%
  footnote(number = c("2017 data missing"))
Emilio M. Bruna
  • 297
  • 1
  • 3
  • 14
  • https://stackoverflow.com/questions/50185857/indenting-disturbed-when-adding-superscript-to-rowname-of-a-grouped-row-using-ka https://stackoverflow.com/questions/43322881/how-can-i-subscript-names-in-a-table-from-kable – user63230 Jun 26 '20 at 15:38
  • Thanks, I can get it to work if I replace ```NYC``` with ```"NYC^1^"``` when creating the df, but only if I remove the format="latex") from the kable call. Unfortunately, that means I also can't use the kable_styling. I tried using ```df[1,1]<-"NYC\\textsubscript{1}"``` but no luck. – – Emilio M. Bruna Jun 26 '20 at 16:56

1 Answers1

6

I don't have the elegant answer for you. Here's a way to hack it by replacing the strings inside the generated latex string.

library(kableExtra)
library(stringr)

df <- data.frame(city=c("NYCHACKIT","LA","CHI","MIA"),score=sample(1:100, 4, replace=T))

tmp <- knitr::kable(df,  
      digits = 2,
      format = "latex",
      align="c",
      row.names = FALSE,
      booktabs=T) %>%
  kable_styling(bootstrap_options = c("hover"),
                full_width = F,
                font_size = 12,
                position = "left") %>%
  footnote(number = c("2017 data missing"))

knitr::asis_output(str_replace(tmp, "HACKIT", "$^{1}$"))
Ben Toh
  • 742
  • 5
  • 9