knitr icon indicating copy to clipboard operation
knitr copied to clipboard

Setting message=FALSE chunk opts suppresses fig.cap for PDF output

Open dchiu911 opened this issue 3 years ago • 13 comments

I'm not sure when this started, but I've tried with the devel version of knitr and the issue persists: setting knitr::opts_chunk$set(message = FALSE) suppresses figure captions when the output is PDF.

---
title: "Untitled"
date: "7/29/2021"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE)
```

```{r test, fig.cap=caps}
library(ggplot2)
caps <- c("another caption", "and yet an other")
qplot(1:5)
qplot(6:10)
```

And it is important for me to have message=FALSE to suppress messages such as `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

dchiu911 avatar Jul 29 '21 19:07 dchiu911

Reproduced.

With keep_md: true, I see it happens because images are aligned inline like below. They should be separated by blank lines. I'll take a look.

![another caption](example_files/figure-latex/test-1.pdf) ![and yet an other](example_files/figure-latex/test-2.pdf) 

atusy avatar Aug 03 '21 23:08 atusy

I think this is the minimal reproducible example.

---
output: pdf_document
---

```{r test, fig.cap=caps, echo=FALSE}
caps <- c("another caption", "and yet an other")
plot(1:5)
plot(6:10)
```

atusy avatar Aug 04 '21 00:08 atusy

I also find that html_document(keep_md = TRUE) has the same problem, which is inconsistent to keep_md = FALSE.

---
output:
  html_document:
    keep_md: true
---

```{r test, fig.cap=caps, echo=FALSE}
caps <- c("another caption", "and yet an other")
plot(1:5)
plot(6:10)
```

atusy avatar Aug 04 '21 23:08 atusy

and also in md_document

atusy avatar Aug 04 '21 23:08 atusy

I also find that html_document(keep_md = TRUE) has the same problem, which is inconsistent to keep_md = FALSE.

@atusy, I am curious about this. Did you found why we get different result if keep_md is set to TRUE or FALSE ?

cderv avatar Aug 16 '21 14:08 cderv

That is because the w below is NULL when keep_md = TRUE, whereas w becomes 672 when keep_md = FALSE. That results in keep_md = TRUE to generate markdown output and keep_md = FALSE to generate HTML output. The value 672 is equal to 7 inch times 92 dpi, and comes from the default values of html_document.

https://github.com/yihui/knitr/blob/55a2df9353af1157b3954a968cfef5c1e30dd64c/R/hooks-md.R#L58

atusy avatar Aug 16 '21 15:08 atusy

Oh thanks. This seems odd, isn't it ? For me, keep_md = TRUE should result in "not-deleting" the md file generated by knitr as input for Pandoc. But here, it seems that knitr won't generate the same md file based on the keep_md options. This seems like a bug to me.

Am I missing something in what keep_md is supposed to really mean ?

cderv avatar Aug 16 '21 16:08 cderv

I do not know the background, but html_document(keep_md = FALSE) forces a chunk option fig.retina = NULL. This in turn makes difference in a chunk option, out.width.

https://github.com/rstudio/rmarkdown/blob/8cda18b54dc9db2273abcfd0862dccc19119cf09/R/html_document.R#L518-L519

https://github.com/yihui/knitr/blob/05ccb074deeedf699feff93712df6557c1143076/R/utils.R#L286-L294

atusy avatar Aug 17 '21 00:08 atusy

Thanks for the investigation @atusy ! That really helps !

@yihui do you have background to share on this difference in output when keep_md = TRUE or keep_md = FALSE ? It doesn't seem something we want... is it ? 🤔

cderv avatar Aug 17 '21 09:08 cderv

@atusy is this issue in fact the same as #1524 for which you already made a PR #1760 ?

Seems directly related as in the first example here, echo = FALSE is set.

There is definitely something with the plot hook that we need to look into and make sure it outputs correctly between format. Pandoc will soon (hopefully) gains better support for Figure with an environment syntax to not only rely on the +implicit_figures extensions, which mix inline image and figure environment image with same syntax

cderv avatar Aug 18 '21 08:08 cderv

@cderv Oh yes, you are right! Thanks. I was totally forgetting about it. It means this behavior has been continued at least two years.

Anyway, I close the #2033 and keep #1760.

atusy avatar Aug 18 '21 12:08 atusy

do you have background to share on this difference in output when keep_md = TRUE or keep_md = FALSE ? It doesn't seem something we want... is it ?

@cderv I don't know. It does sound like a bug.

yihui avatar Aug 20 '21 03:08 yihui

It seems the background is noted by @jjallaire https://github.com/rstudio/rmarkdown/blame/8cda18b54dc9db2273abcfd0862dccc19119cf09/R/html_document.R#L33

fig_retina Scaling to perform for retina displays (defaults to 2, which works for all widely used retina displays). Set to \code{NULL} to retina scaling. Note that this will always be \code{NULL} when \code{keep_md} is specified (this is because \code{fig_retina} relies on HTML directly into the markdown document).

But I think it is totally fine to include HTML output within md file.

atusy avatar Aug 20 '21 04:08 atusy