问题描述
如何在使用 R Markdown 生成的 beamer
演示文稿中减少表格与其标题之间的距离?
理想情况下,如果使用 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}
解决方法
如果您想为表格添加 \captionof
,您应该使用 table
作为标题类型,而不是 table-wLatex
。
如果生成的标题仍然很高(这可能是因为 rmarkdown 自动加载了 caption
包,这对于 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 -->