It seems that a table generated using a code chunk is thrown into a `layout` environment and treated as a sub table. The caption and cross-ref only work when two captions are defined: both at the code function level and at the chunk level.

For example,

With the code bellow, the table is not numbered and cross-ref does not work.

````markdown
```{r}
#| label: tbl-no_kable_caption
#| tbl-cap: "A table from kable"

knitr::kable(mtcars[1:5, 1:5])
```
@tbl-no_kable_caption
````
With the code below, the table is numbered, cross-ref works, but the resulting table has two captions: the one defined in the kable function is used as a sub-table caption; and the one defined as the chunk option is used as the main caption. 
In addition, the placement of the main caption is below the table, not above it.

````markdown
```{r}
#| label: tbl-with_kable_caption
#| tbl-cap: Chunk Caption

knitr::kable(mtcars[1:5, 1:5], caption = "kable caption")
```
@tbl-with_kable_caption

````
With the code below, everything works fine, but it seems that there is no way to cross-ref the table.  Adding a label in `{r}` like the way in rmarkdown does not help neither.

````markdown
```{r}
knitr::kable(mtcars[1:5, 1:5], caption = "no chunk caption")
```
````
Attached is the output pdf file so you can see the differences.
[table-crossref.pdf](https://github.com/quarto-dev/quarto-cli/files/7732289/table-crossref.pdf)

What is the proper approach?

I understand that this is also related to an earlier issue (#90) that I submitted a while ago. Any improvement on this matter is appreciated. 


2021-12-17T05:38:16Z
juliantao
closed
Caption and cross-ref for single table generated from code chunk
https://github.com/quarto-dev/quarto-cli/issues/228