如何在 R Markdown to Beamer 中使用 kable 删除标题和表格之间不必要的空白

问题描述

我希望我的表格标题 (blabla) 靠近表格顶部(没有多余的空间)。此问题可能与 Sara 中的问题相同,但我提供了一个可重现的示例。我试过 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}

产生:

enter image description here

我怎样才能删除多余的空白(在任何一种情况下)? 最好是适当的解决方案,即无需手动更改 kable 中的 LaTeX 代码...

解决方法

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

另一半是投影仪在字幕下方使用的默认间距。您可以通过 \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}