# Chapterbib gives citation undefined and [?] when also using hyperref package, how to solve?

I am using the packages hyperref and chapterbib. The citations kept showing as [?], even though I could find the correct keys back in the aux and bbl files. After excluding packages one-by-one I found that when I remove \usepackage{hyperref} from my packages, I do get the correct reference.

Is there a way to use both of these packages at the same time? Am I missing something in the options of the package that I should add?

Working example:

In main directory, main.tex

\documentclass[10pt]{book}
% comment out \usepackage{hyperref} to get [1] instead of [?]
\usepackage{hyperref}
\usepackage{chapterbib}
\begin{document}
\include{chapters/chapter1}
\end{document}


in chapters/ directory, chapter1.tex:

\chapter[Example]{Test}
test: \cite{albertRoleRegulatoryVariation2015}
\bibliographystyle{naturemag}
\bibliography{chapters/chapter1}


in chapters/ directory, chapter1.bib:

@article{albertRoleRegulatoryVariation2015,
title = {The Role of Regulatory Variation in Complex Traits and Disease},
author = {Albert, Frank W. and Kruglyak, Leonid},
year = {2015},
month = apr,
volume = {16},
pages = {197--212},
issn = {1471-0064},
doi = {10.1038/nrg3891},
journal = {Nature Reviews Genetics},
}


Build with pdflatex main.tex; bibtex chapters/chapter1; pdflatex main.tex; pdflatex main.tex

• Have you tried to run bibtex inside the chapters folder? (just as a test) Feb 20 '20 at 14:27
• After your comment I have tried this, but it gives the same result ([?] when including hyperref, [1] when removing hyperref)
– Niek
Feb 20 '20 at 14:46
• Sigh, first rule of hyperref: hyperref should be loaded last, Second rule of hyperref there are a few exceptions to the first rule, chapterbib is not one of them. Compiles just fine if I change the order of chapterbib and hyperref Feb 20 '20 at 15:08
• I was not aware of this rule. Switching hyperref to the bottom indeed fixes the problem. Can you put this as answer so that I can accept?
– Niek
Feb 20 '20 at 15:58

With very few exceptions the hyperref package has to be the last of the packages loaded (as it needs to patch several packages)

In this case loading hyperref after chapterbib solves the problem