Cross-referencing in rticles - r

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?

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

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

#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:
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 :)

Related

R Bookdown: Append chapter without re-rending whole book

I am rather experienced with R but new to markdown and especially bookdown. I wonder how to render a single chapter and merge it with the existing book without re-building the whole book. The reason why I am asking is that I am creating a book that contains parameterised chapters and each chapter runs very long (1-2 hours). All in all, it will be hundreds of chapters with the same analysis, but different input data and would time-wise add up. I am aware of the knit-and-merge approach, in fact, that's the reason why I chose bookdown, but somehow it is not working as expected.
I am honestly also a little bit confused by the different rendering possibilities. There is the built-book button, the knit button, serve_book(), render_book(), preview_chapter(), ... I figured that one should avoid using the knit button in bookdown and that serve_book() will automatically compile changes. My approach so far is entering serve_book() into console, where it renders the whole book, then I create a new chapter, hit save and it runs the chunks of only that chapter (exactly what I want!) and the single HTML file of the new chapter appears. However, it doesn't show them when I look at the book via the index.html file.
I am only interested in an HTML book and do not need any other output formats.
In order to recreate the problem, I am just using the bookdown example and adding chapters to it.
_bookdown.yaml
book_filename: "book_name"
new_session: yes #true
before_chapter_script: _common.R
delete_merged_file: true
language:
ui:
chapter_name: "Chapter "
_output.html
bookdown::bs4_book:
css: style.css
theme:
primary: "#10395B"
#repo: https://github.com/rstudio/bookdown-demo
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
bookdown::epub_book: default
_index.yaml
---
title: |
![](./Logo_PureGene.png){width=200px}|
|-|
||
author: "Name"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
#output: bookdown::bs4_book
documentclass: book
bibliography: [book.bib, packages.bib]
# url: your book url like https://bookdown.org/yihui/bookdown
# cover-image: path to the social sharing image like images/cover.jpg
description: |
This is a minimal example of using the bookdown package to write a book.
The HTML output format for this example is bookdown::bs4_book,
set in the _output.yml file.
biblio-style: apalike
csl: chicago-fullnote-bibliography.csl
---
# About {-}
I would appreciate any help!
Thanks in advance :)

Using Unicode characters in math mode with Bookdown (xelatex)

I try to use a Unicode character (U+2685) in math mode with Bookdown, I set mathfont: STIX Two Math (which contains this character), yet the resulting PDF contains an empty space. (At the same time the HTML is correct.)
What's wrong here?
(My best guess is that I should perhaps use the unicode-math package. Unfortunately I can't include it in the preamble.tex as it is incompatible with the mathspec, but at the same time I see no way to get rid of mathspec; it seems to be hardcoded in Bookdown that mathspec is usepackaged when xelatex is used.)
Minimal reproducible example (showing index.Rmd, all other file is the same as with the default new project created with RStudio):
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
link-citations: yes
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
mainfont: STIX Two Math
mathfont: STIX Two Math
---
# Prerequisites
In text: ⚀.
In math mode: $⚀$.
This is how the result looks like:
This issue is now solved (after an update to rmarkdown), see here.

Insert r inline code in abstract with rticles journal template

I am using a r-markdown rticles journal template to prepare a manuscript. Almost all rticles templates place the abstract in the YAML header section. I am not able to insert inline r code to the abstract in YAML. I could move the abstract to the text section to insert inline r code. However, the final format would also change. In biomedical field, abstracts often include some numbers from data. What is the best way to add r inline code to the abstract with a rticles journal template? Thanks
Edit: I have just realized that r code for analysis can be placed before the YAML header section. This will solve my problem. Here is a shorten version using Elsevier Journal template:
```{r, echo=FALSE, include=FALSE}
n <- length(mtcars$mpg)
```
---
title: Short Paper
author:
- name: Alice Anonymous
email: alice#example.com
affiliation: Some Institute of Technology
footnote: Corresponding Author
address:
- code: Some Institute of Technology
address: Department, Street, City, State, Zip
abstract: |
There are `r n` observations.
journal: "An awesome journal"
date: "`r Sys.Date()`"
bibliography: mybibfile.bib
csl: elsevier-harvard.csl
output: rticles::elsevier_article
---
However, the journal template in the YAML header is no longer working. Now, I am back to where I started.

R markdown / bookdown - how to switch to rticles?

Input data
I prepared an example Rmd file with references to figure, table and equation, setting as an output 'bookdown::pdf_document2'. It compiles without errors to PDF.
I placed it on dropbox:
https://www.dropbox.com/sh/zmu0a4wq95ywssv/AAD-nHlkDiLknLk2NVR4Xup3a?dl=0
Question
Now I wish to set as an output format 'rticles::elsevier_article'
How can I do that?
Issue
When I change output line from:
bookdown::pdf_document2
to
rticles::elsevier_article
I'm receiving an error message.
Even if I remove other parameters from output:
I still receive an error message:
! Undefined control sequence.
Accented characters when input "as is" do not appear to behave well with elsevier_article. See suggestions below.
Bare-bones document
Here is a bare-bones document using rticles::elsevier_article:
---
title: "Sample document"
author:
- name: "Mateusz Kędzior"
affiliation: Some Institute of Technology
email: Mateusz#example.com
footnote: Corresponding Author
- name: Żąćł Źęń
csl: https://www.zotero.org/styles/geoderma
output:
rticles::elsevier_article:
citation_package: natbib
keep_tex: yes
number_sections: yes
toc: no
keywords: keywordA, keywordB
abstract: This is a sample abstract \newline This is the second line of abstract.
---
Hello world.
which renders with no complaints:
Reference with accents
Now, we wish to add a reference with accents. We follow the answer here: https://tex.stackexchange.com/questions/57743/how-to-write-%C3%A4-and-other-umlauts-and-accented-letters-in-bibliography. I imported your bibliography into Zotero, and then exported the item with a "Central European (ISO)" encoding (not UTF-8) to obtain
#article{kedzior_this_2018,
title = {This is sample title only {\k A} {\L }},
volume = {99},
url = {http://megooglethat.com/},
journal = {Some journal},
author = {K{\k e}dzior, Mateusz and {\'Z}{\k e}{\'n}, {\.Z}{\k a}{\'c}{\l }},
year = {2018},
keywords = {keywordC},
pages = {21 -- 31}
}
The R Markdown document now becomes
---
title: "Sample document"
author:
- name: "Mateusz Kędzior"
affiliation: Some Institute of Technology
email: Mateusz#example.com
footnote: Corresponding Author
- name: Żąćł Źęń
csl: https://www.zotero.org/styles/geoderma
output:
rticles::elsevier_article:
citation_package: natbib
keep_tex: yes
number_sections: yes
toc: no
biblio-files: bibliography2.bib
keywords: keywordA, keywordB
abstract: This is a sample abstract \newline This is the second line of abstract.
---
## Citations and references
Let me cite an article: [#kedzior_this_2018]
# References
I then knited this in RStudio, but realised that I had to get the tex output and rebuild it (outside of RStudio) to get the desired output
Other problems
For accented characters in figure captions, encode them accordingly (as with the bibliography). You may find http://w2.syronex.com/jmr/latex-symbols-converter helpful. In addition, to the best of my knowledge bookdown style cross-referencing does not work with rticles. If you have follow-up questions, you may get more helpful answers if you break your question down into smaller chunks.
I've added a bit of updated material to the commenthttps://github.com/rstudio/rticles/issues/92#issuecomment-402784283 where it states (may be updated):
output:
bookdown::pdf_document2:
base_format: rticles::elsevier_article
number_sections: yes
such that the way I've had this work is using pdf_book versus pdf_document2:
output:
bookdown::pdf_book:
base_format: rticles::elsevier_article
number_sections: yes
This allows for figure and table referencing within an rticles document.

How to add a latex package to R Journal template from rticle package in R

I am using rticle package by RStudio to write an article in R using rmarkdown package.
In the draft template there is an option
...
preamble: >
% Any extra latex you need in the preamble
output: rticles::rjournal_article
...
Where I suspect I can add lines like \usepackage{package_name} or set a file name in which I load all desired Latex packages.
The first approach
I've tried to add one single package to preamble section like this but have stacked on an error
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
\usepackage{rotating}
% Any extra latex you need in the preamble
output: rticles::rjournal_article
---
\begin{sidewaystable}
\centering
\caption{Your caption here}
\begin{tabular}{ll}
First First & First Second\\
Second First & Second Second
\end{tabular}
\end{sidewaystable}
The first approach code end
BŁĄD: running 'texi2dvi' on 'RJwrapper.tex' failed
LaTeX errors:
D:\RTCGA.data\Untitled\Untitled.tex:11: LaTeX Error: Environment sidewaystable
undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
D:\RTCGA.data\Untitled\Untitled.tex:13: Package caption Error: \caption outside
float.
See the caption package documentation for explanation.
Type H <return> for immediate help
D:\RTCGA.data\Untitled\Untitled.tex:18: LaTeX Error: \begin{article} on input l
ine 20 ended by \end{sidewaystable}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
Dodatkowo: Warning message:
uruchomione polecenie '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "RJwrapper.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/bibtex/bst"' otrzymało status 1
Wykonywanie wstrzymane
I've seen the same preamble - ignoring behavior in this issue https://github.com/rstudio/rticles/issues/11, but there was no response for that.
I've also tried to specify file named header.tex that contains a line \usepackage{rotating} in a last argument of a yaml like this but also there happend to be an error.
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
\usepackage{rotating}
output:
rticles::rjournal_article:
includes:
in_header: header.tex
---
BŁĄD: running 'texi2dvi' on 'RJwrapper.tex' failed
LaTeX errors:
D:\RTCGA.data\rticle\rticle.tex:145: LaTeX Error: Environment sidewaystable und
efined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
D:\RTCGA.data\rticle\rticle.tex:147: Package caption Error: \caption outside fl
oat.
See the caption package documentation for explanation.
Type H <return> for immediate help
D:\RTCGA.data\rticle\rticle.tex:192: LaTeX Error: \begin{article} on input line
20 ended by \end{sidewaystable}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
Dodatkowo: Warning message:
uruchomione polecenie '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "RJwrapper.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/bibtex/bst"' otrzymało status 1
Wykonywanie wstrzymane
EDIT
When I specify yaml in a way it is intended to work for pdf_document output
...
preamble: >
\usepackage{rotating}
output:
rticles::rjournal_article:
includes:
in_header: header.tex
...
I receive an error that
Error in (function () :
unused (includes = list(in_header = "header.tex"))
EDIT 2
Even for an empty new document I get the same error
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
% Any extra latex you need in the preamble
output:
rticles::rjournal_article:
includes:
in_header: header.tex
---
Hello World
The header.tex document (which is in the same directory as saved empty new file) contains only 1 line:
\usepackage{rotating}
I am aware of in_header option in regular pdf_document output format (see even my files with RMD extensions on github) but it looks like to does not work with rticles::rjournal_article
EDIT 3
I am interested in adding other latex package than those that are included automatically in the RJwrappter.tex file which is generated after render of the .RMD file.
\documentclass[a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{RJournal}
\usepackage{amsmath,amssymb,array}
\usepackage{booktabs}
%% load any required packages here
\begin{document}
%% do not edit, for illustration only
\sectionhead{Contributed research article}
\volume{XX}
\volnumber{YY}
\year{20ZZ}
\month{AAAA}
\begin{article}
\input{rticle}
\end{article}
\end{document}
Anyone maybe knows how to solve this?
I am using MiKTeX 2.9.
I know this question's old but I just had the same problem as Marcin and wanted to share my solution.
What worked for me was changing the template.tex file in the rticles package for the ASA article. At the top of the template, where they have all the other \usepackage commands, just add your package(s). I had the same problem that you did and neither of the typical solutions as provided in the first answer worked for me.
To edit the template file, go to the rticles directory in your package library folder (.libPaths() in R), then open up rmarkdown/templates/rjournal_article/resources/RJWrapper.tex. Add your package amongst all the others. This should work for any of the other *_article/resources/template.tex files.
No, that is wrong. In the YAML header declare that you want an include.
See the rmarkdown reference:
---
title: "Habits"
output:
pdf_document:
includes:
in_header: header.tex
before_body: doc_prefix.tex
after_body: doc_suffix.tex
---
I have used header.tex for this purpose in several documents.
Edit: As you appear to get lost in the woods, here is a simplyfied example started from the default useR abstract template:
---
title: "Title of Your Submission"
author:
- name: FirstNameA LastNameA
keywords: First, Second, ... up to 5 keywords
output:
pdf_document:
includes:
in_header: marcin_header.tex
---
Content to be added here.
which I coupled with the simplest possible header called marcin_header.tex (placed in the same directory) and containing just
\usepackage{booktabs}
Works for me.
Edit 2: Oh boy.
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
% Any extra latex you need in the preamble
\usepackage{booktabs}
output: rticles::rjournal_article
---
## Introduction
Rest omitted.
As dash2 mentioned you need to edit one of your rticles templates to take advantage of the header-includes or preamble clause. For example for the rticles::aea_article format the template could be in:
/home/user/R/x86_64-pc-linux-gnu-library/3.3/rticles/rmarkdown/templates/aea_article/resources/template.tex
This is for a linux system, but there's a location on your computer, whatever the OS, where R libraries are stored and you need to edit this file to include the snippet that dash2 provided.
There is no easy way to do this, as of Oct 16. What you need to do is find the relevant template and edit it to include the lines:
$for(header-includes)$
$header-includes$
$endfor$
You can find the template by looking at the source code for e.g. rjournal_article.
I managed to include the float package by adding
header-includes:
- \usepackage{float}
into my yaml-header at the beginning of the frontiers-template

Resources