kable_styling with full_width = T in a tint document

kable_styling with full_width = T in a tint document

我有以下 tint PDF 文档:

---
title: "Title"
subtitle: "Subtitle"
author: "Author"
date: "`r Sys.Date()`"
output: tint::tintPdf
---

```{r echo = FALSE, message = FALSE}
# Load library
library(dplyr)

# Create data frame
df <- data.frame(A = runif(10), 
                 B = runif(10), 
                 C = runif(10), 
                 D = runif(10),
                 E = runif(10),
                 F = runif(10),
                 G = runif(10),
                 H = runif(10))

# Print as a table
knitr::kable(df, booktabs = TRUE, format = "latex", 
             caption = "This is a caption in the margin.") 
```

这将生成具有以下内容的 PDF table:

table 很宽,超出了标题。为避免这种情况,我可以在 kableExtra.

kable_styling 函数中使用 full_width = TRUE 指定它是全宽 table
---
title: "Title"
subtitle: "Subtitle"
author: "Author"
date: "`r Sys.Date()`"
output: tint::tintPdf
---

```{r echo = FALSE, message = FALSE}
# Load library
library(dplyr)

# Create data frame
df <- data.frame(A = runif(10), 
                 B = runif(10), 
                 C = runif(10), 
                 D = runif(10),
                 E = runif(10),
                 F = runif(10),
                 G = runif(10),
                 H = runif(10))

# Print as a table
knitr::kable(df, booktabs = TRUE, format = "latex", 
             caption = "This is a caption in the margin.") %>%
  kableExtra::kable_styling(full_width = TRUE)
```

这会产生以下错误:

! LaTeX Error: Environment tabu undefined.

Error: Failed to compile Test.tex. See Test.log for more info. Execution halted

似乎对 tabu 包(或缺少包)感到不安。所以,我在我的 YAML 中添加了这个包,如下所示:

---
title: "Title"
subtitle: "Subtitle"
author: "Author"
date: "`r Sys.Date()`"
output: tint::tintPdf
header-includes:
  - \usepackage{tabu}
---

这会运行,但会产生以下内容:

现在,table 的内容重叠了。哼哼。即使我在块选项中包含 fig.fullwidth = TRUE 我也没有运气。

在这种情况下如何生成全宽 table?

这是一个解决方法。通过在 kable 中指定 longtable = TRUE,标题会被顶到顶部。此外,在 YAML 中:tables: yes。违反直觉的是,将 kable_stylingfull_width = TRUE 一起使用会将 table 压缩到主 body 的整个宽度,而 而不是 主 body + 保证金。

---
title: "Title"
subtitle: "Subtitle"
author: "Author"
date: "`r Sys.Date()`"
output: tint::tintPdf
tables: yes
---

```{r echo = FALSE, message = FALSE}
# Load library
library(dplyr)

# Create data frame
df <- data.frame(A = runif(10), 
                 B = runif(10), 
                 C = runif(10), 
                 D = runif(10),
                 E = runif(10),
                 F = runif(10),
                 G = runif(10),
                 H = runif(10))

# Print as a table
knitr::kable(df, booktabs = TRUE, format = "latex", longtable = TRUE,
             caption = "This is a caption in the margin.") 
```