36

I'm using the knitr package and pandoc in R to convert a .Rmd file to a PDF. Pandoc is linked to a .bib file and automatically inserts the bibliography at the end of the PDF The entries in my .bib file look like these, taken from http://johnmacfarlane.net/pandoc/demo/biblio.bib:

@Book{item1,
        author="John Doe",
        title="First Book",
        year="2005",
        address="Cambridge",
        publisher="Cambridge University Press"
  }

@Article{item2,
         author="John Doe",
         title="Article",
         year="2006",
         journal="Journal of Generic Studies",
         volume="6",
         pages="33-34"
}

To build my bibliography, I'm using the following function, taken from: http://quantifyingmemory.blogspot.co.il/2013/02/reproducible-research-with-r-knitr.html

knitsPDF <- function(name) {
  library(knitr)
  knit(paste0(name, ".Rmd"), encoding = "utf-8")
  system(paste0("pandoc -o ", name, ".pdf ", name, ".md --bibliography /Users/.../Desktop/test.bib --csl /Users/.../Desktop/taylor-and-francis-harvard-x.csl"))
}

The contents of my .Rmd file is:

This is some text [@item1]

This is more text [@item2]

# References

And outputted PDF looks like this:

enter image description here

If I try to insert an appendix, the references still print at the end of the document, like this:

enter image description here

How do insert an appendix after the references?

3 Answers 3

58

With newer pandoc versions, you can specify the bibliography's position with <div id="refs"></div> source

This is some text [@item1]

This is more text [@item2]

# References

<div id="refs"></div>

# appendix
Sign up to request clarification or add additional context in comments.

This does not seem to work for me. The output format of my Rmarkdown document is bookdown::pdf_document2.
This should become the accepted answer as it is perfectly straight-forward, I think.
Agree that this is a great answer, and my preference.
It is wonderful when it works. It has worked for me in the past but not right now, possibly because I am using citation_package: natbib.
17

Eventually reference handling will change to make it possible to put the references wherever you like (https://github.com/jgm/pandoc/issues/771), but right now there's no easy way to do it.

As suggested here, you could put your appendix in a separate file, use pandoc to convert it to a LaTeX fragment, then include that fragment using the --include-after-body flag. It would then come after the bibliography.

Whew glad that's it. I suspected Yihui had put a back end into knitr and was using it to steal people's data.
But, as NOON SILK says in the link you cited, "This doesn't work if the Appendix cites a reference." :-(
This was now implemented, cf. pandoc.org/MANUAL.html#placement-of-the-bibliography You can also refer to stackoverflow.com/a/58226766/2657549 for an example.
6

When working in an Rmarkdown document, enter the following text where the citations are to be located. It can be placed in any part of the document allowing other materials, like an appendix, to follow as necessary. The method relies on pandoc's fenced divs which will work in Rmarkdown.

::: {#refs}
:::

The aforementioned code should not be in an R code chunk, rather it should be placed on blank lines by themselves. Once processed by pandoc via knitter, this code will produce the same result as <div id="refs"></div> mentioned in the answer by @soca. The two lines of code do consistently allow for exact placement of the references in any section of the document.

In the example below, references are placed first under a heading of the same name while all of the code chunks in the document are placed afterwards in a code appendix. Here is the pandoc fenced div placed in Rmarkdown that can be used to generate the image that follows.

# References
::: {#refs}
:::

# Appendix A: R Code
```{r ref.label=knitr::all_labels(), echo=TRUE, eval=FALSE}

```

Provided there is a .bib file identified in the yaml frontmatter, the preceding Rmarkdown produces output similar to the following: enter image description here

Helpful links:

This is excellent advise, just one nitpick: the solution uses pandoc's fenced divs, it has nothing to do with R Markdown.
I edited the answer with the aim of properly referencing the use of pandoc's fenced divs. Hopefully I have it right.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.