Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table captions in HTML output for package Huxtable #19

Open
atyre2 opened this issue Jul 23, 2017 · 6 comments
Open

Table captions in HTML output for package Huxtable #19

atyre2 opened this issue Jul 23, 2017 · 6 comments

Comments

@atyre2
Copy link

atyre2 commented Jul 23, 2017

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

@yihui
Copy link
Member

yihui commented Jul 23, 2017

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

@atyre2
Copy link
Author

atyre2 commented Jul 24, 2017

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.

@yihui
Copy link
Member

yihui commented Jul 24, 2017

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

@atyre2
Copy link
Author

atyre2 commented Jul 29, 2017

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

@yihui
Copy link
Member

yihui commented Jul 30, 2017

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

@atyre2
Copy link
Author

atyre2 commented Jul 30, 2017

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 (-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants