Search code examples
rlatexr-markdownknitr

How to use Rmarkdown with natbib citation package?


I am trying to write a PDF article in Rmarkdown, and want to include a scientific bibliography using the latex natbib citation package.

Unfortunately, I get an error message.

This is a minimum example that contains the most important details of the Rmarkdown document:

---
output: 
  pdf_document:
     citation_package: natbib
bibliography: test_library.bib
---

@GoelThakor2008

The file test_library.bib contains just one article, for brevity:

@article{GoelThakor2008, 
year = {2008}, 
title = {{Overconfidence, CEO selection, and corporate governance}}, 
author = {Goel, Anand M and Thakor, Anjan V}, 
journal = {Journal of Finance}, 
pages = {2737 -- 2784}, 
volume = {63},  
month = {00}
}

After compiling, or "knitting", the Rmarkdown file into a PDF file using Rstudio, I get a long error message, part of which is this:

Warning: Package natbib Warning: Citation `GoelThakor2008' on page 1 undefined on input Warning: Package natbib Warning: There were undefined citations.

Interestingly, if I comment-out the line citation_package: natbib then it works like a charm.


Solution

  • After a lot of debugging, I figured out the answer: The filename test_library.bib has an underscore "_" that latex natbib cannot handle.

    Once I renamed the file to testlibrary.bib the file compiled correctly.