问题描述
如何使表格交叉引用在包含所有输出格式 pdf、docx 和 html 的 bookdown 文档中起作用?或者更具体地说,我怎样才能获得适用于 flextables
的表格交叉引用?
How can I make table cross-references work in a bookdown document with all of the output formats pdf, docx, and html? Or maybe more specifically, how can I get table cross-references working for flextables
?
下面是一个最小的工作示例.第二个表,使用 kable()
,让我几乎一路走到了那里.问题是 docx 输出中的表格渲染完全无法使用(不是在这个 MWE 中,而是在我的实际用例中).我考虑有条件地创建表格,使用 flextable
输出 docx 和 kable
输出 pdf 和 html .flextable
在 docx 输出中看起来不错.但是表引用不起作用!
Below is a minimal working example. The second table, using kable()
, gets me almost all the way there. The problem is that the table rendering in docx output is completely unusable (not in this MWE, but in my actual use-case). I considered creating the table conditionally, using flextable
for docx output and kable
for pdf and html output. flextable
looks good in docx output. But the table references don't work!
---
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 块选项:
Add tab.cap="Your Caption"
to the knitr chunk options:
```{r, test03, echo = FALSE, eval = !knitr::is_latex_output(), tab.cap="My flextable caption!"}
mtcars %>%
head() %>%
flextable() %>%
autofit()
```
Reference to Table \@ref(tab:test03).
请参阅此处了解更多表格标题选项.
See here for more table caption options.
这也正确地将数字添加到表格中.如果您希望表格标题采用参考文档中指定的格式,例如表格标题"或Caption",您可以指定tab.cap.style =Table Caption"
.
This also adds numbers to tables correctly. If you want your table captions to be in a format designated in your reference document like "Table Caption" or "Caption", you can specify tab.cap.style = "Table Caption"
.
这篇关于带有 MS-Word 输出的 Bookdown 中的表交叉引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!