在Rmd pdf输出中包含add_header_above

时间:2018-06-06 15:52:14

标签: latex r-markdown kableextra

问题

如何在上面包装标题(由add_header_above()插入)?

There is a simple way对一个分层标题执行此操作,但在有第二个(或第三个)标题时不起作用。

可重复的示例

library(kableExtra)
names(iris) <- c("L", "W", "L", "W", " ")
iris[1:2, ] %>% 
  kable("latex") %>% 
  add_header_above(
    c(
      "Sepal is great" = 2, 
      "Petal is better, (in fac my favorite)" = 2, 
      "nc" = 1)
    ) %>%
  column_spec(2:ncol(iris), width = "0.3in")

当前输出看起来

enter image description here

R代码的预期输出(大致)

enter image description here

1 个答案:

答案 0 :(得分:1)

正如我在Best Practice for newline in LaTeX table中所说,如果你需要在所有kableExtra函数中使用换行符,只需使用\n。否则,您可以试用linebreak函数。

library(kableExtra)
names(iris) <- c("L", "W", "L", "W", " ")
iris[1:2, ] %>% 
  kable("latex") %>% 
  add_header_above(
    c(
      "Sepal\nis great" = 2, 
      "Petal is better,\n(in fac my favorite)" = 2, 
      "nc" = 1)
    ) %>%
  column_spec(2:ncol(iris), width = "0.3in")