kable/kableExtra Add superscript to group labels in group_rows

折月煮酒 提交于 2020-03-03 11:32:02

问题


I have a table that I am creating for a pdf presentation using kable and kableExtra. I am wanting to group the rows and I need to use superscripts in the row group labels. I have tried several different things. Here is an example of some of the methods I have tried so far.

library(kable)
library(kableExtra)

foo <- data.frame(a = 1:10, b = 11:20, c = 21:30)

kable(foo, format = "latex", booktabs = T, row.names = FALSE, linesep = "", escape = FALSE) %>%
  kable_styling(latex_options = c("striped")) %>%
  group_rows("Group1<sup>a</sup>", 1, 2) %>% 
  group_rows(paste0("Group2", footnote_marker_alphabet(1), sep = ""), 3, 4) %>% 
  group_rows(expression("Group3"^a), 5, 6) %>% 
  group_rows("Group4\\textsuperscript{a}", 7, 8)

I have run out of ideas and haven't been able to find any additional suggest in my search.


回答1:


You need escape=FALSE in your group_rows() calls to allow the latex commands to be interpreted. You also seem to need to double each backslash (I don't quite understand why). After that, there are a few different options that work:

kable(foo, format = "latex", booktabs = T, row.names = FALSE, linesep = "", escape = FALSE) %>%
  kable_styling(latex_options = c("striped")) %>%
  group_rows("$\\\\text{Group1}^a$", 1, 2, escape = FALSE) %>% 
  group_rows(paste0("Group2\\\\", footnote_marker_alphabet(1), sep = ""), 3, 4, escape = FALSE) %>% 
  # I don't think expression() is helpful, doesn't seem to get converted
  # to latex
  group_rows(expression("Group3"^a), 5, 6) %>% 
  group_rows("Group4\\\\textsuperscript{a}", 7, 8, escape = FALSE)


来源:https://stackoverflow.com/questions/52070416/kable-kableextra-add-superscript-to-group-labels-in-group-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!