BibTeX, biblatex and biberChanging sort order with natbib author-year style

Information and discussion about BiBTeX - the bibliography tool for LaTeX documents.
Post Reply
mdenwood
Posts: 4
Joined: Thu Feb 04, 2010 12:25 pm

Changing sort order with natbib author-year style

Post by mdenwood »

Hi guys

I've (nearly) finished writing my thesis, using natbib and the author-year style for references and my own bst file (generated using custom bib). I was using the 'sort' option for natbib, which sorted multiple references in alphabetical order (the same as the bibliography) when using, for example, \citep{smith:92, blogs:95, dent:94} as follows:

... (Blogs et al, 1995; Dent et al, 1994; Smith et al, 1992).

I was happy with this, but my supervisor wants them in chronological rather than alphabetical order (without changing the order of the bibliography!) as follows:

... (Smith et al, 1992; Dent et al, 1994; Blogs et al, 1995).

I can obviously do this by removing the 'sort' flag to natbib and re-ordering all my references manually, but this seems somewhat laborious :roll: Is there a package or natbib option/reconfigure that will do it automatically? I've had a look through the help for natbib but can't find anything that looks like what I want...

Thanks in advance!

Matt

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

olofos
Posts: 56
Joined: Tue Nov 24, 2009 10:37 pm

Re: Changing sort order with natbib author-year style

Post by olofos »

I don't think there is an easy way to do this with natbib. The references are not sorted alphabetically, but in the order in which they appear in the bibliography. To change this you would probably need to redefine the macro \NAT@sort@cites in some clever and tricky way.
mdenwood
Posts: 4
Joined: Thu Feb 04, 2010 12:25 pm

Re: Changing sort order with natbib author-year style

Post by mdenwood »

Hmm, I feared as much. Plan B is to write a shell script to grep, reorder and replace the citep entries by cross-referencing against the bib file; I'll be damned if I'm doing it manually ;)

Thanks for the reply anyway,

Matt
mdenwood
Posts: 4
Joined: Thu Feb 04, 2010 12:25 pm

Changing sort order with natbib author-year style

Post by mdenwood »

In case anybody else encounters the same issue, the attached shell script re-orders the order that references are given in the tex source file. Removing the 'sort' option to natbib should then result in references that are sorted chronologically in the text but alphabetically in the bibliography. Some citation keys give it trouble but most are fine, and a backup is made of the tex file before starting.

Cheers,

Matt
biborder.sh.txt
(3.62 KiB) Downloaded 1369 times
mdenwood
Posts: 4
Joined: Thu Feb 04, 2010 12:25 pm

Re: Changing sort order with natbib author-year style

Post by mdenwood »

Update: Replacing the following line (number 79):

startline=`cat $bibfile | grep -n $reference | awk -F: '{print $1}'`

with:

startline=`cat $bibfile | grep -n @[a-z]*{$reference, | awk -F: '{print $1}'`

prevents problems with short/1 character cite keys and should work 100% of the time as long as the bibfile is in the correct format (for example as created by BibDesk).

For those that are unfamiliar with unix shell scripts, fire up a console/terminal and type:

chmod 755 biborder.sh.txt
biborder.sh.txt "<nameoftexfile>" "<nameofbibfile>" "<cite command to look for eg citep>"

Hope this is of use to someone....

Matt
JDash
Posts: 1
Joined: Fri Dec 25, 2020 4:49 pm

Changing sort order with natbib author-year style

Post by JDash »

Thank you so much mdenwood for providing the solutions to this. But does anyone know how I can do this on Overleaf?
Post Reply