7

I am trying to create a table in rmarkdown with kable and kableExtra and I want to put greek letters in the add_header_above function.

kable(a, format = "latex", booktabs = T, longtable = T) %>% 
kable_styling() %>%
add_header_above(c("n", "$\\alpha$", "Estimates for $\\alpha$" = 4, 
"Estimates for $\\beta$" = 4), align = "l", escape = F)

here

I tried using escape = F like here, but it seems it doesn't work for greek letters.

I want that style of table in my rmarkdown document with the greek letters in the column headers. Is there a way to do that with kable and kableExtra or even with another method?

My data:

a <- structure(list(c("5", "", "", "", ""), c(0.1, 0.25, 0.5, 1, 2
), MLE = c(0.0839, 0.2082, 0.4194, 0.8237, 1.6201), MME = c(0.0839, 
0.2082, 0.4194, 0.8234, 1.6147), UMLE = c(0.1048, 0.2602, 0.5242, 
1.0296, 2.0251), UMME = c(0.1048, 0.2602, 0.5242, 1.0293, 2.0183
), MLE = c(1, 1.0057, 1.0232, 1.0824, 1.2543), MME = c(1, 1.0057, 
1.0232, 1.0824, 1.2551), UMLE = c(0.9994, 1.0019, 1.0077, 1.0233, 
1.0456), UMME = c(0.9994, 1.0019, 1.0077, 1.0233, 1.0476)), .Names = c("", 
"", "MLE", "MME", "UMLE", "UMME", "MLE", "MME", "UMLE", "UMME"
), row.names = c(NA, 5L), class = "data.frame")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
falecomdino
  • 87
  • 1
  • 6

1 Answers1

9

You just need to give some extra escapes. Something in your pipeline ate the \\ characters, so use \\\\ instead:

kable(a, format = "latex", booktabs = TRUE, longtable = TRUE) %>% 
  kable_styling() %>%
  add_header_above(c("n", "$\\\\alpha$", "Estimates for $\\\\alpha$" = 4, 
                     "Estimates for $\\\\beta$" = 4), 
                   align = "l", escape = FALSE)

This gives:

enter image description here

I also changed your T and F to TRUE and FALSE; this is not necessary here, but it is good defensive programming.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • What do you mean by "something in your pipeline ate \\" ? The `kableExtra` package was meant to use the magrittr's pipeline by default. Do you know what happened? – falecomdino Apr 27 '18 at 02:42
  • I meant that you entered the backslashes, but they didn't come out at the end. Presumably it was something in `add_header_above`, but I didn't track down where it happened. – user2554330 Apr 27 '18 at 10:20