Search code examples
rlatexr-markdownmarkdownbeamer

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? Ideally, the solution also works if the output format is bookdown::pdf_book using base_format: rmarkdown::beamer_presentation (see MWE below).

Fig

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 LaTexcaption instead of the bookdown approach above in the MWE throws the following error:

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

! Package caption Error: No float type 'tablewLatex' defined.


Solution

  • If you want to add a \captionof for a table, you should use table as caption type, not 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.

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

    enter image description here