Spe*_*ife 4 latex r knitr kableextra kable
# Toy Data
ID <- c(paste("G0", as.character(1:9), sep = ""),"G10","G11","Mean")
V1 <- c(10.06,11.06,12.06,13.06,14.06,15.06,16.06,17.07,18.07,19.07,6.88,13.86)
V2 <- c(0.21,0.03,0.09,0.03,0.09,0.03,0.09,0.03,0.09,0.21,0.31,NA)
tbl <- data.frame(ID, V1, V1, V2, V1, V2, V1, V2, V2)
colnames(tbl) <- c('ID','Get. \\%','Get. \\%','K','Get. \\%','K','Get. \\%','K','P')
# Specify kable NA value and load kableExtra
options(knitr.kable.NA = '--')
require(kableExtra)
# Generate table for PDF output (LaTeX)
kbl(tbl, format = 'latex', align = 'l', booktabs = T, escape = F, digits = 2,
linesep = "", caption = "This is a table caption.") %>%
add_header_above(c(" ", "AB", "BP" = 2, "CK" = 2, "JAM" = 2, ""), bold = T) %>%
column_spec(1, width = '1.15cm') %>%
row_spec(11, hline_after = T) %>%
row_spec(12, bold = T) %>%
kable_styling(position = "center", latex_options = "hold_position") %>%
footnote(general_title = "Note.", footnote_as_chunk = T,
general = "Relatively long footnote that I would like to span
a couple of lines. Relatively long footnote that I
would like to span a couple of lines.")
Run Code Online (Sandbox Code Playgroud)
问题 1:输出在脚注中显示“makecell[1]”,我显然不希望将其包含在内。添加参数escape = T并没有像我预期的那样解决这个问题。
注意:通过设置footnote_as_chunk = F,这个问题得到了解决,但是在标题开始之前引入了换行符,从而产生了不良影响。彼得下面的回答证明了这一点。
问题 2脚注不希望受到表格长度的限制。我想人们也许能够在脚注字符串中手动添加换行符,但这似乎是乏味的解决方法,我希望有一种方法可以更有效地实现这一点。文档显示了(参见表 4,第 25页 )如何规避此问题的示例,但缺少代码。
编辑:threeparttable = T这个问题(#2)已通过调用时的设置解决kbl。
使用 pdflatex 或 xelatex 编译似乎没有任何区别。任何见解将不胜感激。
尝试这个:
library(kableExtra)
library(magrittr)
kbl(tbl,
format = 'latex',
longtable = TRUE,
align = 'l',
booktabs = T,
escape = F,
digits = 2,
linesep = "",
caption = "This is a table caption.") %>%
add_header_above(c(" ", "AB", "BP" = 2, "CK" = 2, "JAM" = 2, ""), bold = T) %>%
column_spec(1, width = '1.15cm') %>%
row_spec(11, hline_after = T) %>%
row_spec(12, bold = T) %>%
kable_styling(position = "center", latex_options = "hold_position", full_width = FALSE) %>%
footnote(general_title = "Note.",
footnote_as_chunk = TRUE,
threeparttable = TRUE,
general = "Relatively long footnote that I would like to span a couple of lines. Relatively long footnote that I would like to span a couple of lines.")
Run Code Online (Sandbox Code Playgroud)
与footnote_as_chunk = TRUE使用“一般”脚注选项“注释”一样。和“脚注....”文本在同一行开始。如本例所示,如下图所示。