r - 带有 MS-Word 输出的 Bookdown 中的表格交叉引用?

标签 r ms-word bookdown kable flextable

如何使表格交叉引用在具有所有输出格式 pdf、docx 和 html 的 bookdown 文档中起作用?或者更具体地说,我怎样才能获得适用于 flextables 的表格交叉引用? ?

下面是一个最小的工作示例。第二个表,使用 kable() ,让我几乎一路走到了那里。问题是 docx 输出中的表格渲染完全无法使用(不是在这个 MWE 中,而是在我的实际用例中)。我考虑过使用 flextable 有条件地创建表用于 docx 输出和 kable用于 pdf 和 html 输出。 flextable在 docx 输出中看起来不错。但是表引用不起作用!

---
title: "A Book"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
  bookdown::word_document2: default
  bookdown::pdf_book: default
  bookdown::gitbook: default
---

# Hello World

```{r setup, include=FALSE}
library(dplyr)
library(flextable)
```

<!--- this tabulates in docx and html output --->
```{r, test01, echo = FALSE, eval = !knitr::is_latex_output()}
mtcars %>%
  head() %>%
  flextable() %>%
  set_caption("My caption!") %>%
  autofit()
```

<!--- this reference does not work in any form of output --->
Trying to reference Table \@ref(tab:test01). 

<!--- this tabulates in pdf, docx, html output (but very ugly in docx output) --->
```{r, test02, echo = FALSE}
mtcars %>%
  head() %>%
  knitr::kable(caption = "Need a caption!")
```

<!--- this reference works in pdf, docx, html output --->
Trying to reference Table \@ref(tab:test02). 

最佳答案

添加 tab.cap="Your Caption"到 knitr 块选项:

```{r, test03, echo = FALSE, eval = !knitr::is_latex_output(), tab.cap="My flextable caption!"}
mtcars %>%
  head() %>%
  flextable() %>%
  autofit()
```

Reference to Table \@ref(tab:test03). 
here更多表格标题选项。
这也正确地将数字添加到表中。如果您希望表格标题采用引用文档中指定的格式,例如“表格标题”或“标题”,您可以指定 tab.cap.style = "Table Caption" .

关于r - 带有 MS-Word 输出的 Bookdown 中的表格交叉引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56676952/

相关文章:

c# - COM Word Selection TypeText 带换行符

.net - 以编程方式替换 Word 文档中文本的最快方法

r-markdown - bookdown/rmarkdown/knitr : Open a document with the (graphical) result of a later code chunk?

VBA Word - 带有初始文件名的另存为对话框

css - 在 RStudio 的 bookdown 中创建自定义 block

r - `bookdown`/`rmarkdown`/`knitr` : Child documents and path definition in `YAML` headers

r - pdf 设备和字体系列 "Arial"/或 : Change font name (not font) in PDF

tol 参数在 QR 分解中的作用

r - 如何循环 lapply 以在 R 中的多个变量上创建 LAG 项

r - 计算任意两个节点之间的无向边