我正在使用bookdown::pdf_document2
编写Rmarkdown文档,为此,我有一个表需要执行一些非常特定的操作,但是我根本找不到找到使所有这些文件同时工作的解决方案:
endfloat
包那样浮动到文档的末尾。我已经尝试使这三个选项与knitr::kable
,kableExtra::column_spec
和pander
的各种组合一起使用,但是我找不到一种使它们一次全部运行的方法。下面,我将粘贴一个示例Rmarkdown文档,其中包含各种测试,但没有一个完全有效。
是否有一种简单的方法可以让桌子做我想要的事情?甚至一个好的解决方法也是可以接受的...
谢谢,大卫
---
title: "Test table formatting"
author: "David M. Kaplan"
date: "5/24/2020"
output: bookdown::pdf_document2
header-includes:
- \usepackage[tablesfirst]{endfloat}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
# Data
```{r}
df.long = data.frame(
char = c('*this is some very long text with lots of words that will cause problems with word wrap if it is not properly handled with something like kableExtra*','**b**','~~c~~'),
num = c(1,2,3))
```
# Kable with format=pandoc
```{r}
knitr::kable(df.long,caption="test1",format="pandoc")
```
**Result:** Handles formatting, but table does not float and no wordwrap.
# Kable with booktab
```{r}
knitr::kable(df.long,caption="test2",booktab=TRUE)
```
**Result:** Floats, but does not handle formatting or do wordwrap.
# kableExtra for wordwrap
```{r}
knitr::kable(df.long,caption="test3",booktab=TRUE) %>%
kableExtra::column_spec(1,width="30em")
```
**Result:** Table floats and has wordwrap, but does not handle formatting.
# Pander
```{r}
pander::panderOptions("table.alignment.default","left")
pander::pander(df.long,caption="test4")
```
**Result:** Wordwrap and formatting, but does not float.
header-includes
条目,使用我发现的longtable
某些LaTeX魔术,将here声明为浮动香精。header-includes:
- \usepackage[tablesfirst]{endfloat}
- \DeclareDelayedFloatFlavour*{longtable}{table}
然后可以使用pander
生成表:
pander::panderOptions("table.alignment.default","left") pander::pander(df.long,caption="test pander",split.cell=50)