简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM