fig.cap and echo in markdown

3,869 views
Skip to first unread message

Gib Hemani

unread,
Jun 17, 2014, 7:22:51 PM6/17/14
to kn...@googlegroups.com
I am trying to add figure captions to plots in markdown, but I also want to set echo=FALSE. It seems that I can have figure captions with echo, or no figure captions without echo. e.g. This will produce no figure captions

# A minimal R Markdown example
A _paragraph_ here. A code chunk below (remember the three backticks):
```{r, fig.cap=c("hello", "bye"), echo=FALSE}
plot(1:10)
hist(rnorm(1000))
```

but this one will produce figure captions:

# A minimal R Markdown example
A _paragraph_ here. A code chunk below (remember the three backticks):
```{r, fig.cap=c("hello", "bye"), echo=TRUE}
plot(1:10)
hist(rnorm(1000))
```

Any idea how I can get figure captions, but without echo?

Many thanks

Carl Boettiger

unread,
Jun 18, 2014, 1:28:45 PM6/18/14
to Gib Hemani, knitr
Hi Gib,

Interesting! Looks like there are actually several problems here, most having very little to do with echo but more to do with markdown parsing. 

Recall knitr turns .Rmd to .md; other tools turn it to html. Note that the .md files created in your examples work as expected, the figure caption follows the Pandoc convention in the md, ![figure caption here](link to image here). 

Look closely though, and you'll also notice in your .md file that you are not starting a new line before either figure, your output markdown looks like: 

# A minimal R Markdown example
A _paragraph_ here. A code chunk below (remember the three backticks):
![hello](figure/unnamed-chunk-1.png) ![bye](figure/unnamed-chunk-2.png)


Pandoc interprets those figures as "inline" figures, which don't get captions. If you want figures with captions, it seems like you have to give each it's own block (and skip a line before starting a chunk!)


# A minimal R Markdown example
A _paragraph_ here. A code chunk below (remember the three backticks):

```{r, fig.cap=c("hello"), echo=FALSE}
plot(1:10)
```

```{r, fig.cap=c("bye"), echo=FALSE}
hist(rnorm(1000))
```

Use pandoc to compile: pandoc -s file.md -o file.html and voila, you have captions.  


Things get more complicated if you're using RStudio's html button.  Briefly, recall that the current RStudio doesn't use pandoc to generate HTML, but a different markdown flavor which ignores the pandoc-style captions.  The preview/pre-release version of RStudio (0.98.932) does use pandoc, but turns captions off by default to avoid lots of "unnamed-chunk-2" captions.  

(You can turn captions on for pdfs with fig_caption: true argument in the yaml, but I don't see how to turn them on for html when using the RStudio button/rmarkdown::render).  

 I note that "echo" doesn't have any effect on the html captions once you have skipped lines appropriately.  

HTH,

Carl







--
You received this message because you are subscribed to the Google Groups "knitr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knitr+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Carl Boettiger
UC Santa Cruz
http://carlboettiger.info/

Yihui Xie

unread,
Jun 21, 2014, 12:52:51 AM6/21/14
to Carl Boettiger, Gib Hemani, knitr
Yes, Carl's explanation is absolutely correct. You need ![]() in a
separate paragraph to get figure captions in HTML, as documented here:
http://johnmacfarlane.net/pandoc/README.html#images

The trick here is to add some line breaks between the two images:

---
output:
html_document:
fig_caption: yes
---

# A minimal R Markdown example

A _paragraph_ here.

```{r, fig.cap=c("hello", "bye"), echo=FALSE, fig.retina=1}
plot(1:10)
cat('\n\n')
hist(rnorm(1000))
```

Note the chunk option fig.retina=1: without it, rmarkdown::render()
will generate plots for Retina displays, which means the plots are
written in raw <img> tags instead of ![](), and Pandoc will not be
able to generate figure captions in that case.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name

Yihui Xie

unread,
Jun 21, 2014, 12:54:43 AM6/21/14
to Carl Boettiger, Gib Hemani, knitr
I forgot to mention that you can also set the fig.retina option
globally, either through opts_chunk$set(fig.retina = 1), or in the
YAML metadata, e.g.

---
output:
html_document:
fig_caption: yes
fig_retina: 1
---

# A minimal R Markdown example

A _paragraph_ here.

```{r, fig.cap=c("hello", "bye"), echo=FALSE}
plot(1:10)
cat('\n\n')
hist(rnorm(1000))
```


Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


Gib Hemani

unread,
Jun 21, 2014, 3:29:12 AM6/21/14
to kn...@googlegroups.com, cboe...@gmail.com, explode...@gmail.com
Dear Carl and Yihui

Very grateful for your responses, thanks for the explanations it all makes sense now =]

Knitr is cool
Gib
Reply all
Reply to author
Forward
0 new messages