# Problem when compiling book with bookdown - natbib error

I am creating a pdf-book with bookdown using the following YAML:

``````---
site: bookdown::bookdown_site
geometry: "left=4cm,right=3cm,top=3cm,bottom=3cm"
subparagraph: true
output:
bookdown::pdf_book:
latex_engine: xelatex
fig_caption: yes
toc: false
citation_package: natbib
includes:
before_body: frontpage.tex
after_body: after_body.tex
fontsize: 11pt
linestretch: 1.2
documentclass: book
bibliography: [packages.bib,library.bib]
biblio-style: [plainnatnew.bst]
---
``````

In my preamble.tex i use `\setcitestyle{numbers,square,comma}` to specify citation type. I have created a new biblio-style to match my needs for the bibliography, where most of it is similar to the `plainnat.bst` style (i have specified to remove URLs from article entries, f. ex.). I cite in R like this: [@Reference1996].

I use Zotero to create the .bib file, with betterBibtex. The R-packages i use is found in the `packages.bib`, created by `knitr::write.bib()`.

Last week this worked perfectly, with no errors. However, suddenly I get a lot of this error:

``````Package natbib Warning: Citation X on page 9 undefined on input line 271
``````

Additionally, I get a bunch of these

``````Warning--empty publisher/year in X
``````

However, in the specific citation, both Year and publisher are present.

After seeing this error I tried to update all the R packages (including bookdown, markdown and knitr), reinstalled LaTeX with `reinstall_tinytex()`, re-created both `.bib` files, and tried compiling without the `plainnat.new` specification, but nothing seem to work. I have double-checked the `.bib` files, and they have all the entries that natbib says it cannot find. Also, I have deleted all files created by bookdown and re-ran everything from the start.

Does anyone know what may be the issue? As everything seemed to work fine before I do not understand what the issue may be... However, I did notice that this issue arose after i used `bookdown::preview_chapter()` on a single chapter of the book to Word-format, but this haven't changed any settings in the files as far as I am aware.

This is my `preamble.tex`:

``````\usepackage{titlesec}
\usepackage{pdfpages}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries}{\thechapter}{1em}{}  % set title format
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\AtBeginDocument{\let\maketitle\relax}
\usepackage{makeidx}
\makeindex
\setcitestyle{numbers,square,comma}
\usepackage{url}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\captionsetup[figure]{textfont={small,it}, labelfont={normalsize,bf,it}}  % set figure caption font size and style
\usepackage{graphicx}
\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
\expandafter\origfigure\expandafter[H]
} {
\endorigfigure
}
\raggedbottom
\usepackage{fancyhdr,blindtext}
\fancyhf{}
\setlength{\headheight}{27.7pt} % as requested by fancyhdr's warning
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\makeatletter
\renewcommand{\chaptermark}[1]{%    % change settings for including headers
\if@mainmatter
\markboth{Chapter \thechapter{}: #1}{}%
\else
\markboth{#1}{}%
\fi
}
\makeatother
\usepackage{epigraph}
\setcounter{secnumdepth}{1}
\setcounter{tocdepth}{1}
``````
• Have you checked that the cite of the text ( `@X` or `[@X]`) correspond really with one reference in the .bib file? that is, that really there are some reference as `@article{X, author={X... ` but not `@article{, author={X... ` nor `@article{whatever, author={X... ` ? – Fran Sep 23 '19 at 19:11
• @Fran The .bib file is correctly formatted. Since everything worked fine previously and suddenly stopped working i wondered what the cause may be... – Haakonkas Sep 24 '19 at 9:45
• Try to reduce your document and .bib removeing cites and references. At some point the error will be lost and you will know where it is. If the error persist even with only one cite and one reference and the mistake is not evident for you , post both the complete .Rmd file and the .bib file. – Fran Sep 26 '19 at 19:45

`Martines-Martines1998` and `Martines-martines1998`. Nice to know that case doesn't matter in the .bib file. I changed one of the entries, then the book compiled as before.
• To avoid undefined citations take a look to JabRef: It change automatically duplicated keys (even in this case) and also detect duplicated entries even if the keys do not match (or even if there are minor differences in entry fields). On the other hand, also can help with undefined references due to errors in the text citations, as you can copy easily the keys of the selected entry/entries with `Ctrl+Shift+K` and then paste in the editor without make mistakes. – Fran Sep 30 '19 at 23:29