4

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()
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Jordan
  • 614
  • 1
  • 7
  • 20

1 Answers1

6

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

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Brilliant! I had read the post you reference but hadn't realized that 'longtable' in LaTeX behaved similarly to the RMarkdown argument for longtable within kable(). I'm still trying to understand the connection between these two worlds. Thank you! – Jordan Sep 07 '17 at 17:02
  • This is not working for me and it's driving me crazy. I've followed all the links and tried adding all the suggestions to the YAML, but when I use longtable my table caption is squished in the middle of the page. Any suggestions? – Michelle Dec 14 '20 at 14:57
  • @elle11e: Are you getting that problem with the sample document in this question, or only with your own? I just tried the solution on the sample document again, and it still works for me. If it's only your own document that has trouble, I'd post a new question containing a minimal version of your document that illustrates the problem, with a link to this question so people know you tried this answer and it didn't work. – user2554330 Dec 14 '20 at 17:32
  • @user2554330 I don't understand what the "solution" is on the sample document? When I run the example code, I get the same result as I get with my own document. When I try the suggested solution, it won't knit to PDF. So I'm asking for some VERY CLEAR instructions on what the suggestion is telling me to do because as I did it, it doesn't work for me. – Michelle Dec 15 '20 at 13:32
  • Much appreciated! It now seems to be that the caption package is not installed, but it seems like this will do the trick once I figured that piece out. Thank you! – Michelle Dec 15 '20 at 18:31