Duck
Duck

Reputation: 39603

Including citation and references when using appendix on RMarkdown

I am working on RMarkdown to generate a report that includes an appendix after references. I have written the appendix on a different RMarkdown file and adapted my principal file to compile it. This is the code for my principal Rmd file that generates report:

---
bibliography: bb.bib
fontsize: 11pt
nocite: '@*'
output: 
  pdf_document:
    includes:
      after_body: Demo2.Rmd
      keep_tex: yes
link-citations: true
---
\newpage

\section{Testing}\label{sec1}
```{r}
summary(cars)
```
\section{Demo}
This was done using @shiina and we will use some info from Section \ref{sec1} to do.
```{r}
summary(iris[,1:2])
```
\section{References} 

The file bb.bib contains next references:

@article {shiina,
author = {Shiina, Takayuki and Birge, John R.},
title = {Stochastic unit commitment problem},
journal = {International Transactions in Operational Research},
volume = {11},
number = {1},
publisher = {Blackwell Publishing},
pages = {19--32},
year = {2004},
}

@book{groewe2001,
  title={Stochastic unit commitment in hydro-thermal power production planning},
  author={Gr{\"o}we-Kuska, N. and R{\"o}misch, W.},
  year={2001},
  series = { Preprints aus dem Institut f{\"u}r Mathematik },
  publisher = { Humboldt-Universit{\"a}t zu Berlin, Institut f{\"u}r Mathematik },
}

Finally, my appendix Rmd file, Demo2.Rmd, contains this structure:

\appendix
\section*{Appendix}
\section{Additional info}
In this section we also follow @shiina to explain concepts.

Compilation works fine and generate document but issues are appearing in the appendix section. I used a reference with @shiina to cite something, but I am getting this output in the final report: enter image description here

The circle in black shows that citation from bibliography is not working. Instead of @shiina, it should appear Shiina and Birge (2004). I have tried replacing Rmd file with a TeX file but it did not work.

Is it any way to correct that?, I do not know if after_body needs to be adjusted or what to do.

Upvotes: 4

Views: 706

Answers (1)

Max Teflon
Max Teflon

Reputation: 1800

So, I did actually find a solution that does use some minor trickery.

---
bibliography: bb.bib
fontsize: 11pt
nocite: '@*'
output:
  pdf_document:
      keep_tex: true
      includes:
        after_body: Demo2.tex
link-citations: true
---

```{r,include=FALSE}
library(tidyverse)
rmarkdown::render('Demo2.Rmd')
a <- readChar('Demo2.tex', file.size('Demo2.tex'))
a <- a %>% str_remove('[[:space:]]*\\\\hypertarget[[\\w\\W]]+\\z') %>%
  str_remove('\\A[[\\w\\W]]+begin.document.')
writeChar(a, 'Demo2.tex',eos = NULL)
```


\newpage

\section{Testing}\label{sec1}
```{r}
summary(cars)
```

\section{Demo}
This was done using @shiina and we will use some info from Section \ref{sec1} to do.
```{r}
summary(iris[,1:2])
```
\section{References} 

And your Appendix-file:

---
bibliography: bb.bib
fontsize: 11pt
output:
  pdf_document:
      keep_tex: yes
link-citations: true
---


\appendix
\section*{Appendix}
\section{Additional info}
In this section we also follow @shiina to explain concepts.

# References

results in:

enter image description here


The way it works is to render the Demo2.Rmd- file before rendering the actual file and to keep the associated .tex- file. Then the non included R-chunk cuts of all the parts we don't want to have at the end of the main file and overwrites the Demo2.tex-file. What remains is the exact tex-code you need to have your references working.

Feels pretty dirty, but should work.

Upvotes: 1

Related Questions