kableExtra addfootnote 一般跨越多行 PDF (LaTeX) 输出 代码输出

问题描述

问题

代码

# 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.21,0.31,NA)
tbl <- data.frame(ID,V1,V2,V2)
colnames(tbl) <- c('ID','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.")

输出

enter image description here

评论

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

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

问题 2 脚注不想受限于表格的长度。我想人们可能能够在脚注字符串中手动添加换行符,但这似乎是乏味的解决方法,我希望有一种方法可以更有效地实现这一点。 documentation 显示(见表 4,第 25 页)一个如何规避此问题的示例,但没有代码

编辑:此问题 (#2) 已通过在调用 threeparttable = T 时设置 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.")

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

enter image description here