R Markdown 表格标题宽度与 kable 和 longtable

Jor*_*dan 4 r longtable knitr r-markdown kableextra

使用 R Markdown 输出 pdf。kable() 效果很好,但是当我添加longtable=T标题时,不再扩展表格的整个宽度。我似乎找不到一个可以控制此处标题详细信息的参数。我可以将标题移动到每个代码块的输出,但如果可能的话,我宁愿使用 kable 中的内置功能。

谢谢!

---
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()
```
Run Code Online (Sandbox Code Playgroud)

use*_*330 6

这可能是包中的 LaTeX 问题longtable。本页面建议了一种解决方法: https://tex.stackexchange.com/questions/287283/how-to-define-caption-width-in-longtable。就放

header-includes:
   - \usepackage{caption}
Run Code Online (Sandbox Code Playgroud)

在您的 YAML 标头中,事情就会按您预期的方式工作。您还可以添加 LaTeX 代码

\captionsetup{width=5in}
Run Code Online (Sandbox Code Playgroud)

(或使用其他一些措施,例如5cm\textwidth\textheight等)以获得其他尺寸的一致标题宽度。

编辑添加:

这是原始示例,按照建议进行修改(加上一些清理):

---
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()
```
Run Code Online (Sandbox Code Playgroud)

这是它生成的两个表:

在此输入图像描述

截屏