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

fix disappearance of figure captions when a chunk generates multiple figures continuously #1760

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

atusy
Copy link
Collaborator

@atusy atusy commented Sep 6, 2019

Knitr generates multiple figures in one line when a chunk generates multiple figures continuously.

For example,

```{r echo=FALSE, fig.cap = c('cap1', 'cap2')}
plot(1)
plot(2)
```

becomes something like

![cap1](plot1.png)![cap2](plot2.png)

In such case, Pandoc will suppress figure captions.
Yihui pointed out this in #1756 and I did in #1755 .
These issues were about office formats, but the issue occurs also in HTML and PDF.

I think users expect figure captions be shown rather than figures stay in one line.

This PR fixes this problem by adding blank lines between captioned figures.

![cap1](plot1.png)

![cap2](plot2.png)

The current implementation requires fig.show='hold' or echo=FALSE, which is maybe too strict because

```{r, fig.cap = c('cap1', 'cap2')}
g <- ggplot2::qplot(1, 1)
purrr::walk(list(g, g), print)
```

is still like

![cap1](plot1.png)![cap2](plot2.png)

Unfortunately, I have no idea how to detect such a case.
A workaround is to add blank lines regardless of what is specified to fig.show and echo.
A disadvantage of the workaround is that needless blank lines will be added when multiple figures are generated by a chunk but are separated from each other by something, i.e.

```{r fig.cap = c('cap1', 'cap2')}
plot(1)
plot(2)
```

will be like

```r
plot(1)
```

![cap1](plot1.png)



```r
plot(2)
```

![cap2](plot2.png)

IMO, For the most of the formats, this is okay because the markdown format is just the intermediate format.
Some may dislike the needless blank lines when they want the markdown format as the output format.

Any thoughts?

Example Rmd

---
title: "Untitled"
output :
  html_document: default
  rtf_document: default
  odt_document: default
  powerpoint_presentation: default
  word_document: default
  pdf_document: default
---

# echo is FALSE

```{r base, fig.cap=.cap, echo=FALSE}
.cap <- c('cap1', 'cap2')
plot(1)
plot(2)
```

# fig.show is hold

```{r ref.label='base', fig.cap=.cap, fig.show='hold'}
```

@CLAassistant
Copy link

CLAassistant commented Sep 22, 2020

CLA assistant check
All committers have signed the CLA.

@atusy
Copy link
Collaborator Author

atusy commented Nov 11, 2020

I see I need some improvements.
I would happy to work on, but want to make sure if we want this solved as this PR has been open for longer than a year.

Current implementation tries to split sub-figures when fig.cap is specified.
However, I now think the split should take place only if the length of fig.cap is equal to the number of figures.

Also, in html_document, Splitting inlined figures into multiple lines will not show figure captions for each figures.

@cderv
Copy link
Collaborator

cderv commented Nov 11, 2020

We've been focus on other subject and did not have time to look into it yet. I'll try to look at this issue and suggested fix later this week. Sorry for the delay.

@atusy
Copy link
Collaborator Author

atusy commented Nov 11, 2020

Don't worry :) I'm not rush about this.

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

Successfully merging this pull request may close these issues.

Figure Captions with Multiple Figures and echo=FALSE Does not Print the Caption
3 participants