yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.38k stars 877 forks source link

fig.cap and fig.alt appear redundant (fig.alt is preferred) #2029

Open alanocallaghan opened 3 years ago

alanocallaghan commented 3 years ago

I was very grateful to find the new alt-text feature. However it doesn't seem to work as described, and when both fig.cap and fig.alt are supplied, only fig.alt is present in the output. This is not ideal as alt-text may need be more detailed than the caption for some images.

I have made a tiny testing repo here to demonstrate: https://github.com/Alanocallaghan/testalt/ The issues page suggests zipfiles, so here's that repo as a zip too: testalt.zip

r$> xfun::session_info()                                                        
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Locale:
  LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
  LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
  LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
  LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C            
  LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

Package version:
  compiler_4.1.0  evaluate_0.14   glue_1.4.2      graphics_4.1.0 
  grDevices_4.1.0 highr_0.9       knitr_1.33.8    magrittr_2.0.1 
  markdown_1.1    methods_4.1.0   mime_0.11       parallel_4.1.0 
  png_0.1-7       stats_4.1.0     stringi_1.7.3   stringr_1.4.0  
  tools_4.1.0     utils_4.1.0     xfun_0.24       yaml_2.2.1     

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

cderv commented 3 years ago

Thanks for the report. I worked on this feature, so I'll have a look at your reproducible example. And then I'll have a look at your PR.

cderv commented 3 years ago

Can you clarify what results you are expecting ?

The fig.alt feature was made primarly for HTML output, and is not correctly supported in other type of output. When I render the document to rmarkdown::html_document() using rmarkdown::render("test.Rmd", rmarkdown::html_document(keep_md=TRUE)", this is the content of the md file I get before convertion to HTML

---
title: Testing alt-text
output: 
  html_document:
    keep_md: true
---

```r
knitr::include_graphics("mcgriff.png")
knitr::include_graphics("mcgriff.png")
Caption only.

Caption only.

knitr::include_graphics("mcgriff.png")
This has alt text too.

Caption and alt text.

knitr::include_graphics("mcgriff.png")
This only has alt text.


It seems in this case, this is the expected result. isn't it ?

It would be interesting to have the detailed of what you expect. Either in the issue discussion here, or as detailed explanation in your PR. 

Changing this feature is touchy and we need to make sure we improve one output without breaking another one. That is why I am interested in the detail of your use case (you are not using **rmarkdown** and only **knitr** ? 

Thanks! 
alanocallaghan commented 3 years ago

Yes, I am only using knitr to knit to a markdown document. You can see here that knitr::knit overwrites the fig.cap as the title attribute of the img tag when fig.alt is set.

Specifically, the output is:

---
title: Testing alt-text
---

```r
knitr::include_graphics("mcgriff.png")

plot of chunk nothing

knitr::include_graphics("mcgriff.png")

Caption only.

knitr::include_graphics("mcgriff.png")
This has alt text too.
knitr::include_graphics("mcgriff.png")
This only has alt text.

while I would hope it would be

````md
---
title: Testing alt-text
---

```r
knitr::include_graphics("mcgriff.png")

plot of chunk nothing

knitr::include_graphics("mcgriff.png")

Caption only.

knitr::include_graphics("mcgriff.png")
This has alt text too.
knitr::include_graphics("mcgriff.png")
This only has alt text.
alanocallaghan commented 3 years ago

The reason I'm knitting to a markdown doc rather than html directly is because it's part of a jekyll site setup, fwiw. Part of a carpentries lesson.

cderv commented 3 years ago

The reason I'm knitting to a markdown doc rather than html directly is because it's part of a jekyll site setup, fwiw. Part of a carpentries lesson.

That is really interesting. We don't have a jekyll_document indeed. So we need to make fig.alt supported for markdown output resulting of knitr::knit with Rmd input.

That makes sense to me now. Thanks for the precision!