如何使用列标题中的希腊字母在rmarkdown中创建表格?

时间:2018-04-26 14:49:53

标签: r r-markdown knitr kable

我正在尝试使用kablekableExtra在rmarkdown中创建一个表格,我想在add_header_above函数中添加希腊字母。

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

我尝试使用像here这样的escape = F,但它似乎对希腊字母不起作用。

我想在我的rmarkdown文档中使用列标题中的希腊字母表格。有没有办法用kablekableExtra或甚至用其他方法来做到这一点?

我的数据:

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 个答案:

答案 0 :(得分:2)

你只需要额外逃脱一些。管道中的某些内容会占用\\个字符,因此请改用\\\\

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)

这给出了:

enter image description here

我还将您的TF更改为TRUEFALSE;这不是必要的 在这里,但这是一个很好的防御性编程。