问题
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)

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")
回答1:
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:
I also changed your T
and F
to TRUE
and FALSE
; this is not necessary
here, but it is good defensive programming.
来源:https://stackoverflow.com/questions/50046055/how-to-create-table-in-rmarkdown-using-greek-letters-in-column-headers