Closed jay-sf closed 9 months ago
It seems to be a bookdown issue. Minimal reprex
---
title: "My Title"
subtitle: "Subtitle"
author: "Me"
date: "1/1/2016"
output: bookdown::html_document2
---
```{r cars, fig.cap = "<b>An amazing plot</b>"}
plot(cars)
Note that removing `<b>` around the caption takes solves it.
Using Markdown to put bold works too: `fig.cap = "**An amazing plot**"`
Thanks for prompt answer. Right, there are tags used and wanted. The problem appeared after updating all packages. Tried both, downgrading to rmarkdown_2.24
and bookdown_0.34
, but didn't help. Only downgrading to knitr_1.43
solved it, i.e. brought the old behavior shown in second figure back.
The problem comes from c7a423c02246ecea48a24197126d9343e126b9a6 in #2269 to fix #2023
We produce
<div class="figure">
<img src="test_files/figure-html/cars-1.png" alt="<b>An amazing plot</b>" />
<p class="caption">(\#fig:cars)<b>An amazing plot</b></p>
</div>
and before
<div class="figure">
<img src="test_files/figure-html/cars-1.png" alt="<b>An amazing plot</b>" />
<p class="caption">(\#fig:cars)<b>An amazing plot</b></p>
</div>
It seems this change has broken the refs resolving of bookdown
I believe the alt
text should contains escaped HTML balise
Right, there are tags used and wanted
An really about this, fig.alt
should probably be specified in that case as by default fig.alt
will be set to fig.cap
, and you don't want HTML tag in fig.alt
We could probably run xfun::strip_html()
on fig.alt
to insure that...
In the meantime, it seems one can use fig.cap = "**An amazing plot**"
instead
Thanks @etiennebacher ! That is what I mentioned in https://github.com/yihui/knitr/issues/2290#issuecomment-1729480818 above.
One could also provide fig.alt
also
---
title: "My Title"
subtitle: "Subtitle"
author: "Me"
date: "1/1/2016"
output: bookdown::html_document2
---
```{r cars, fig.cap = "<b>An amazing plot</b>", fig.alt="An amazing plot"}
plot(cars)
A `alt` text should have no HTML tag, so if some are passed for caption, they should be removed.
In **knitr**
* `fig.alt` is set to `fig.cap` is no alt text is provided.
* `fig.alt` is HTML escaped - which is #2291 fix as this was a regression in 1.44
HTML escape means that `<b>` and `</b>` will still be in the alt text, that is why I think if one need HTML tags in caption, then `fig.alt` should be set manually.
If I understand correctly, fig.alt=
is created automatically if not provided and escapes any HTML but this doesn't work well.
We could probably run xfun::strip_html() on fig.alt to insure that...
So I guess quoted suggestion would remove any HTML before creating automated alt text which I think would be desirable. Probably nobody needs tags in alt text, they are designed for screen readers or if images won't load, and both don't need HTML tags.
Just noticed that fig.alt=""
also works.
---
title: "My Title"
subtitle: "Subtitle"
author: "Me"
date: "1/1/2016"
output: bookdown::html_document2
---
```{r cars, fig.cap = "<b>An amazing plot</b>", fig.alt=""}
plot(cars)
```
But this probably isn't W3C conform.
If you could implement a fix, that would be great. I'm sure I'm not the only lazy person who doesn't specify a specific alt text, and you could keep tons of .rmds with gzillions of figures running without having to change their code.
With the PR #2291, special HTML characters will be escaped. If you want to strip HTML tags instead, you can use an option
hook:
knitr::opts_hooks$set(fig.cap = function(options) {
if (is.null(options$fig.alt)) options$fig.alt = options$fig.cap
options$fig.alt = xfun::strip_html(options$fig.alt)
options
})
Too bad, that does not work. Floating seems fixed, but no figure numbering.
---
title: "My Title"
subtitle: "Subtitle"
author: "Me"
date: "1/1/2016"
output: bookdown::html_document2
---
```{r, include=FALSE}
knitr::opts_hooks$set(fig.alt = function(options) {
if (is.null(options$fig.alt)) options$fig.alt = options$fig.cap
options$fig.alt = xfun::strip_html(options$fig.alt)
options
})
```
```{r cars, fig.cap = "<b>An amazing plot</b>"}
plot(cars)
```
I think the options hook should be on fig.cap
knitr::opts_hooks$set(fig.cap = function(options) {
if (is.null(options$fig.alt)) options$fig.alt = options$fig.cap
options$fig.alt = xfun::strip_html(options$fig.alt)
options
})
And you can install dev version of knitr which have the escape fix now since #2291 is merged
Works, thanks very much.
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.
After updating knitr_1.43 to knitr_1.44 captions are no longer numbered and floating right.
Example:
Should be:
Also see https://stackoverflow.com/q/77148220/6574038
Thanks J
By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.