在使用 R markdown 生成的 beamer 演示文稿中减少 table 和标题之间的距离

Reduce distance between table and caption in a beamer presentation generated with R markdown

如何在使用 R markdown 生成的 beamer 演示文稿中减小 table 与其标题之间的距离? 理想情况下,如果输出格式为 bookdown::pdf_book 使用 base_format: rmarkdown::beamer_presentation(请参阅下面的 MWE),该解决方案也适用。

MWE

---
output:
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    latex_engine: xelatex
    toc: false
    slide_level: 2
header-includes:
  - \usepackage{booktabs} 
  - \usepackage{longtable} 
  - \usepackage{array}          
  - \usepackage{multirow}   
  - \usepackage{wrapfig}    
  - \usepackage{float}
  - \usepackage{colortbl}    
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable} 
  - \usepackage{threeparttablex}
  - \usepackage[normalem]{ulem}
  - \usepackage{makecell}
---

## Slide with table

(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b

\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='30%'}
library(kableExtra)
library(dplyr)

# table with manually added footnotes within table
df <- data.frame(
  col1 = c("Category 1", "Category 2"),
  col2 = c(
    "foo and \emph{special foo}$^{a}$", 
    "bar and \n $\boldsymbol{\cdot}$ \emph{random bar}$^{a}$\n $\boldsymbol{\cdot}$ \emph{special bar}$^{b}$")
)

# header: add column names
names(df) <- c("Categories", "Description")

df %>%
  mutate_all(linebreak) %>% # required for linebreaks to work
  kable(
    "latex",
    escape = FALSE, # needed to be able to include latex commands
    booktabs=TRUE,
    align = "l",
    caption = "Caption Table with LaTex" # short caption for LoT
  ) %>%
  kableExtra::footnote(
    alphabet = c(
      "(ref:footnote-a)",
      "(ref:footnote-b)"
      ),
    threeparttable = TRUE, # important! Else footnote runs beyond the table
    footnote_as_chunk = TRUE, title_format = c("italic", "underline")
  ) %>% 
  column_spec(1, width = "3cm") %>% # fix width column 1
  column_spec(2, width = "5cm") # fix width column 2
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->

在 MWE 中添加一个简单的 LaTex 标题而不是上面的 bookdown 方法会引发以下错误:

# ... table as above
\captionof{table-wLatex}{Table caption}
\label{table-wLatex}

!包标题错误:未定义浮点类型 'tablewLatex'。

如果您想为 table 添加 \captionof,您应该使用 table 作为标题类型,而不是 table-wLatex

如果生成的标题仍然太高(这可能是因为 rmarkdown 自动加载 caption 包,这对于 beamer 来说是不必要的...),您可以使用 [=15= 稍微调整一下] 或类似的。

---
output:
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    latex_engine: xelatex
    keep_tex: true
    toc: false
    slide_level: 2
header-includes:
  - \usepackage{booktabs} 
  - \usepackage{longtable} 
  - \usepackage{array}          
  - \usepackage{multirow}   
  - \usepackage{wrapfig}    
  - \usepackage{float}
  - \usepackage{colortbl}    
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable} 
  - \usepackage{threeparttablex}
  - \usepackage[normalem]{ulem}
  - \usepackage{makecell}
---

## Slide with table

(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b

\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
\captionof{table}{Table caption}
\label{table-wLatex}
\vspace{-0.2cm}
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='30%'}
library(kableExtra)
library(dplyr)

# table with manually added footnotes within table
df <- data.frame(
  col1 = c("Category 1", "Category 2"),
  col2 = c(
    "foo and \emph{special foo}$^{a}$", 
    "bar and \n $\boldsymbol{\cdot}$ \emph{random bar}$^{a}$\n $\boldsymbol{\cdot}$ \emph{special bar}$^{b}$")
)

# header: add column names
names(df) <- c("Categories", "Description")

df %>%
  mutate_all(linebreak) %>% # required for linebreaks to work
  kable(
    "latex",
    escape = FALSE, # needed to be able to include latex commands
    booktabs=TRUE,
    align = "l",
  ) %>%
  kableExtra::footnote(
    alphabet = c(
      "(ref:footnote-a)",
      "(ref:footnote-b)"
      ),
    threeparttable = TRUE, # important! Else footnote runs beyond the table
    footnote_as_chunk = TRUE, title_format = c("italic", "underline")
  ) %>% 
  column_spec(1, width = "3cm") %>% # fix width column 1
  column_spec(2, width = "5cm") # fix width column 2
```

\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->