ulyngs / oxforddown

Template for writing an Oxford University thesis in R Markdown; uses the OxThesis LaTeX template and was inspired by thesisdown.
https://ulyngs.github.io/oxforddown/
MIT License
221 stars 81 forks source link

Sorting references in alphabetical order using natbib #25

Closed zavud closed 3 years ago

zavud commented 3 years ago

I want to use natbib for my references, and therefore I set use-biblatex: false and use-natbib: true in the YAML and change nothing else. When I knit the document, the references are ordered (in the references section) not in an alphabetical order, but some strange/unpredictable order. How I can solve this?

ulyngs commented 3 years ago

my guess is that they're presented in the order in which they appear in the text - to change to alphabetical order, google for how to set this as an option for natbib, then try to adjust accordingly in template.tex at this location

\usepackage{natbib}
\setcitestyle{$natbib-citation-style$}
\bibliographystyle{$natbib-bibliography-style$}
ulyngs commented 3 years ago

Ok, I just looked at this properly.

Turns out I already designed the template to make this easy to set directly from index.Rmd's YAML header. Just use e.g.

use-natbib: true
natbib-citation-style: authoryear
natbib-bibliography-style: plainnat

(remember to set use-biblatex: false)

see https://tex.stackexchange.com/questions/425837/alphabetical-sorting-of-the-bibliography-using-natbib-and-unsrtnat

zavud commented 3 years ago

@ulyngs I have seen this post (tex stackexchange) and tried the trick before and nothing helped. I have tried other things as well, still no progress.

Regarding your assumption on how the references are ordered: they are not presented in the order they appear in the text. After analyzing it a bit, I figured out how they are presented: they are actually presented alphabetically, however, based on the name that comes the last. For example, if some author is called "Lee Zaa Ann" then this will come the first (because the last name in the full name starts with "A"). Conversely, if someone is called "Ann Lee Zaa" then this will be presented as the last reference. But what I really want is "Ann Lee Zaa" to be treated as the first author and so on. Do you happen to have a workaround for this? Thanks :)

ulyngs commented 3 years ago

Interesting, so you want ordering based on first number rather than last name? That feels slightly strange to me - wouldn't you always want sorting based on family name?

Either way, I think there's two options:

zavud commented 3 years ago

@ulyngs I am not really sure if I have to sort based on family or last name. I am just trying to write a normal-looking thesis (following the universal rules) :) and I read a lot of scientific papers and other theses and in all of them ordering is based on whatever comes first. And honestly, I do not know which name is someone's family and first names.

For example, this is a part from the last page of my references:

image

As you can see, you can find authors in the last page whose names start with "A" or "D" and so on. This may look very confusing and strange to the readers.

ulyngs commented 3 years ago

So the sorting in your current reference list is indeed by last name, i.e. family name:

This is expected behaviour.

It sounds like you actually just want their names to appears as "Upreti, Deepak" and so on, so that their family name is written before their first name in the reference list, which makes it immediately obvious that the sorting is alphabetical by family name

https://tex.stackexchange.com/questions/131087/displaying-authors-name-in-a-bibliographic-entry-in-the-form-surname-first-in

zavud commented 3 years ago

@ulyngs yes, this post makes sense. I downloaded the plainnat.bst file and put it in the templates directory. And then, I followed the answer in the link you shared. However, nothing really changed. Perhaps the the line natbib-bibliography-style: templates/myplainnat.bst in the yaml is overwritten by some line in the template.tex?

Even before I downloaded the plainnat.bst file, everything looked the same in the reference. Before downloading plainnat.bst , ACM-Reference-Format.bst was in use

ulyngs commented 3 years ago

to see if anything is getting overridden in unexpected ways, have a look at docs/_main.tex after you built to PDF - then you can see exactly what gets inserted in the .tex file that LaTeX builds the PDF from

you should also just try changing something at random in your templates/mypainnat.bst file and see if that propagates to the final PDF in the way you'd expect. The simplest way to start would be to just write a bunch of gibberish in that file and see if you get an error message when trying to build to PDF, as you'd expect

zavud commented 3 years ago

@ulyngs I checked the docs/_main.tex file as suggested. templates/myplainnat.bst has been inserted, which seems right.

I also did templates/mypainnat.bst as you suggested. It did give me an error without completing the knitting process.

ulyngs commented 3 years ago

... and are you able to make some sort of change in the bst files that also changes things in the reference list? E.g. try to remove the author names from appearing at all, just to make sure you can actually make it affect stuff in the output, then experiment from there

zavud commented 3 years ago

I am not sure how you completely remove the author names from appearing (first time in my life that I am dealing with a .bst file :D ). But I did remove the line { s nameptr "{ff~}{vv~}{ll}{, jj}" format.name$ 't := from the .bst file and I got an error when I tried to knit the file.

ulyngs commented 3 years ago

does it work as expected when you change this

{ s nameptr "{ff~}{vv~}{ll}{, jj}" format.name$ 't :=

to this

{ s nameptr "{vv~}{ll}{, f.}{, jj}" format.name$ 't :=

As per this

zavud commented 3 years ago

@ulyngs it actually does! I am really surprised because I did it multiple times yesterday (I swear :D) and failed to achieve this. But when copying from your comment here, it somehow (and perhaps "magically") worked! Thanks a lot for bearing with me and continuously giving suggestions! :)