Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Markdown : \@ref() not working

Tags:

r

r-markdown

I'm having a lot of trouble getting basic references to work in R Markdown. To reduce complexity from my original project, I've decided to use the bookdown example code, but I'm experiencing the same problem. Here's a link to the intro exmample code: https://github.com/rstudio/bookdown-demo/blob/master/01-intro.Rmd

When I use Knitr to HTML or PDF the file is being generated fine but the references are not working, instead the file will just containt "@ref(example)". Here is an image to show better the output (my emphasis added in red):

problem

Direct link to image: https://i.imgur.com/2yxB5h3.png

Here is a minimal example:

---
title: "Minimal"
output: 
  pdf_document:
    fig_caption: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Here is a reference to the plot below \@ref(fig:minGraph)

```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```

With the output appearing as such: https://i.imgur.com/J3UECqn.png

like image 965
colton Avatar asked Aug 03 '18 16:08

colton


People also ask

Why is my R Markdown file not knitting?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt . To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.

How do you reference in R Markdown?

The usual way to include citations in an R Markdown document is to put references in a plain text file with the extension . bib, in BibTex format. Then reference the path to this file in index. Rmd's YAML header with bibliography: example.

How do I reference a section in R Markdown?

Authoring Books and Technical Documents with R Markdown In fact, you can also reference sections using the same syntax \@ref(label) , where label is the section ID. By default, Pandoc will generate an ID for all section headers, e.g., a section # Hello World will have an ID hello-world .


1 Answers

If you want make use of the bookdown extensions in a normal rmarkdown document you can use bookdown::html_document2 and bookdown::pdf_document2 instead of rmarkdown::html_document and rmarkdown::pdf_document. Example:

---
title: "Minimal"
output: 
  bookdown::html_document2:
    fig_caption: yes
  bookdown::pdf_document2:
    fig_caption: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Here is a reference to the plot below \@ref(fig:minGraph)

```{r minGraph, echo=FALSE, fig.cap="test"}
plot(x=1)
```
like image 105
Ralf Stubner Avatar answered Sep 29 '22 07:09

Ralf Stubner