1

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
image

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
image

```
---
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.

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
user97371
  • 35
  • 5

2 Answers2

3

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}
mindonly
  • 54
  • 3
  • Thanks @mindonly. This did work for me. It produces just a single number (the figure number or identifier) without a Figure prefix but that is fine and easily fixed. I really appreciate your help. Thank you. – user97371 Jul 30 '20 at 04:23
  • It worked using the prefix. – LEo Dec 06 '21 at 11:49
  • Unfortunately it does not work when creating the HTML document. It prints nothing. Any tip how to manage that for HTML as well? – LEo Dec 06 '21 at 11:50
1

I found that hyphens (and I believe under scores) caused problems for the referencing. So \@ref(fig:fig-margin) would not work, but \@ref(fig:figMargin) would, provided you renamed the chunk to figMargin.

  • 1
    Thanks @Jesse Brunner. Unfortunately this did not work for me. I still get @ref(fig:figMargin) as the output when I used figMargin without the hyphen as the chunk name. – user97371 Jul 30 '20 at 04:21