Table numbering in bookdown: Why can only kable do it portably; huxtable, pander, flextable fail

Being able to number tables consistently is of highest importance in all publications, and the kable in a bookdown can do it nicely for html and Word documents via pandoc (I have not checked latex/pdf here).

All other packages that do not indirectly use kable fail. huxtable at least works for HTML. While kable covers 90% of my uses, I occasionally would like special features like coloring of cells. As yihui writes here, he is not happy with the situation, but I accept that it is not his job to add kable-like feature to other authors' packages.

However, it would be nice if something like a framework existed so that package authors could cleanly implement table numbering with bookdown.

---
title: "Table numbering in html and Word"
output: 
  bookdown::html_document2:
    toc: true
  bookdown::word_document2:
    toc: true
---


# Introduction 

None of the crosslinks work in Word; this is a pandoc problem


See

(mtcars) \@ref(tab:mtcars) 

(mtcars1) \@ref(tab:mtcars1)

(mtflex) \@ref(tab:mtflex)

(mthux) \@ref(tab:mthux)



+Html means: Numbering works in HTML


## But how about numbering

https://github.com/rstudio/bookdown/issues/746#issuecomment-513453147


```{r,  echo=FALSE}
library(knitr)
opts_chunk$set(warning = FALSE, echo = FALSE)
```

### kable (first)
```{r mtcars}
suppressPackageStartupMessages(library(dplyr))
mt = mtcars[1:5, 1:5]
knitr::kable(mt, caption = "This is mtcars,  +Html +Word")
```


### kable (second)

```{r mtcars1}
knitr::kable(mt, caption = "This is mtcars1, +Html +Word")
```


### huxtable

```{r tab:mthux}
options('huxtable.bookdown'=TRUE) # Should be automatic, just to be sure
h = huxtable::huxtable(mt)
huxtable::caption(h) = "(#tab:mthux)This is hux, +Html -Word"
h
```

### huxtable Using atusy hack with backslashes

https://github.com/rstudio/bookdown/issues/746#issuecomment-519780681

```{r tab:mthux1}
options('huxtable.bookdown'=TRUE) # Should be automatic, just to be sure
h = huxtable::huxtable(mt)
huxtable::caption(h) = "Table: (\\#tab:mthux1)This is hacked hux, +Html -Word"
h
```


### pander

```{r tab:mtpander}
library(pander)
pander(mt, caption = "(#tab:mtpander)This is pander, +Html -Word")
```


### flextable

See also

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

```{r tab:mtflex}
f = flextable::flextable(mt)
flextable::set_caption(f, "(#tab:mtflex)This is mtflex, -Html -Word ")
f
```


1 Like

The "hacked hux" version works for me in Word, in the sense that output looks like:

Table: Table 4: This is hacked hux, +Html -Word

That is not ideal because it says "Table" twice.

The underlying issue is with flextable, which huxtable uses. kable prints out a markdown table, and markdown (or bookdown) then autointerprets the numbering. flextable prints out a full Word XML table. This is what gives flextable the power to do more styling than kable, but it means the autonumbering doesn't quite work. See the comment by @davidgohel here:

The "Table table" was no acceptable for me. I had not noticed that huxtable uses flextable internally. As @davidgohel mentioned on github, coming versions will support automatic table numbering.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.