kableExtra addfootnote general spanning multiple lines with PDF (LaTeX) 输出

kableExtra addfootnote general spanning multiple lines with PDF (LaTeX) output

问题

代码

# 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.")

输出

评论

问题 1: 输出在脚注中显示 'makecell[1]',我显然不希望包括在内。添加参数 escape = T 并没有像我预期的那样解决这个问题。

N.B. 通过设置 footnote_as_chunk = F,这个问题得到了解决,但是在字幕开始之前引入了一个换行符。彼得在下面的回答证明了这一点。

问题2脚注不想被限制在table的长度。我想也许可以在脚注字符串中手动添加换行符,但这似乎是一种乏味的解决方法,我希望有一种方法可以更有效地实现这一点。 documentation 显示(参见 Table 4,第 25 页)一个示例,说明如何规避此问题,但缺少代码。

编辑: 这个问题(#2)在调用 kbl.

时通过设置 threeparttable = T 解决了

使用 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.")

footnote_as_chunk = TRUE 使用“常规”脚注选项“注释”。和“脚注....”文本在同一行开始。如本例所示,如下图。