quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.59k stars 294 forks source link

[knitr] Cell should not allow two caption attributes #9936

Open yonisidi opened 1 month ago

yonisidi commented 1 month ago

When setting fig.cap in global knitr::opts_chunk every table gets appended the value set globally to fig.cap

--- 
title: "Test"
date: "`r Sys.Date()`"
format: pdf
editor_options: 
  chunk_output_type: console
---

```{r}
knitr::opts_chunk$set(fig.cap = '123')
tbl_data <- tibble::tibble(a = letters[1:5], b = LETTERS[1:5])

#| tbl-cap: 'Test GT'
#| tbl-label: test_gt
  tbl_data |> gt::gt()


[test.pdf](https://github.com/user-attachments/files/15739461/test.pdf)
cderv commented 1 month ago

First note that tbl-label is not a known option. I don't know what you expect from it, but I don't think it will have any effect.

When setting fig.cap in global knitr::opts_chunk

When you are doing this, this is equivalent to setting fig-cap in all your chunk individually.

--- 
title: "Test"
date: today
format: pdf
---

```{r}
tbl_data <- tibble::tibble(a = letters[1:5], b = LETTERS[1:5])
#| tbl-cap: 'Test GT'
#| tbl-label: test_gt
#| fig-cap: '123'
tbl_data |> gt::gt()

So basically, you get what you are asking, as you set two caption in the cell. I don't think this is really good practice so set `fig.cap` as a global option.  What use case do you have for this ? 

We could probably do better to help users do the right things, and check for them the option, but it could happen only in some specific context (like a prefixed label like `#[ label: tbl-tab` is used, so we know the caption should be `tbl-cap` and not `fig-cap`. 

But just to re-clarify here, the result is what is asked to **knitr**

I believe we will fix this when we will have the correct intermediate markdown syntax for computation 

* https://github.com/quarto-dev/quarto-cli/issues/7062

because **knitr** nor **jupyter** shouldn't be allowed in Quarto write a caption in a div syntax, and an attribute. 

## Issue to fix: Two caption attributes shouldn't be set on a cell

````markdown
--- 
title: "Test"
date: today
format: html
keep-md: true
---

```{r}
#| tbl-cap: 'Test GT'
#| fig-cap: '123'
tibble::tibble(a = letters[1:5], b = LETTERS[1:5]) |> knitr::kable()

![image](https://github.com/quarto-dev/quarto-cli/assets/6791940/d00d1768-66da-4b9b-b6c9-a47373b93a58)

with this intermediate

````markdown
--- 
title: "Test"
date: today
format: html
keep-md: true
---

::: {.cell tbl-cap='Test GT'}

```{.r .cell-code}
tibble::tibble(a = letters[1:5], b = LETTERS[1:5]) |> knitr::kable()

::: {.cell-output-display}

a b
a A
b B
c C
d D
e E

123 ::: :::



We see the in div caption text, handle by **knitr** and the attributes. 

This should not happen
yonisidi commented 1 month ago

right, i was pointing out that i am rendering a table and getting the figure caption too. which is an issue.

from the documentation of quarto: tbl-cap

comment regarding fig-cap as a global settings is right, I think that is an old habit from rmd settings that you had to "turn on" fig.caps at one point for it to work (maybe bookdown).