timtrice
timtrice

Reputation: 307

Prefixing chunk label with "plot" or "plt" modifies figure caption

I recently began prefixing chunk labels to help me identify expected output; i.e., "plot" for a plot, "tbl" for table, etc.

This morning I attempted to add a fig.cap to a plot. The caption will not display correctly; it looks like this:

(#fig:plt_cars)This caption will not display correctly.

I expect this:

Figure 1: This caption will display correctly.

After playing around I have found that adding "plot" or "plt" as a prefix to a chunk label causes this.

The following example demonstrates this.

---
title: Test Post
author: Tim Trice
date: '2017-10-14'
slug: test-post
categories: []
tags: []
---

```{r}
library(ggplot2)
```

```{r}
data(cars)
```

```{r cars, fig.cap = "This caption will display correctly."}
ggplot(cars, aes(x = speed, y = dist)) + geom_point()
```

```{r plt_cars, fig.cap = "This caption will not display correctly."}
ggplot(cars, aes(x = speed, y = dist)) + geom_point()
```

```{r plot_cars, fig.cap = "Nor will this caption display correctly."}
ggplot(cars, aes(x = speed, y = dist)) + geom_point()
```

All captions will render fine in a normal Rmd document; but not in blogdown.

I am using blogdown 0.1.

I have verified this on Debian and Windows but only R 3.4.0 at the moment.

Can anyone advise why I wouldn't be able to use those prefixes?

Edit: It is not the prefix but rather using either delimiter "_" or ".".

These examples do not work:

```{r test_cars, fig.cap = "Nor will this caption display correctly."}
ggplot(cars, aes(x = speed, y = dist)) + geom_point()
```

```{r test.cars, fig.cap = "Nor will this caption display correctly."}
ggplot(cars, aes(x = speed, y = dist)) + geom_point()
```

```{r c_a_r_s, fig.cap = "Nor will this caption display correctly."}
ggplot(cars, aes(x = speed, y = dist)) + geom_point()
```

Edit 2: Same issue applies towards knitr::kable captions.

```{r tblcars}
kable(cars, caption = "This table caption works.")
```

```{r tbl_cars}
kable(cars, caption = "This table caption does not work.")
```

Upvotes: 1

Views: 114

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30174

From Appendix A of the blogdown book:

[...] and you need to read Chapter 2 of the bookdown book (Xie 2016) to learn more about these [R Markdown] features.

From Section 2.4 of the bookdown book:

If you want to cross-reference figures or tables generated from a code chunk, please make sure the chunk label only contains alphanumeric characters (a-z, A-Z, 0-9), slashes (/), or dashes (-).

Underscores are not supported if you want to number or cross-reference figures.

Upvotes: 1

Related Questions