I am writing a large document and recently had a weird bug crop up regarding the order of my references. I've now resolved it, but I'm not sure how it was able to occur, or why my solution even worked. I'd like to understand this so I can find other similar bugs if they exist, or at least be confident my references are ordered correctly without having to go through 200+ pages with a fine-toothed comb.

My document is in roughly this form:

\documentclass{book}
\usepackage{cleveref}
\usepackage{cite}
\usepackage[nottoc]{tocbibind}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\include{Chapter1}
\include{Chapter2}
\include{Chapter3}

\backmatter
\bibliographystyle{unsrt}
\bibliography{bibfile}

\end{document}


And the output looked something like this:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit.[1] Duis cursus dictum purus,[2] sit amet malesuada diam viverra vel.[3] Donec et velit sit amet lorem lobortis vulputate.[20] Aenean vel leo tempor,[4] fringilla nisl eget, mollis arcu.[5]

Bibliography

1. Dummy reference A, Journal of Foo Bar, vol 1, 1999.

2. Dummy reference B, Journal of Bar Foo, vol 12, 2000.

3. etc"

For some reason I was never able to determine, reference 20 was not numbered correctly. It was the 4th reference cited in the document (including in captions, TOC, etc), but it was assigned the number 20 instead of 4. Out of the references that I explicitly checked, there were no other similar issues. Compiling the document multiple times didn't correct the order. At some point I opened up the .bib file and moved the offending reference to a different random point in the .bib file and suddenly everything was fine. There is no difference in the code in the main document, and in theory the .bib file should give the same result regardless of the order of the references, yet this seemingly irrelevant change has made it go from not working to working. As a side note, at no point was there a problem compiling the document, and there were never any error messages, either before or after the fix.

Can anyone explain what happened here? Or how I might avoid similar issues in the future?

• You have not shown any relevant code to allow anyone to help! do you have any citations in table captions or section headings etc. Jul 26, 2018 at 7:13
• The .blg-file is the "log" from bibtex. Jul 26, 2018 at 7:33
• The .blg file gets re-written each time BibTeX is run.
– Mico
Jul 26, 2018 at 7:45
• @Ulysses latex itself never regenerates the bibliography, you need to re-run bibtex after any change, but of course your build system (make, latexmk, your editor build config or anything else you have not shown) may trigger bibtex to run under any conditions that may be programmed in to that system. Jul 26, 2018 at 8:21
• I've just dug through the program options and it appears a checkbox is ticked that means every time I compile the document it will first recompile the bibliography only if the .bib file was changed. Presumably if the .bib file isn't changed but I add a reference into the main document earlier than it was originally, then the bibliography won't update, or reorder references to match. This is likely the source of my error. If I can figure out how to manually run bibtex then I can ensure this issue doesn't reoccur. Jul 26, 2018 at 8:36

• Note that biblatex would warn you in cases like this and would ask for a re-run of Biber, so that the expected order can be restored. But of course then you need to read the warnings and act accordingly. Jul 26, 2018 at 10:52
• Yes, I am. But as far as I can see your are not using biblatex - you are using plain, ol' BibTeX (tex.stackexchange.com/q/25701/35864). I didn't say anything about BibTeX (or rather about LaTeX-side packages that work with it), because I wasn't sure whether or not they issue a warning. BTW: The warning would have to come from LaTeX (BibTeX could only say anything if it was run, but the whole point of the warning is to figure out whether or not to run BibTeX), so it would be in the .log not in the .blg. Jul 27, 2018 at 5:37
• As I said, you use BibTeX (which in the way you use it is incompatible with biblatex) and I was saying that biblatex would warn you. So I didn't expect you to get a warning here. It was just meant as a tiny shred of information for people with a similar problem. And of course it also doubles as a convenient ad for biblatex in case people think that is a nice feature. Jul 27, 2018 at 6:41