rknitrr-markdownlongtablekableextra

R Markdown table caption width with kable and longtable


Using R Markdown to output a pdf. kable() works great but when I add longtable=T the caption no longer extends the full width of the table. I can't seem to find an argument that will control the caption details here. I can move the caption to be output for each code chunk but would rather use the built in functionality within kable if possible.

Thanks!

---
title: "test"
author: ""
date: "September 6, 2017"
output: 
pdf_document: 
latex_engine: xelatex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
library(knitr)
library(dplyr)
```

```{r table1}
test <- data.frame(col1=rep("MyLongWordsareLong",5),
               col2=rep("MyLongWordsareLong",5),
               col3=rep("MyLongWordsareLong",5),
               col4=rep("MyLongWordsareLong",5),
               col5=rep("MyLongWordsareLong",5),
               col6=rep("MyLongWordsareLong",5))

kable(test,format='latex',booktabs=TRUE,
caption="This is my example caption. See how, when I don't use 
longtable, it extends the full width of the table, but when I use the 
longtable option, it compresses down to only a portion of the table's wdith. 
Is this weird or is it just me?") %>% 
 landscape()

kable(test,longtable=TRUE,format='latex',booktabs=TRUE,caption="This is my 
example caption. See how, when I don't use longtable, it extends the full 
width of the table, but when I use the longtable option, it compresses down 
to only a portion of the table's wdith. Is this weird or is it just me?") 
%>% 
landscape()
```

Solution

  • This is probably a LaTeX problem in the longtable package. This page suggests a workaround: https://tex.stackexchange.com/questions/287283/how-to-define-caption-width-in-longtable . Just put

    header-includes:
       - \usepackage{caption}
    

    in your YAML header, and things will work the way you expected. You can also add the LaTeX code

    \captionsetup{width=5in}
    

    (or use some other measure, such as 5cm, \textwidth, \textheight, etc.) to get consistent caption widths of other sizes.

    Edited to add:

    Here's the original example, modified as suggested (plus a little bit more cleanup):

    ---
    title: "test"
    author: ""
    date: "September 6, 2017"
    output: 
      pdf_document: 
        latex_engine: xelatex
    header-includes:
      - \usepackage{caption}
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    library(kableExtra)
    library(knitr)
    library(dplyr)
    ```
    
    Add `\captionsetup{width=5in}` on its own line here for smaller captions. 
    
    ```{r table1}
    test <- data.frame(col1=rep("MyLongWordsareLong",5),
                   col2=rep("MyLongWordsareLong",5),
                   col3=rep("MyLongWordsareLong",5),
                   col4=rep("MyLongWordsareLong",5),
                   col5=rep("MyLongWordsareLong",5),
                   col6=rep("MyLongWordsareLong",5))
    
    kable(test,format='latex',booktabs=TRUE,
    caption="This is my example caption. See how, when I don't use 
    longtable, it extends the full width of the table, but when I use the 
    longtable option, it compresses down to only a portion of the table's wdith. 
    Is this weird or is it just me?") %>% 
     landscape()
    
    kable(test,longtable=TRUE,format='latex',booktabs=TRUE,caption="This is my 
    example caption. See how, when I don't use longtable, it extends the full 
    width of the table, but when I use the longtable option, it compresses down 
    to only a portion of the table's wdith. Is this weird or is it just me?") %>% 
    landscape()
    ```
    

    Here are the two tables it produces:

    enter image description here

    screenshot