[英]R Markdown table caption width with kable and longtable
Using R Markdown to output a pdf.使用 R Markdown 输出 pdf。 kable() works great but when I add
longtable=T
the caption no longer extends the full width of the table. kable() 效果很好,但是当我添加
longtable=T
,标题不再扩展表格的整个宽度。 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.
我可以为每个代码块移动要输出的标题,但如果可能的话,我宁愿使用 kable 中的内置功能。
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.这可能是
longtable
包中的 LaTeX 问题。 This page suggests a workaround: https://tex.stackexchange.com/questions/287283/how-to-define-caption-width-in-longtable .此页面建议了一种解决方法: 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.在您的 YAML 标头中,事情将按您预期的方式工作。 You can also add the LaTeX code
您还可以添加 LaTeX 代码
\captionsetup{width=5in}
(or use some other measure, such as 5cm
, \\textwidth
, \\textheight
, etc.) to get consistent caption widths of other sizes. (或使用其他一些度量,例如
5cm
、 \\textwidth
、 \\textheight
等)以获得其他尺寸的一致标题宽度。
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:这是它生成的两个表:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.