6

The bookdown offers great cross-referencing options for equations, figures, tables and sections: https://bookdown.org/yihui/bookdown/cross-references.html

However, they seems to not work when I set as an output 'rticles::elsevier_article'.

What are the available options for cross-referencing in rticles?

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
matandked
  • 1,527
  • 4
  • 26
  • 51

3 Answers3

6

I haven't tried, but there is a possible solution here: https://bookdown.org/yihui/bookdown/a-single-document.html

Particularly, specify in your YAML metadata:

output:
  bookdown::pdf_book:
    base_format: rticles::elsevier_article
  • 2
    Thank you for response. I'm aware of this solution. However I wonder if rticles do not offers its own referencing system. Regarding your response, I have problem with base_format of pdf_book. If I add line 'citation_package: natbib' in output, bibliography is not compiling at all. But I guess this is for different question. Are you sure there are no 'native' referencing options for rticles? – matandked Feb 21 '17 at 20:32
  • You could try [captioner package](https://cran.rstudio.com/web/packages/captioner/). See [vignette](https://cran.rstudio.com/web/packages/captioner/vignettes/using_captioner.html) or this short [tutorial](https://datascienceplus.com/r-markdown-how-to-number-and-reference-tables/). Or see also this related SO question: http://stackoverflow.com/questions/38861041/knitr-rmarkdown-latex-how-to-cross-reference-figures-and-tables – F Rodriguez-Sanchez Feb 22 '17 at 10:57
2

Since I am new using R Markdown I have decided to post this answer as some people may incur in the same mistake. I have tried myself F Rodriguez-Sanchez answer but it did not work. I got the following message:

! LaTeX Error: File `elsarticle.cls' not found.

! Emergency stop.
<read *> 

Erro: Failed to compile report.tex. See report.log for more info.

It did not work because I was making a rookie mistake since I was trying to add the suggested answer choosing New Markdownand then choosing Document.

I have then tried to open a New R Markdown choosing From Template and Elsevier Journal Article from rticles package. After that, I have used F Rodriguez-Sanchez suggested answer and it worked!

The final yaml header was:

---
title: Short Paper
author:
  - name: Alice Anonymous
    email: alice@example.com
    affiliation: Some Institute of Technology
    footnote: Corresponding Author
  - name: Bob Security
    email: bob@example.com
    affiliation: Another University
address:
  - code: Some Institute of Technology
    address: Department, Street, City, State, Zip
  - code: Another University
    address: Department, Street, City, State, Zip
abstract: |
  This is the abstract.

  It consists of two paragraphs.

journal: "An awesome journal"
date: "`r Sys.Date()`"
bibliography: mybibfile.bib
output:
  bookdown::pdf_book:
    base_format: rticles::elsevier_article
---
  • I have the same problem. Can you please add to your answer and example about how do you cross reference the figure? suggestion of `\@ref(my-chunk-name)` does not work. Thank you – maycca Jul 22 '20 at 13:43
  • @maycca ` c.f. Fig. \@ref(fig:my-chunk-name)` should get you there. – Ray Aug 19 '20 at 17:24
  • Thank you @Ray, I have tried it but it still does not work.. As output in RMarkdown, I am using `output: bookdown::pdf_book: base_format: rticles::elsevier_article` as suggested. Maybe there is another solution? Thanks again – maycca Sep 03 '20 at 05:58
  • No worries @maycca. I paste a longer answer below. Hope this helps. – Ray Oct 04 '20 at 16:43
0

@maycca please make sure to open the RMarkdown by choosing New Files from Template and select the Elsevier Journal version/template. The template(s) will be available after the rticles installation.

This will setup the article "infrastructure" (in particular the respective cls and other files). This also includes a mybibfile.bib example (thus, I would have not needed to comment the biblio out). If you choose to save this in a sub-folder, make sure that your Rmd file is saved in that sub-folder.

As presented above/below change the output: tag of the YAML to include the bookdown and baseformat rticles::elsevier_article pointer.
Check the use of colons and tabs carefully.

Based on the example above, you can then use the bookdown cross-referencing as shown below. I used
(i) an external (bookdown) figure caption defined before the code chunk using (ref:awesomeplotcaption). This is useful to keep the chunk options short(er).
(ii) a (bookdown) cross-reference to the figure \@ref(fig:awesomeplot). Please note that the \@ref(fig:...) uses the chunk-name to make the pointer work. Thus, make sure your chunk-name comes with standard letters, numbers, and dashes, i.e. no underscore!

Hitting the knit button will do the magic!

---
title: Short Paper
author:
  - name: Alice Anonymous
    email: alice@example.com
    affiliation: Some Institute of Technology
    footnote: Corresponding Author
  - name: Bob Security
    email: bob@example.com
    affiliation: Another University
address:
  - code: Some Institute of Technology
    address: Department, Street, City, State, Zip
  - code: Another University
    address: Department, Street, City, State, Zip
abstract: |
  This is the abstract.

  It consists of two paragraphs.

journal: "An awesome journal"
date: "`r Sys.Date()`"
#bibliography: mybibfile.bib
output:
  bookdown::pdf_book:
    base_format: rticles::elsevier_article
---

# First Heading

Some cool introductory text.

And an even more fascinating plot.

(ref:awesomeplotcaption) A simple demo plot

```{r awesomeplot, fig.cap="(ref:awesomeplotcaption)"}
x <- -5:5
y <- x^2

plot(x,y)
```

More explanatory text.

Using bookdown cross-referencing, have again a closer look at Fig. \@ref(fig:awesomeplot).

This results in the following:

bookdown-reference-in-elsevier-template

P.S. Focus on the cross-reference and ignore the code-chunk, this could be hidden with echo = FALSE. The figure follows below (in this example, placed via LATEX). I truncated it to keep the figure manageable :)

Ray
  • 2,008
  • 14
  • 21