R Markdown表格标题宽度(带有线和长桌) [英] R Markdown table caption width with kable and longtable

查看:159
本文介绍了R Markdown表格标题宽度(带有线和长桌)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

谢谢!

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

解决方案

这可能是longtable软件包中的LaTeX问题.本页提出了一种解决方法: https://tex.stackexchange.com/Questions/287283/如何定义字幕在长表中的宽度.刚放

header-includes:
   - \usepackage{caption}

在您的YAML标头中,一切将按您预期的方式工作.您还可以添加LaTeX代码

\captionsetup{width=5in}

(或使用其他度量,例如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()
```

这是它生成的两个表:

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 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:

这篇关于R Markdown表格标题宽度(带有线和长桌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆