简体   繁体   English

在使用 R markdown 生成的投影仪演示中减少表格和标题之间的距离

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

How to reduce the distance between a table and its caption in a beamer presentation generated with R markdown?如何在使用 R markdown 生成的beamer演示中减少表格与其标题之间的距离? Ideally, the solution also works if the output format is bookdown::pdf_book using base_format: rmarkdown::beamer_presentation (see MWE below).理想情况下,如果 output 格式是bookdown::pdf_book使用base_format: rmarkdown::beamer_presentation (参见下面的 MWE),该解决方案也有效。

图

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 -->

Adding a simple LaTex caption instead of the bookdown approach above in the MWE throws the following error:在 MWE 中添加一个简单的LaTex标题而不是上面的bookdown方法会引发以下错误:

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

: Package caption Error. : Package 字幕错误。 No float type 'tablewLatex' defined.未定义浮点类型“tablewLatex”。

If you want to add a \captionof for a table, you should use table as caption type, not table-wLatex .如果要为表格添加\captionof ,则应使用table作为标题类型,而不是table-wLatex

If the resulting caption is still to high (which is likely because rmarkdown automatically loads the caption package, which is unnecessary with beamer...), you can tweak it a bit with \vspace{-0.2cm} or similar.如果生成的标题仍然很高(这可能是因为 rmarkdown 会自动加载caption package,这对于 beamer 来说是不必要的......),您可以使用\vspace{-0.2cm}或类似方法对其进行一些调整。

---
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 -->

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM