如何在 R Markdown to Beamer 中使用 kable 删除标题和 table 之间不必要的白色 space

How to remove unnecessary white space between title and table using kable in R Markdown to Beamer

我希望 table (blabla) 的标题靠近 table 的顶部(没有多余的 space)。这个问题可能与 Sara, but I provide a reproducible example. I have tried these tips 中的问题相同,但它们没有解决。

---
title: "XYZ"
output: 
  beamer_presentation
---
```{r, include=FALSE}
library(knitr)
```
## 
```{r}
kable(mtcars[1:2,1:2],
      caption = "blabla",
      format = "latex")
```

\begin{table}

\caption{blabla}
\centering
\begin{tabular}{l|r|r}
\hline
  & mpg & cyl\
\hline
Mazda RX4 & 21 & 6\
\hline
Mazda RX4 Wag & 21 & 6\
\hline
\end{tabular}
\end{table}

生产:

如何去除多余的白色 space(无论哪种情况)? 最好有一个合适的解决方案,即无需手动更改 kable 的 LaTeX 代码...

一半的白色space是rmarkdown使用\begin{tabular}[t]{l|r|r}而不是\begin{tabular}{l|r|r}造成的

另一半是 beamer 在字幕下方使用的默认间距。您可以通过 \setlength\belowcaptionskip{7pt} 控制它,但我建议将更改限制在 table 环境中,以免影响标题应该位于图形下方的地方。

\documentclass{beamer}

\AtBeginEnvironment{table}{\setlength\belowcaptionskip{0pt}}

\begin{document}
    
\begin{frame}
\begin{table}
\caption{content...}
\begin{tabular}{cc}
\hline
c & d\
\hline
\end{tabular}
\end{table}
\end{frame} 

\begin{frame}
\begin{figure}
\includegraphics[width=.5\textwidth]{example-image-duck}
\caption{content...}
\end{figure}
text
\end{frame} 
    
\end{document}