【问题标题】:Table Cross-References in Bookdown with MS-Word Output?带有 MS-Word 输出的 Bookdown 中的表交叉引用?
【发布时间】:2019-06-19 23:02:18
【问题描述】:

如何使表格交叉引用在具有所有输出格式 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). 

【问题讨论】:

  • set_caption 应该会从新版本的 flextable 开始生效(cran 即将发布 0.5.5)。但是,bookdown 对每个表格标题进行编号(在 markdown 表格之后或之前以 Table: 开头),并且 flextable 无法将自己与此过程挂钩(我正在研究它,但目前没有解决方案)。这同样适用于灵活的 HTML 输出。我想解决这个问题,如果有任何想法,欢迎

标签: r ms-word bookdown kable flextable


【解决方案1】:

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"

【讨论】:

  • 块名不能包含下划线,否则交叉引用将不起作用。
猜你喜欢
  • 2018-09-23
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多