# In-text citation sorted according to publication year with apalike bibliography style

I am using the natbib bibtex package with the [authoryear,compress] options and the bibliographystyle apalike. Is it possible to modify the natbib package options so that in-text citations are ordered according to the publication year?

The same question has already been asked here: Year-wise citation in text with apalike bibliography style

The accepted answer is only a workaround if your bibtex-keys in your \citep{} command are already in the correct order (you just avoid the sort&compress option), though. But if you want them ordered automatically according to the publication year (without manually providing the correct sorting in the \citep{} command, this answer does not solve the issue.

Is there a solution to this? The answer here (last paragraph) sounds a little bit as if this is not possible (?): https://tex.stackexchange.com/a/62411/112395

To give a worked example:

\documentclass[12pt,a4paper,hidelinks]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[authoryear,compress]{natbib}        %Calls bibliography commands + Cite with author-year style (Harvard) instead of the numeric style. Let's just use compress instead of sort&compress: Note, however, that this means that the ordering of the in-text references follows the order you specified in the text (i. e., no automatical sorting is carried out!)

\begin{document}

%

\section*{References}
\label{sec:References}
\citep{Owen1876,
Berman1977,
Berman1987,
Berman1992,
Berman1993,
Berman1993_Seymouria,
Berman2003_Aspidosaurus,
Berman2004,
Berman2010_Fedexia,
Berman2011_Rotaryus,
Berman2014,
Berman2015}

\citep{Berman1977,
Berman1987,
Berman1992,
Berman1993,
Berman1993_Seymouria,
Berman2003_Aspidosaurus,
Berman2004,
Berman2010_Fedexia,
Berman2011_Rotaryus,
Berman2014,
Berman2015,
Owen1876}

\bibliographystyle{apalike}
\bibliography{myrefs}

\end{document}


with the following myref.bib file:

@Book{Owen1876,
title     = {{Descriptive and illustrated catalogue of the Fossil Reptilia of South Africa in the collection of the British Museum}},
publisher = {British Museum (Natural History)},
year      = {1876},
author    = {Richard Owen},
note      = {88 pp.},
url       = {http://gallica.bnf.fr/ark:/12148/bpt6k987042},
}

@Article{Berman1977,
author    = {Berman, David S},
title     = {{A new species of \textit{Dimetrodon} (Reptilia, Pelycosauria) from a non-deltaic facies in the Lower Permian of north-central New Mexico}},
journal   = {Journal of Paleontology},
year      = {1977},
volume    = {51},
number    = {1},
pages     = {108--115},
}

@Article{Berman1987,
author    = {David S. Berman and Robert R. Reisz and David A. Eberth},
title     = {{\textit{Seymouria sanjuanensis} (Amphibia, Batrachosauria) from the Lower Permian Cutler Formation of north-central New {Mexico} and the occurrence of sexual dimorphism in that genus questioned}},
journal   = {Canadian Journal of Earth Sciences},
year      = {1987},
volume    = {24},
number    = {9},
pages     = {1769--1784},
}

@Article{Berman1992,
author    = {David S. Berman and Stuart S. Sumida and R. Eric Lombard},
title     = {{Reinterpretation of the temporal and occipital regions in \textit{Diadectes} and the relationships of diadectomorphs}},
journal   = {Journal of Paleontology},
year      = {1992},
volume    = {66},
number    = {3},
pages     = {481--499},
}

@InCollection{Berman1993,
author    = {Berman, D. S.},
title     = {{Lower Permian vertebrate localities of New Mexico and their assemblages}},
booktitle = {{Vertebrate Paleontology in New Mexico}},
publisher = {New Mexico Museum of Natural History},
year      = {1993},
editor    = {Spencer G. Lucas AND Jiri Zidek},
volume    = {2},
pages     = {11--21},
}

@Article{Berman1993_Seymouria,
author   = {Berman, David S. and Martens, Thomas},
title    = {{First occurrence of \textit{Seymouria} (Amphibia: Batrachosauria) in the Lower Permian Rotliegend of central Germany}},
journal  = {Annals of Carnegie Museum},
year     = {1993},
volume   = {62},
number   = {1},
pages    = {63--79},
}

@Article{Berman2003_Aspidosaurus,
author   = {Berman, David S. and Lucas, Spencer G.},
title    = {{\textit{Aspidosaurus binasser} (Amphibia, Temnospondyli), a new species of Dissorophidae from the Lower Permian of Texas}},
journal  = {Annals of Carnegie Museum},
year     = {2003},
volume   = {72},
number   = {4},
pages    = {241--262},
}

@Article{Berman2004,
author    = {Berman, David S and Henrici, Amy C and Sumida, Stuart S and Martens, Thomas},
title     = {{New materials of \textit{Dimetrodon teutonis} (Synapsida: Sphenacodontidae) from the Lower Permian of Germany}},
journal   = {Annals of Carnegie Museum},
year      = {2004},
volume    = {73},
number    = {2},
pages     = {48--56},
}

@Article{Berman2010_Fedexia,
author    = {David S Berman and Amy C. Henrici and David K. Brezinski and Albert D. Kollar},
title     = {{A new trematopid amphibian (Temnospondyli: Dissorophoidea) from the Upper Pennsylvanian of Western Pennsylvania: earliest record of terrestrial vertebrates responding to a warmer, drier climate}},
journal   = {Annals of Carnegie Museum},
year      = {2010},
volume    = {78},
number    = {4},
pages     = {289--318},
}

@Article{Berman2011_Rotaryus,
author    = {David S Berman and Amy C. Henrici and Thomas Martens and Stuart S. Sumida and Jason S. Anderson},
title     = {{\textit{Rotaryus gothae}, a new trematopid (Temnospondyli: Dissorophoidea) from the Lower Permian of central {Germany}}},
journal   = {Annals of Carnegie Museum},
year      = {2011},
volume    = {80},
number    = {1},
pages     = {49--65},
}

@InCollection{Berman2014,
author    = {Berman, David S and Henrici, Amy C. and Sumida, Stuart S. and Martens, Thomas and Pelletier, Valerie},
title     = {{First European Record of a Varanodontine (Synapsida: Varanopidae): Member of a Unique Early Permian Upland Paleoecosystem, Tambach Basin, Central Germany}},
booktitle = {{Early Evolutionary History of the Synapsida}},
publisher = {Springer},
year      = {2014},
editor    = {Kammerer, Christian F. and Angielczyk, Kenneth D. and Fr{\"o}bisch, J{\"o}rg},
series    = {Vertebrate Paleobiology and Paleoanthropology},
chapter   = {5},
pages     = {69--86},
}

@InCollection{Berman2015,
author    = {David S. Berman AND Amy C. Henrici AND Spencer G. Lucas},
title     = {{Pennsylvanian-Permian Red Bed vertebrate localities of New Mexico an their assemblages}},
booktitle = {{Fossil Vertebrates in New Mexico}},
publisher = {New Mexico Museum of Natural History},
year      = {2015},
editor    = {Lucas, S. G. AND Sullivan, R. M.},
volume    = {68},
pages     = {65--76},
}


This will produce the following in-text citation:

As you can see, in the second citation the references are not in chronological order: the one from 1876 appears last instead of appearing first. Is there a way to change this (without having to manually go through all the \cite commands)?

Preferably I would like to see a solution using bibtex, not biblatex.

• As far as I understand natbib's sort option sorts citations with multiple keys in the order they appear in the bibliography/list of references. So unless your bibliography is sorted by year first you can't easily get the order you want in the citations. (Since you mention biblatex, it would be possible there: tex.stackexchange.com/q/361031/35864, but can have very bad side-effects so that I would not recommend it even there.) – moewe Apr 11 '19 at 11:25
• Indeed, natbib's sort command sorts citations with multiple keys in the order they appear in the bibliography. In my case they are sorted alphabetically (well actually, it is a bit more complicated - I am using the custom BST you helped me with: tex.stackexchange.com/a/484079/112395; but I did not want to make my request more complicated/confusing so I omitted this). Is there no way to achieve the chronological ordering in bibtex using natbiband/or a modified version of natbib? You would think that other people faced the same problem before. – user6475 Apr 11 '19 at 11:58

As far as I am aware natbib's sort option sorts by the order in the bibliography. So the automatic sorting can only get you the order you would have in the bibliography.

Here is a proof of concept that tries to produce a different sorting in the citations than for the bibliography.

It works as follows: The .bst file produces a bibliography as always. But then afterwards it sorts all entries again in the desired citation order and writes the order information into the .bbl file. This order information is read, written to the .aux and finally results in a sorted list of all entry keys in the desired citation order.

We then modify the internal definition of the \cite command to sort all entries with our list of sorted keys. This is more or less how biblatex sorts its citations.

Hence, you will need a modified .bst file. For apalike.bst as in the example it is enough to add

FUNCTION {sort.again}
{
year field.or.null sortify
"   "
*
sort.label
*
'sort.key$:= } ITERATE {sort.again} SORT EXECUTE {newline$}

FUNCTION {produce.sortlist}
{
"\sortentry{"
cite$* "}" * write$
newline\$
}

ITERATE {produce.sortlist}


at the end of the file to obtain the sorting information. YMMV with different styles. Assume the new style is called apalike-citesort.bst. You can download it from https://gist.github.com/moewew/22e70d3c0c6df24859bf949a44bffb62.

The MWE shows the necessary redefinitions of the cite macro. I had the feeling that it should have been simpler to trick natbib into using the new citation list, but in the end I had to use a less pretty route.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[authoryear]{natbib}

\usepackage{etoolbox}

\makeatletter
\newcommand*{\sortentry}[1]{%
\if@filesw
\immediate\write\@auxout{\string\uNAT@aux@sortentry{#1}}%
\fi}
\newcommand*{\uNAT@aux@sortentry}{%
\newcommand*{\uNAT@bibsortlist}{}

\newcommand*{\uNAT@citekeys}{}

\newcommand*{\uNAT@writetocitelistsort}[1]{%
\ifinlist{#1}{\uNAT@citekeys}
{\ifdefvoid{\NAT@cite@list}
{\def\NAT@cite@list{#1}}
{\expandafter\def\expandafter\NAT@cite@list\expandafter{\NAT@cite@list,#1}}%
{}}

% Let's be paranoid: we might have dropped an item that
% has not made it to the bibliography yet
\newcommand*{\uNAT@writetocitelistforgotten}[1]{%
\ifinlist{#1}{\uNAT@foundkeys}
{}
{\ifdefvoid{\NAT@cite@list}
{\def\NAT@cite@list{#1}}
{\expandafter\def\expandafter\NAT@cite@list\expandafter{\NAT@cite@list,#1}}}}

\newcommand*{\uNAT@sortcites}[1]{%
\let\NAT@cite@list\@empty
\let\uNAT@citekeys\@empty
\let\uNAT@foundkeys\@empty
\forlistloop{\uNAT@writetocitelistsort}{\uNAT@bibsortlist}%
\forlistloop{\uNAT@writetocitelistforgotten}{\uNAT@citekeys}%
}

\def\NAT@citex%
[#1][#2]#3{%
\NAT@reset@parser
\NAT@sort@cites{#3}%
\uNAT@sortcites{#3}%<- this is new
\NAT@reset@citea
\@cite{\let\NAT@nm\@empty\let\NAT@year\@empty
\@for\@citeb:=\NAT@cite@list\do
{\@safe@activestrue
\edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
\@safe@activesfalse
\@ifundefined{b@\@citeb\@extra@b@citeb}{\@citea%
{\reset@font\bfseries ?}\NAT@citeundefined
\PackageWarning{natbib}%
{Citation \@citeb' on page \thepage \space undefined}\def\NAT@date{}}%
{\let\NAT@last@nm=\NAT@nm\let\NAT@last@yr=\NAT@year
\NAT@parse{\@citeb}%
\ifNAT@longnames\@ifundefined{bv@\@citeb\@extra@b@citeb}{%
\let\NAT@name=\NAT@all@names
\global\@namedef{bv@\@citeb\@extra@b@citeb}{}}{}%
\fi
\ifNAT@full\let\NAT@nm\NAT@all@names\else
\let\NAT@nm\NAT@name\fi
\ifNAT@swa\ifcase\NAT@ctype
\if\relax\NAT@date\relax
\@citea\NAT@hyper@{\NAT@nmfmt{\NAT@nm}\NAT@date}%
\else
\ifx\NAT@last@nm\NAT@nm\NAT@yrsep
\ifx\NAT@last@yr\NAT@year
\def\NAT@temp{{?}}%
\ifx\NAT@temp\NAT@exlab\PackageWarningNoLine{natbib}%
{Multiple citation on page \thepage: same authors and
year\MessageBreak without distinguishing extra
letter,\MessageBreak appears as question mark}\fi
\NAT@hyper@{\NAT@exlab}%
\else\unskip\NAT@spacechar
\NAT@hyper@{\NAT@date}%
\fi
\else
\@citea\NAT@hyper@{%
\NAT@nmfmt{\NAT@nm}%
\NAT@aysep\NAT@spacechar}{\@citeb\@extra@b@citeb
}%
\NAT@date
}%
\fi
\fi
\or\@citea\NAT@hyper@{\NAT@nmfmt{\NAT@nm}}%
\or\@citea\NAT@hyper@{\NAT@date}%
\or\@citea\NAT@hyper@{\NAT@alias}%
\fi \NAT@def@citea
\else
\ifcase\NAT@ctype
\if\relax\NAT@date\relax
\@citea\NAT@hyper@{\NAT@nmfmt{\NAT@nm}}%
\else
\ifx\NAT@last@nm\NAT@nm\NAT@yrsep
\ifx\NAT@last@yr\NAT@year
\def\NAT@temp{{?}}%
\ifx\NAT@temp\NAT@exlab\PackageWarningNoLine{natbib}%
{Multiple citation on page \thepage: same authors and
year\MessageBreak without distinguishing extra
letter,\MessageBreak appears as question mark}\fi
\NAT@hyper@{\NAT@exlab}%
\else
\unskip\NAT@spacechar
\NAT@hyper@{\NAT@date}%
\fi
\else
\@citea\NAT@hyper@{%
\NAT@nmfmt{\NAT@nm}%
{\@citeb\@extra@b@citeb}%
\NAT@date
}%
\fi
\fi
\or\@citea\NAT@hyper@{\NAT@nmfmt{\NAT@nm}}%
\or\@citea\NAT@hyper@{\NAT@date}%
\or\@citea\NAT@hyper@{\NAT@alias}%
\fi
\if\relax\NAT@date\relax
\NAT@def@citea
\else
\NAT@def@citea@close
\fi
\fi
}}\ifNAT@swa\else\if*#2*\else\NAT@cmt#2\fi
\if\relax\NAT@date\relax\else\NAT@@close\fi\fi}{#1}{#2}}
\makeatother

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{Owen1876,
title     = {{Descriptive and illustrated catalogue of the Fossil Reptilia of South Africa in the collection of the British Museum}},
publisher = {British Museum (Natural History)},
year      = {1876},
author    = {Richard Owen},
note      = {88 pp.},
url       = {http://gallica.bnf.fr/ark:/12148/bpt6k987042},
}
@article{Berman1977,
author    = {Berman, David S},
title     = {{A new species of \textit{Dimetrodon} (Reptilia, Pelycosauria) from a non-deltaic facies in the Lower Permian of north-central New Mexico}},
journal   = {Journal of Paleontology},
year      = {1977},
volume    = {51},
number    = {1},
pages     = {108--115},
}
@article{Berman1993_Seymouria,
author   = {Berman, David S. and Martens, Thomas},
title    = {{First occurrence of \textit{Seymouria} (Amphibia: Batrachosauria) in the Lower Permian Rotliegend of central Germany}},
journal  = {Annals of Carnegie Museum},
year     = {1993},
volume   = {62},
number   = {1},
pages    = {63--79},
}
@incollection{Berman2015,
author    = {David S. Berman AND Amy C. Henrici AND Spencer G. Lucas},
title     = {{Pennsylvanian-Permian Red Bed vertebrate localities of New Mexico an their assemblages}},
booktitle = {{Fossil Vertebrates in New Mexico}},
publisher = {New Mexico Museum of Natural History},
year      = {2015},
editor    = {Lucas, S. G. AND Sullivan, R. M.},
volume    = {68},
pages     = {65--76},
}
\end{filecontents}

\begin{document}
\citep{Owen1876,
Berman1977,
Berman1993_Seymouria,
Berman2015}

\citep{Berman1977,
Berman1993_Seymouria,
Berman2015,
Owen1876}

\bibliographystyle{apalike-citesort}
\bibliography{\jobname}
\end{document}


• Thank you very much! I can confirm that this works for my MWE - it even works, when using other \cite commands like \citet or \citealp. Unfortunately, for my actual document it does not work. Initially I thought that this was because I already tried to define a new cite command like so: %Cite with 's \newcommand\cites[1]{\citeauthor{#1}'s\ (\citeyear{#1})} This gives an error, if the new command generated by your proposal is not inserted after my new command. Continue below. – user6475 Apr 11 '19 at 13:56
• But even when the command order is reversed, the complete PDF generation is not possible - or rather it is possible only to a certain extent. I can compile the document + run bibtex + get a PDF, but my Latex editor (Texmaker) will hang when I try to get the references in the bibliography linked to the in-text references (which therefore appear as "?"). I will have to figure out, why that is the case. Since this is not directly linked to my question, I will just wait some time to see whether someone else comes up with a different solution. If not, I will accept your answer. Thanks a lot! – user6475 Apr 11 '19 at 13:59
• @user6475 Mhhh, in a short test based on my MWE I can get you \cites to work more or less as I expected. Can you upload an MWE to pastebin.com or a similar text-sharing website that reproduces the issue? – moewe Apr 11 '19 at 14:02
• Thank you for your continuous help! I will try and see, whether I can generate an appropriate MWE. Unfortunately, the original document is about 500 pages long (with hundreds of references) and quite complicated with various chapters that are coerced to a single document by a master file. Furthermore, it also contains confidential information which I am currently not allowed to share (otherwise I could just upload everything). So it might take a while for me to figure out, what is actually causing the issue here. As you say, it is almost certainly not the \cites` command. – user6475 Apr 11 '19 at 14:10