bookdown
bookdown copied to clipboard
Figure label printed in the caption using `bookdown::pdf_book()`
When using bookdown::pdf_book()
, the figure label is automatically printed inside the caption.
Here is an example R-markdown document.
---
title: "Example"
author: "Paul Hargarten"
date: "5/22/2020"
output:
bookdown::pdf_book
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
You can also embed plots (Figure \ref{fig::plots}), for example:
```{r pressure, echo = FALSE, fig.cap = "\\label{fig::plots} Some caption."}
plot(pressure)
```
But if I use `knitr::include_graphics()`, the label shows up in the caption of the picture.
```{r fig.r, fig.pos="h", echo=FALSE, fig.cap="\\label{fig::R} R is a flexible statistical program."}
knitr::include_graphics("Rlogo.pdf")
```
R is great (Figure \ref{fig::R}).
There is no label shown when I use `rmarkdown::pdf_document()`.
```
Using this image Rlogo.pdf, compiling this document yields
Example_for_StackOverflow.pdf.
The following contains the figure legend in the pdf file.
Figure 2: (#fig:fig.r) R is a flexible statistical program.
Is it possible to remove the figure label from the compiled caption? Thanks!
By filing an issue to this repo, I promise that
- [X] I have fully read the issue guide at https://yihui.org/issue/.
- [X] I have provided the necessary information about my issue.
- If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
- If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included
xfun::session_info('bookdown')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('rstudio/bookdown')
. - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [X] I have learned the Github Markdown syntax, and formatted my issue correctly.
I understand that my issue may be closed if I don't fulfill my promises.
NOTE: Due to the inability of saving files for a reproducible example on Stack Overflow, I am posting my issue here.
Where is your Stack Overflow post? (The issue guide asked you to provide the link to the post) Thanks!
I did not make one. As I am unable to save files for a reproducible example on Stack Overflow, I posted the issue here.
I stumbled upon the same issue and just in case of someone landing here as well: Apparently, R chunk names containing spaces, dots, or underscores (and maybe other special characters) are not correctly translated to labels in Latex. In the example below, instead of \caption{\label{tab:my.table}This is my table.}
it becomes \caption{(\#tab:my.table)This is my table).}
I do not know if this is the intended behavior or if it is a Latex limitation.
What worked for me, was restricting the chunk labels to simple one-word labels or only using regular hyphens.
Just a test. See Table \@ref(tab:my.table).
```{r my.table}
knitr::kable(
data.frame(
cbind(
numbers = c(1, 2, 3),
letters = c("a", "b", "c")
)
),
caption = "This is my table."
)
```
Thankyou so much @jogrue!
I was getting this HTML output:
Removing the _
's in the chunk names has caused the figure labels to render correctly. :+1:
Perhaps using underscores or other special chars in chunk names with a figure caption should cause a warning?
About the special characters in labels, this is related to other issues raised in the past with that: https://github.com/rstudio/bookdown/issues/839 https://github.com/rstudio/bookdown/issues/157 https://github.com/rstudio/bookdown/issues/941 https://github.com/rstudio/bookdown/issues/867 https://github.com/rstudio/bookdown/issues/406 https://github.com/rstudio/bookdown/issues/194 https://github.com/rstudio/bookdown/issues/336
This is indeed documented (in https://bookdown.org/yihui/bookdown/figures.html)
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 (-).
but the idea of a warning has also been already proposed. This impact only bookdown when labels is used for referencing but maybe we can detect when it is used and do something about this.