首页 > 解决方案 > kableExtra:如果一个表中包含多个数据帧,则 short.caption 参数不起作用

问题描述

我最近注意到,如果命令中包含多个数据帧,则 kableExtra 包中的 kable() 行为会很奇怪。

不使用 Short.caption 参数

在此示例中,我创建了两个数据框并希望将它们包含在一个表中。

library(kableExtra)
t1 <- c(68, 48, 50, 113, 98, 94)
t2 <- c(26, 16, 22, 30, 16, 12)
Group <- c("b1", "b2", "b3", "b4", "b5", "b6")
cows <- data.frame(Group, t1, t2)
colnames(cows) <- c("Group", "t1", "t2")

kable(list(cows[1:3,], cows[4:6,]), 
     format = "latex",
     caption= "Number of some variables used in some groups",
     caption.short = "Anything shorter.",
     booktabs = T, row.names = F) %>%
  kable_styling(latex_options = c("striped", "hold_position"))

假设\listoftables包含在 Rmarkdown 文件的开头,它将无法识别短标题。如果我查看实际的 Latex 代码,则不包括 [Anything Short.]:

\begin{table}[!h]
\caption{\label{tab:}Number of some variables used in some groups}

\centering
\begin{tabular}[t]{lrr}
\toprule
Group & t1 & \vphantom{1} t2\\
\midrule
\rowcolor{gray!6}  b1 & 68 & 26\\
b2 & 48 & 16\\
\rowcolor{gray!6}  b3 & 50 & 22\\
\bottomrule
\end{tabular}
\centering
\begin{tabular}[t]{lrr}
\toprule
Group & t1 & t2\\
\midrule
\rowcolor{gray!6}  b4 & 113 & 30\\
b5 & 98 & 16\\
\rowcolor{gray!6}  b6 & 94 & 12\\
\bottomrule
\end{tabular}
\end{table}

如果我排除一个表,则会出现 [Anything short.] 参数:

library(kableExtra)
t1 <- c(68, 48, 50, 113, 98, 94)
t2 <- c(26, 16, 22, 30, 16, 12)
Group <- c("b1", "b2", "b3", "b4", "b5", "b6")
cows <- data.frame(Group, t1, t2)
colnames(cows) <- c("Group", "t1", "t2")

kable(cows[4:6,], 
      format = "latex",
      caption= "Number of some variables used in some groups",
      caption.short = "Anything shorter.",
      booktabs = T, row.names = F) %>%
   kable_styling(latex_options = c("striped", "hold_position"))

乳胶代码:

\begin{table}[!h]

\caption[Anything shorter.]{\label{tab:}Number of some variables used in some groups}
\centering
\begin{tabular}[t]{lrr}
\toprule
Group & t1 & t2\\
\midrule
\rowcolor{gray!6}  b4 & 113 & 30\\
b5 & 98 & 16\\
\rowcolor{gray!6}  b6 & 94 & 12\\
\bottomrule
\end{tabular}
\end{table}

我在 row_spec() 和 add_header_above() 功能中遇到了类似的行为。到目前为止,我已经手动插入了相关的 Latex 代码以适应我的工作,但我想知道其他人是否有类似的问题。

我想知道这个问题是源于我这边的错误还是 kableExtra 包中的潜在问题。

标签: rr-markdownkablekableextra

解决方案


推荐阅读