How do I create a huxtable table caption using bookdown in rmarkdown?

我们两清 提交于 2019-12-24 19:04:05

问题


I have numerous huxtable tables in my rmarkdown. I'd like to caption them using bookdown. So far I've been unable to do this using the bookdown instructions for "other R packages to generate tables" (see URL above).

Here's an example which follows the instructions in this answer:

---
title: "huxtable-mwe"
site: bookdown::bookdown_site
output:
  bookdown::html_book
documentclass: book
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(huxtable)
library(magrittr)
```

See table \@ref(tab:bar).

Table (\#tab:foo): Foo

```{r foo, echo=FALSE}
ht <- hux(
  foo = c('foo','bar')
) %>%
  set_all_borders(1)
ht
```

See table \@ref(tab:foo).

Table (\#tab:bar): Bar

```{r bar, echo=FALSE}
ht <- hux(
  foo = c('bar', 'baz')
) %>%
  set_all_borders(1)
ht
```

References work, but I get the following table captions:

Table (#tab:foo): Foo

Table (#tab:bar): Bar

when I expected:

Table 1: Foo

Table 2: Bar

Grateful for a MWE.


回答1:


Resolved. Use set_caption(...) to get the caption into a <caption>...</caption> element, and don't escape the label:

---
title: "huxtable-mwe"
site: bookdown::bookdown_site
output:
  bookdown::html_book
documentclass: book
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(huxtable)
library(magrittr)
```

See table \@ref(tab:bar).

```{r foo, echo=FALSE}
ht <- hux(
  foo = c('foo','bar')
) %>%
  set_all_borders(1) %>%
  set_caption('(#tab:foo) Foo')
ht
```

See table \@ref(tab:foo).

```{r bar, echo=FALSE}
ht <- hux(
  foo = c('bar', 'baz')
) %>%
  set_all_borders(1) %>%
  set_caption('(#tab:bar) Bar')
ht
```


来源:https://stackoverflow.com/questions/46318658/how-do-i-create-a-huxtable-table-caption-using-bookdown-in-rmarkdown

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