Reputation: 415
The following MWE should correctly display the number of the figure by referring to it with \@ref(fig:chunk-label)
yet the reference is not found by the function. Is there any option that I have to add to the chunk header to achieve a correct reference?
MWE :
---
title: "Untitled"
author: "dsf"
date: "18 1 2022"
output:
bookdown::pdf_document2:
keep_tex: yes
fig_caption: true
number_sections: true
toc: true
lot: true
lof: true
graphics: true
---
```{r decision-tree, fig.cap="Decision Tree Example for the strava irmi dataset", echo= FALSE, message = FALSE}
library(rpart)
library(rattle)
library(tidyverse)
attach(mtcars)
train <- mtcars
train <- train %>%
mutate(across( .cols = everything(), ~scale(.x)))
# Create a decision tree model
tree <- rpart(mpg~., data=train, cp=.05)
# Visualize the decision tree with rpart.plot
fancyRpartPlot(tree,yesno=2,split.col="black",nn.col="black",
caption="Decision Tree Example for the irmi dataset",palette="Set3",branch.col="black")
```
Figure \@ref(fig:decision-tree) shows an example of an decision tree for the irmi dataset.
EDIT : Thanks to stefan. I followed knitr/rmarkdown/Latex: How to cross-reference figures and tables? closely and updated the MWE. Sadly, the solution did not solve the problem!
Upvotes: 1
Views: 78
Reputation: 125418
The issue is quite subtle. To make your reference work you have to add a line break after the code chunk and the following text:
---
title: "Untitled"
author: "dsf"
date: "18 1 2022"
output:
bookdown::pdf_document2:
keep_tex: yes
fig_caption: true
number_sections: true
toc: true
lot: true
lof: true
graphics: true
---
```{r decision-tree, fig.cap="Decision Tree Example for the strava irmi dataset", echo= FALSE, message = FALSE}
library(rpart)
library(rattle)
library(tidyverse)
attach(mtcars)
train <- mtcars
train <- train %>%
mutate(across( .cols = everything(), ~scale(.x)))
# Create a decision tree model
tree <- rpart(mpg~., data=train, cp=.05)
# Visualize the decision tree with rpart.plot
fancyRpartPlot(tree,yesno=2,split.col="black",nn.col="black",
caption="Decision Tree Example for the irmi dataset",palette="Set3",branch.col="black")
```
Figure \@ref(fig:decision-tree) shows an example of an decision tree for the irmi dataset.
Upvotes: 2