-
Notifications
You must be signed in to change notification settings - Fork 57
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
For include_graphics, out.width ignored when fig.cap is set #286
Comments
I can confirm I am seeing this too, not only with include graphics but any figures produced with R code, and not in a bookdown project. https://github.com/apreshill/knitrfigcap Code:
|
I'm seeing the same problem. My temporary fix is to use markdown syntax to insert an image (obviously doesn't work with rendered plots): ![Insert caption text](images/image.png){width=80%} But this solution doesn't allow me to align the image (e.g. left, center, right). It's always left-justified. |
Just noting that I think this has been fixed in v1.2. Images resize now with figure captions according to the |
@jhelvy I don't think this has been fixed. Can you share a small example that works for you ? ```{r nice-fig, fig.asp=.75, fig.align='center'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
```{r ref.label="nice-fig", out.width="50%"}
```
```{r ref.label="nice-fig", fig.cap='Here is a nice figure!', out.width="50%"}
``` This is the HTML code that is outputed for the second plot <div class="figure"><span style="display:block;" id="fig:unnamed-chunk-2"></span>
<img src="test_files/figure-html5/unnamed-chunk-2-1.png" alt="Here is a nice figure!" width="50%" style="max-width: 100%;">
<p class="caption">
Figure 1: Here is a nice figure!
</p>
</div> The issue comes from the fact that knitr currently behave this way:
distill/inst/rmarkdown/templates/distill_article/resources/distill.html Lines 310 to 312 in cd7cf53
This CSS comes from distill and is inserted into the HTML. We need to see how we can tweak it. Current workaround would be to override this CSS rule for the chunk to apply |
Oh interesting ! However, I tried this and it still got me incorrect width ---
title: "Untitled"
output: distill::distill_article
---
```{r nice-fig, fig.asp=.75, fig.align='center'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
```{r ref.label="nice-fig", out.width="300px"}
```
```{r ref.label="nice-fig", fig.cap='Here is a nice figure!', out.width="300px"}
```
Seems like the unit is the key. When removing it as you have done in your source, it works ok. ---
title: "Untitled"
output: distill::distill_article
---
```{r nice-fig, fig.asp=.75, fig.align='center'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
```{r ref.label="nice-fig", out.width="300"}
```
```{r ref.label="nice-fig", fig.cap='Here is a nice figure!', out.width="300"}
``` I don't know why, but when no unit is passed <img src="data:image/png;base64,iVBORw0(...)" alt="Here is a nice figure!" width="300" style="max-width: 100%; width: 300px;"> I need to see where this difference in behavior happens. However, this is a workaround for this issue. |
Yes, I noticed the same behavior. If I included |
Oh yes this is still a bug for sure. But your finding is really interesting, I was not aware of this. Thanks to sharing it! |
fix #286. This seems possible as width is also set / unset in JS for knitr plot output https://github.com/rstudio/distill/blob/0e06b3f7d2b3aafcdb0f3038b6573330df2d45dc/inst/rmarkdown/templates/distill_article/resources/distill.html#L1285-L1296
After much thinking and a few tries, I believe the safest things to do here is to remove the CSS rules that is taking precedence and rely only on inline attribute |
For use of
knitr::include_graphics
: without a figure caption, an image width can be specified byout.width
,but
out.width
is ignored and uses 100% whenfig.cap
is used:Distill version 1.1.2, GithubSHA1: e53e8ff
The text was updated successfully, but these errors were encountered: