1

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!

mugdi
  • 365
  • 5
  • 17
  • Does this answer your question: [knitr/rmarkdown/Latex: How to cross-reference figures and tables?](https://stackoverflow.com/a/38884378/12993861). Additionally using underscores in chunk labels is not recommended. See e.g. https://stackoverflow.com/a/57620745/12993861 – stefan Jan 18 '22 at 18:40
  • 1
    Edit the question. Sadly, the problem still exists. – mugdi Jan 18 '22 at 19:11
  • Try with adding a line break i.e. an empty line between your chunk and following text. – stefan Jan 18 '22 at 19:14
  • 1
    Very well. That solved the problem. Would you mind write a short answer to the problem so I can accept it? – mugdi Jan 18 '22 at 19:15

1 Answers1

2

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. 

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51