问题
I have recently noticed that kable() from the kableExtra-package behaves oddly if more than one dataframe is included in the command.
Short.caption argument is not used
In this example, I create two data-frames and want to include them into one table.
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"))
Assuming \listoftables
is included at the beginning of the Rmarkdown-file, it will not recognize the short caption. If I look at the actual Latex-code, the [Anything shorter.] is not included:
\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}
If I exclude one table, the [Anything shorter.] argument appears:
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"))
The Latex code:
\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}
I have experienced similar behaviour in the row_spec() and add_header_above() features. Up until now, I have manually inserted the relevant Latex-code to suit my work, but I was wondering whether others have had similar issues.
I am wondering whether this issue stems from an error on my side or is a potential issue in the kableExtra package.
来源:https://stackoverflow.com/questions/64281612/kableextra-short-caption-argument-does-not-work-if-several-dataframes-are-inclu