Bibliography entries occasionally containing an extra comma

Occasionally, I am seeing extra commas being inserted in bibliography entries using natbib and the unsrtnat style. Why does this happen, and how can I fix this?

Here is a minimal example:

\begin{filecontents*}{bibliography.bib}
@misc{bib1,
title="{Title One}",
url="https://dummyurl"
}
@misc{bib2,
title="{Title Two}",
url="https://dummyurl"
}
\end{filecontents*}

\documentclass[]{article}
\usepackage[numbers]{natbib}
\begin{document}

This is a citation.~\cite{bib1}

%%This is another citation.~\cite{bib2} %% <--- if this line is uncommented, both bib entries will have an extra comma after the title

\bibliographystyle{unsrtnat}
\bibliography{bibliography}

\end{document}


Here is a screenshot of what I see: (left shows the output with the line in the MWE commented, right when it is uncommented)

• Nice first question and good MWE! This happens because there is no author or date and natbib makes an attempt to disambiguate the date with a and b. Look in the bbl file to see what is going on. Not sure how to fix though! – David Purton Sep 11 at 14:47
• I think this is a duplicate of tex.stackexchange.com/q/396893/87678, but that question doesn't have a satisfactory answer. – David Purton Sep 11 at 15:04
• If you don't load numbers you can see what it is trying to do, but I've dug into the bst and bbl files and it's beyond me. – Danny Garside Sep 11 at 17:51
• @DavidPurton, @DannyGarside, thanks for pointing me in the right direction. After testing various things and checking the resulting .bbl file, I've managed to find the cause of this behaviour (see my answer below). I'm still unsure as to how one would fix this at the level of .bst files, but at least the problem is not too difficult to avoid! – hiccups Sep 12 at 7:20

This behaviour stems from the way unsrtnat distinguishes between different entries in the bibliography. The following is probably not complete, but is as far as I understand after making some changes to the MWE and viewing the resulting .bbl file.

1. If authors are specified, e.g. "FirstNameA LastNameA" for one entry, and "FirstNameB LastNameB" for another, then their respective tags are LastNameA() and LastNameB()
2. If no last name is specified, the first names are used instead: FirstNameA(), FirstNameB()
3. If the date is specified, this is also used. This differentiates between entries with the same author, e.g. LastNameA(2019) and LastNameA(1999)
4. In the case where multiple entries have the same author and date, an invisible letter is added to the date in the tag, but which does not appear in the final compiled document, e.g. LastNameA(2019{\natexlab{a}}), LastNameA(2019{\natexlab{b}}), and so on
5. If neither authors nor dates are specified, the first three characters of the citation label will be used to identify an entry: e.g. bib(), in the case of the MWE in the question.

Now, in the case of 5., when there are two (or more) entries in the bibliography whose labels begin with the same three characters, the same procedure of 4. is applied: their tags are then bib({\natexlab{a}}) and bib({\natexlab{b}}).

This is where the error arises: even though no date has been specified for these entries, the invisible {\natexlab{a}} and {\natexlab{b}} generated by unsrtnat are still present, and consequently interpreted as a date. This leads natbib to believe that the date is specified, but of course, nothing is printed in this field in the final PDF.

To avoid this issue, one can specify an author(s) and/or date(s), as David Purton suggested in his comment. Otherwise, it is sufficient to ensure that the first three characters in the labels of the citations are different, e.g. instead of defining bib1 and bib2 in the MWE above, one could define 1bib and 2bib.