0

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?

  • 1
    You have not shown any relevant code to allow anyone to help! do you have any citations in table captions or section headings etc. – David Carlisle Jul 26 '18 at 7:13
  • 1
    The .blg-file is the "log" from bibtex. – Ulrike Fischer Jul 26 '18 at 7:33
  • 1
    The .blg file gets re-written each time BibTeX is run. – Mico Jul 26 '18 at 7:45
  • 1
    @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. – David Carlisle Jul 26 '18 at 8:21
  • 1
    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. – Ulysses Jul 26 '18 at 8:36
2

Just to summarise the discussion in the comments:

The issue was found to be due to the way things were (or rather weren't) being compiled, rather than any issues with the code itself. Regardless of the editor used, the order of references will only be updated when the bibliography is compiled, which is separate to the compiling of the main document. So if the document is edited such that the 20th reference is now the 4th one to be cited, it will still appear in the text as [20] when the document is compiled, until such time as the bibliography is compiled again. The solution is to simply force the editor to compile the bibliography, then compile the main document in the usual manner. It may be necessary to repeat this a few times if some issues with ordering remain, for reasons detailed in the main answer here.

In TeXstudio the compiler for the bibliography can be run manually by going to Tools --> Bibliography, or pressing F8, then the main document will show the correct references next time it is compiled, i.e. pressing Build, or F5. TeXstudio can also be set up so that the bibliography will also be automatically compiled if the .bib file is edited, which is why I saw the issue resolved when I made an irrelevant change to the .bib file. Simply changing the order things are cited in the text will not trigger it to recompile.

| improve this answer | |
  • 1
    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. – moewe Jul 26 '18 at 10:52
  • @moewe Are you sure? I just tested this and I was able to reproduce my issue, but there were no warnings or error messages suggesting anything like that. I checked the .blg log file, and the log feed in the editor, but there was nothing. – Ulysses Jul 27 '18 at 5:28
  • 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. – moewe Jul 27 '18 at 5:37
  • @moewe My mistake, it is indeed Bibtex, not Biblatex. I didn't pick up on the difference. However I went through the log file and still did not find any warning or error suggesting Bibtex needed to be run, or that there was any problem with references even vaguely related to the above issue. – Ulysses Jul 27 '18 at 6: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. – moewe Jul 27 '18 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.