Table Cross-References in Bookdown with MS-Word Output?

大憨熊 提交于 2020-03-18 06:30:09

问题


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?

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). 

来源:https://stackoverflow.com/questions/56676952/table-cross-references-in-bookdown-with-ms-word-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!