bookdown-demo icon indicating copy to clipboard operation
bookdown-demo copied to clipboard

Table captions in HTML output for package Huxtable

Open atyre2 opened this issue 7 years ago • 6 comments

I would like to be able to make automatically numbered table captions using package huxtable, which allows for spanning columns/rows and other nice features. It is very easy to use. I added the code chunk and following text to 01-intro.Rmd to the rstudio/bookdown-demo repository (after forking and cloning, of course!)

```{r nice-tab2, results='asis', tidy=FALSE}
library(huxtable)
ht <- hux(head(iris, 5), add_colnames = TRUE)
caption(ht) <- '(\\#tab:nice-tab2) Here is another nice table!'
ht

I want to be able to generate crossreferences and numbered captions for tables (Table \@ref(tab:nice-tab2)), just as I can for knitr::kable(). According to the directions in the bookdown book, I should be able to do this if I follow certain criteria. So far it works for pdf output, but not for the html output.

I have confirmed that the html produced by huxtable uses the <caption></caption> tag by doing print_html(ht). I believe I'm using the correct form for the label (and it works for pdf, so yes). I also tried with results = 'markup'.

I don't know if this is a bookdown issue or a huxtable issue

atyre2 avatar Jul 23 '17 13:07 atyre2

Sorry I don't have time to investigate this. If you want to debug it by yourself, the function you need to debug is bookdown:::parse_fig_labels. In particular, this line: https://github.com/rstudio/bookdown/blob/a8c931e5dae3c11e230635504c9cf13bc250feb2/R/html.R#L530

yihui avatar Jul 23 '17 20:07 yihui

Thanks for the pointer for where to look!

Stepping through bookdown:::parse_fig_labels I discovered that the label in the caption for the huxtable is not picked up here: https://github.com/rstudio/bookdown/blob/a8c931e5dae3c11e230635504c9cf13bc250feb2/R/html.R#L496 The label produced by kable() does not have the leading slash. When I remove the slash from the manual label

caption(ht) <- '(#tab:nice-tab2) Here is another nice table!'

the HTML output captures and produces the label and cross reference correctly but the pdf output doesn't. So there's a bit of a workaround as long as only having one output is not a problem.

atyre2 avatar Jul 24 '17 14:07 atyre2

Interesting. Thanks for the investigation. I don't have a clue on top of my head why that is the case.

yihui avatar Jul 24 '17 16:07 yihui

I came across another oddity with caption labels. If I use an '_' instead of a '-' (#tab:nice_tab2) the caption isn't recognized either. If I do (\\#tab:nice_tab2) and go for pdf output ... it fails too, with lots of undefined refs and citations.

In the documentation '_' is acceptable for chunk labels? Consistency is a virtue in chunk labels! I need to stop mixing camelCase, dashes and underscores ...

atyre2 avatar Jul 29 '17 12:07 atyre2

Here is the documentation: https://bookdown.org/yihui/bookdown/figures.html No, _ is not allowed.

yihui avatar Jul 30 '17 05:07 yihui

Oops. My bad, sorry for the false alarm. Would it be worth adding a phrase to the bit on manual table labels like this: line 558: ... a labeled caption of the form Table: (\#label) Caption here, where label must have the prefix tab:, e.g., tab:simple-table and otherwise only contain alphanumeric characters (a-z, A-Z, 0-9), slashes (/), or dashes (-).

atyre2 avatar Jul 30 '17 11:07 atyre2