1

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.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Paul
  • 527
  • 5
  • 17

1 Answers1

4

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
```
Paul
  • 527
  • 5
  • 17