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

时间:2019-06-19 23:02:18

标签: r ms-word bookdown kable flextable

如何在所有输出格式为pdf,docx和html的书本文档中使用表交叉引用?或者更具体地说,如何获得适用于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). 

1 个答案:

答案 0 :(得分:1)

在编织块选项中添加tab.cap="Your Caption"

```{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"