3
votes

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"))
1
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

1 Answers

3
votes

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}$"))