svmiller / stevetemplates

My collection of R Markdown templates, as an R package.
http://svmiller.com/stevetemplates
125 stars 22 forks source link

Cross references to figures #12

Open MikkoVihtakari opened 2 years ago

MikkoVihtakari commented 2 years ago

A very basic question. How to add cross references to figures?

In bookdown::pdf_document2 we can do:

---
title: "Untitled"
author: "test"
date: "4/3/2022"
output: 
  bookdown::pdf_document2:
    toc: false
---

\```{r setup, include=FALSE} # escaping the \`, remove the \
knitr::opts_chunk$set(echo = FALSE)
\```

This is a cross reference to Figure \@ref(fig:aplot)

\``` {r, aplot, fig.cap="This is a plot"} 
plot(1)
\```

Is this possible in stevetemplates::article2?

The only way I manage is through the captioner package:

---
title: "Untitled"
author: "test"
date: "4/3/2022"
output: 
  stevetemplates::article2
---

\```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(captioner)
fig_nums <- captioner()
\```

This is a cross reference to `r fig_nums("aplot", display = "cite")`

\```{r, aplot}
plot(1)
\```
`r fig_nums(name = "aplot", caption = "This is a plot")`
svmiller commented 2 years ago

Pretty sure you can. You can see how I do this here through {bookdown}

https://github.com/svmiller/peacesciencer/blob/master/manuscript/src/render_pdf.R

Alas, you are right that I built the article2 function to not use {bookdown}. Let me look into this further.