Skip to content
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

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

Open
dchiu911 opened this issue Jul 29, 2021 · 13 comments
Open

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

dchiu911 opened this issue Jul 29, 2021 · 13 comments

Comments

@dchiu911
Copy link

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`.

@atusy
Copy link
Collaborator

atusy commented Aug 3, 2021

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
Copy link
Collaborator

atusy commented Aug 4, 2021

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
Copy link
Collaborator

atusy commented Aug 4, 2021

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
Copy link
Collaborator

atusy commented Aug 4, 2021

and also in md_document

@cderv
Copy link
Collaborator

cderv commented Aug 16, 2021

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 ?

@atusy
Copy link
Collaborator

atusy commented Aug 16, 2021

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.

if (is.null(w) && is.null(h) && is.null(s) && is.null(options$fig.alt) &&

@cderv
Copy link
Collaborator

cderv commented Aug 16, 2021

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 ?

@atusy
Copy link
Collaborator

atusy commented Aug 17, 2021

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

knitr/R/utils.R

Lines 286 to 294 in 05ccb07

# for Retina displays, increase physical size, and decrease output size
if (is.numeric(r <- options$fig.retina) && r != 1) {
if (is.null(options[['out.width']])) {
options$out.width = options$fig.width * options$dpi
}
options$dpi = options$dpi * r
} else {
options$fig.retina = 1
}

@cderv
Copy link
Collaborator

cderv commented Aug 17, 2021

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
Copy link
Collaborator

cderv commented Aug 18, 2021

@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

@atusy
Copy link
Collaborator

atusy commented Aug 18, 2021

@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.

@yihui
Copy link
Owner

yihui commented Aug 20, 2021

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.

@atusy
Copy link
Collaborator

atusy commented Aug 20, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants