I am trying to write a report using R markdown with the Tufte handout output to produce a pdf. I have been unable to place figure references in the text using any of the suggested approaches. I am hoping someone could illustrate how I might do it. I would be grateful for any suggestions.
So far I have tried two broad approaches both described in this stackoverflow response.
Option 1: the base markdown approach of using \@ref(fig:chunk-label)
. This results in a pdf that has the correct format in the figure caption but the in-text reference is @ref(fig:fig-margin).
. See image below the script:
```
---
title: "Markdown example"
date: "`r Sys.Date()`"
output:
tufte::tufte_handout:
citation_package: natbib
link-citations: yes
---
```{r setup, include=FALSE}
library(tufte)
```
```{r fig-margin, fig.cap=("MPG vs horsepower, colored by transmission."), fig.height=3.5, fig.margin=TRUE, fig.width=3.5, message=FALSE, cache=FALSE}
library(ggplot2)
mtcars2 <- mtcars
mtcars2$am <- factor(
mtcars$am, labels = c('automatic', 'manual')
)
ggplot(mtcars2, aes(hp, mpg, color = am)) +
geom_point() + geom_smooth() +
theme(legend.position = 'bottom')
```
Insert the figure reference here \@ref(fig:fig-margin).
There is an image from the pdf copied here but I do not have points to paste it. Hopefully it is visible
I have tried multiple variations on the \@ref(fig:fig-margin)
format, such as removing the backslash, but to no avail.
Option 2: Using captioner. In the answer options of the link above Captioner was suggested as a solution and some code was provided.
I implemented this option and this time got the correct inline reference but the Figure 1: part of the caption on the figure was duplicated (i.e. it read Figure 1: Figure 1: MPG vs horsepower etc).
Here is an image from the pdf using the Captioner approach
```
---
title: "Captioner example"
date: "`r Sys.Date()`"
output:
tufte::tufte_handout:
citation_package: natbib
link-citations: yes
---
```{r setup, include=FALSE}
library(tufte)
library(captioner)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = FALSE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
table_captions <- captioner::captioner()
figure_captions <- captioner::captioner(prefix="Figure.")
fig.cap <- captioner::captioner()
t.ref <- function(label){
stringr::str_extract(table_captions(label), "[^:]*")
}
f.ref <- function(label){
stringr::str_extract(figure_captions(label), "[^:]*")
}
```
```{r fig-margin, fig.cap=figure_captions("fig_one", "MPG vs horsepower, colored by transmission."), fig.height=3.5, fig.margin=TRUE, fig.width=3.5, message=FALSE, cache=FALSE}
library(ggplot2)
mtcars2 <- mtcars
mtcars2$am <- factor(
mtcars$am, labels = c('automatic', 'manual')
)
ggplot(mtcars2, aes(hp, mpg, color = am)) +
geom_point() + geom_smooth() +
theme(legend.position = 'bottom')
```
Insert experiment ref here `r f.ref("fig_one")`.
My preference would be to use just a base markdown approach if that is possible but if the only way to do it is using Captioner that would be fine if someone could advise on getting rid of the duplication (I did try some adjustments to the functions defined in the setup chunk but none worked.
Any advice would be greatly appreciated. Plan B is of course just to do it manually.
Thanks in advance.
I ran into the same issue with tufte handouts (PDF) so I tried \ref
and braces { }
without the @-symbol as suggested in rmarkdown example #1 from the SO thread you linked above. Seems to be working.
\ref{fig:my_fig}