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")
To make a Greek letter in R, You just use \ and then the name of the letter. If you want a subscript, like β1 , you use $\beta_1$ .
The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames.
Math inside RMarkdown If you want a μ in your document, you can just type “mu” instead and forget about the extra complication.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With