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()
```
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 putin your YAML header, and things will work the way you expected. You can also add the LaTeX code
(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):
Here are the two tables it produces: