R Markdown table caption width with kable and longtable

邮差的信 提交于 2021-01-01 06:05:13

问题


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()
```

回答1:


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:



来源:https://stackoverflow.com/questions/46085067/r-markdown-table-caption-width-with-kable-and-longtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!