11

I want to sort in-text citations by year, but bibliography by name. It sound exactly like this question.

However, suggested solution (using \usepackage[sorting=ynt]{biblatex} and \printbibliography[sorting=nty]) does not work for me (see MWEB below). Indeed, the list of references remains year-sorted instead of name sorted.

Question: How to force sorting=nty in \printbibliography?


enter image description here

\documentclass{article}
    \usepackage[%
        backend=biber,%
        style=authoryear-comp,%
        natbib=true,%
        sorting=ynt%
    ]{biblatex}
    \addbibresource{\jobname.bib}
    \usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
    @book{key2000,
      author = {Author, A.},
      year = {2000},
      title = {Alphabetical fist \& Year last},
      publisher = {Publisher},
    }
    @book{key1900,
        author = {Boathor, B.},
        year = {1900},
        title = {Alphabetical last \& Year first},
        publisher = {Publisher},
}
\end{filecontents}

\begin{document}
    Citation should be year-sorted, even if \verb+\citep+ is random \citep{key2000, key1900}.% citation NOT year-sorted

    \printbibliography[sorting=nyt]
\end{document}

P.S.: At the opposite, using

\begingroup
    \newrefcontext[sorting=nyt]
    \printbibliography
\endgroup 

overrides the \usepackage[sorting=ynt]{biblatex} option (i.e. LoR is name-sorted, but in-line citations as well).

ebosi
  • 11,692
  • 2
    At the moment, I'm afraid, it looks quite bleak. I hope someone comes up with a good answer. See Different citation order in text and bibliography using biblatex and latexmk, Multiple citations: citation order different to bibliography order. The problem is that if you use different sorting schemes the disambiguation letters might be off. – moewe Mar 29 '17 at 14:21
  • 1
    The problem is that the sorting option has been moved from \printbibliography to so called 'refcontexts'. See Biblatex order of entries in a multi-citation. And the default is that all citations obey the refcontext they were last printed in the bibliography. So simply using a global sorting that contradicts the \printbibliography's refcontext's sorting does not work – moewe Mar 29 '17 at 14:24
  • @moewe I indeed saw Biblatex order of entries in a multi-citation and started to use sortcites=false and manually sorting references within citation... but I wanted to be lazy and make (Bib)LaTeX do it for me! | Just to be sure to understand your previous comment: the sorting scheme chosen, is the one of the refcontext selected when printbibliography is called and not at the time of the \cite command, right? (I tried to add \newrefcontext[sorting=ynt] right after \begin{document} + what I added in my P.S, but it indeed doesn't work.) – ebosi Mar 29 '17 at 14:32
  • 1
    Indeed. Specifically it is the refcontext of the last appearance of the particularly entry in an \printbibliography. There are ways to manually assign the refcontext (i.e. override the 'use the refcontext of the last bibliography' rule). But you need to be careful about potentially different extrayears. – moewe Mar 29 '17 at 14:33
  • Did my answer work for you even in your actual production document? – moewe Apr 06 '17 at 09:12

2 Answers2

14

Update

With biblatex v3.18 and above you can use \localrefcontext and \GenRefcontextData to switch to a different reference context (e.g. sorting).

Use

\AtBeginRefsection{\GenRefcontextData{sorting=ynt}}

to make sure that each refcontext our document uses also generates an analogous refcontext with ynt sorting.

Then

\AtEveryCite{\localrefcontext[sorting=ynt]}

switches our citations to the ynt sorting refcontext. \localrefcontext only acts locally and need not be "closed" or reset because it happens inside a group.

\documentclass{article}
\usepackage[backend=biber,
  style=authoryear-comp,
  % sortcites=true, % not needed here because it is implied by style=authoryear-comp,
]{biblatex}

\AtBeginRefsection{\GenRefcontextData{sorting=ynt}} \AtEveryCite{\localrefcontext[sorting=ynt]}

\begin{filecontents}{\jobname.bib} @book{key2000, author = {Author, A.}, year = {2000}, title = {Alphabetical fist & Year last}, publisher = {Publisher}, } @book{key1900, author = {Boathor, B.}, year = {1900}, title = {Alphabetical last & Year first}, publisher = {Publisher}, } \end{filecontents} \addbibresource{\jobname.bib}

\begin{document} Lorem \autocite{key2000, key1900}

ipsum \autocite{key1900, key2000}

\printbibliography \end{document}

Lorem (Boathor 1900; Author 2000)
ipsum (Boathor 1900; Author 2000)
References
Author, A. (2000). Alphabetical fist & Year last. Publisher.
Boathor, B. (1900). Alphabetical last & Year first. Publisher.

See also Sort citations by year (ynt) and references by name (nyt) in custom biblatex style.


Solution for older versions of biblatex

Give the desired citation sort order at loading time. Then give the desired order for the bibliography in the new refcontext (\begin{refcontext}[sorting=<sorting>]...\end{refcontext}) for \printbibliography.

\documentclass{article}
\usepackage[backend=biber,
  style=authoryear-comp, sorting=ynt,
  % sortcites=true, % not needed here because it is implied by style=authoryear-comp,
]{biblatex}

\begin{filecontents}{\jobname.bib} @book{key2000, author = {Author, A.}, year = {2000}, title = {Alphabetical fist &amp; Year last}, publisher = {Publisher}, } @book{key1900, author = {Boathor, B.}, year = {1900}, title = {Alphabetical last &amp; Year first}, publisher = {Publisher}, } \end{filecontents} \addbibresource{\jobname.bib}

\begin{document} Lorem \autocite{key2000, key1900}

ipsum \autocite{key1900, key2000}

\begin{refcontext}[sorting=nyt] \printbibliography \end{refcontext} \end{document}

Lorem (Boathor 1900; Author 2000)//ipsum (Boathor 1900; Author 2000)

I used

\begin{refcontext}[sorting=nyt]
\printbibliography
\end{refcontext}

instead of the slightly shorter

\newrefcontext[sorting=nyt]

to be on the safe side if there are citations after the bibliography.


Hints and Caveats

Some more explanation since this comes up more often.

biblatex does not allow the sorting option for \printbibliography any more. It was removed because it could lead to weird sorting results.

Instead, now you use 'refcontexts' to control sorting. A refcontext controls sorting, labelprefix and sortingnamekeytemplate and a few other things (possible more in the future).

An entry can appear in different refcontexts and any extra label data (extradate, extraalpha) will be recalculated based on the specific details (e.g. sorting) for each refcontext.

This can lead to slightly counter-intuitive results in very contrived examples because the sort order may be determined by data that is invisible in the citation itself and those data leads to different sorting results in different schemes.

Here is an admittedly very artificial example that shows this behaviour with your set-up. It can be much easier to achieve such an effect with other pairings of sort schemes. The trick here was that nyt considers the volume for sorting while ynt does not.

\documentclass{article}
\usepackage[backend=biber, style=authoryear, sorting=ynt, sortcites]{biblatex}
\usepackage{hyperref}

\begin{filecontents}{\jobname.bib} @book{one, author = {Elk, Anne}, title = {Title}, volume = {1}, note = {sorts first in ynt}, } @book{two, author = {Elk, Anne}, title = {Title}, note = {sorts first in nyt}, } \end{filecontents}

\addbibresource{\jobname.bib}

\begin{document} \autocite{one,two} \autocite{two,one}

\begin{refcontext}[sorting=nyt] \printbibliography[title={\refname{} (sorting \texttt{nyt})}] \end{refcontext} \end{document}

(Elk n.d.[b]; Elk n.d.[a]) (Elk n.d.[b]; Elk n.d.[a])

moewe
  • 175,683
  • Can you construct a situation in which the situation described in your last paragraph actually occurs? If I understand it correctly, two entries should only receive an extrayear, if they have the same authors and the same year. However, in this case the order is the same, both in nyt and ynt. Hence, the extrayear will not differ. – gerw Jul 21 '17 at 14:22
  • @gerw See the edit, please. The example is contrived and uses a dependence on the volume field in one of the two schemes that might not have been immediately obvious, but it shows that the problem can be reproduced and is not purely theoretical. As mentioned in the answer, other pairings of schemes might be much more susceptible to these kind of entries. – moewe Jul 21 '17 at 15:00
  • I see. However, if I add the (quite reasonable) assumption that no two titles are the same (at least with same authors and years), can something like this happen with nyt and ynt, too? – gerw Jul 21 '17 at 18:58
  • @gerw In that case you are probably safe. At least I can't think of an example that would be problematic in that case off the top of my head. I would, however, not rely on things working out smoothly when it is possible to come up with problematic cases. Keep in mind that with other sort schemes different things might trigger the problematic behaviour. – moewe Jul 21 '17 at 19:07
  • ... Additionally, this is only one (arguably the most - maybe even the only - visible) symptom of the separation into different refcontexts. There are few other things that can differ between refcontexts, if that list expands in future versions, this could become more problematic. – moewe Jul 21 '17 at 19:09
  • 1
    I understand. However, I really like to have ynt in the text and nyt in the bibliography. Btw, what is the explanation of this weird behaviour with volume? – gerw Jul 21 '17 at 19:11
  • @gerw Well, if you must really have it, then currently the answer is the only way to go (save for sorting the citations manually yourself). It makes sense to include the volume for sorting in many schemes (think a multi-volume work published all in the same year), but in ynt it would not be clear where in the sort order volume should go, that's why it is omitted (I think, I can't be sure of course). Since a missing volume is equivalent to volume = {0} for sorting purposes in all schemes that use volume this allows us to generate the problematic case above. – moewe Jul 21 '17 at 19:19
  • @moewe, how to use refcontext together with refsection? – bonanza Jul 25 '18 at 06:54
  • 1
    @bonanza As usual: You can have \newrefcontext or \begin{refcontext}...\end{refcontext} inside a refsection. Does that not work for you? If you have problems with this, I suggest you ask a new question with an MWE. – moewe Jul 25 '18 at 06:57
7

A 'quick and dirty' solution (less good than moewe's one) is use biblatex's sortcites=false option, to enable the manual sorting of in-line citations.

enter image description here

\documentclass{article}
    \usepackage[%
        backend=biber,%
        style=authoryear-comp,%
        natbib=true,%
        sorting=nyt,%
        sortcites=false,
    ]{biblatex}
    \addbibresource{\jobname.bib}
    \usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
    @book{A2000,
      author = {Author, A.},
      year = {2000},
      title = {Alphabetical fist \& Year last},
      publisher = {Publisher},
    }
    @book{B1900,
        author = {Boathor, B.},
        year = {1900},
        title = {Alphabetical last \& Year first},
        publisher = {Publisher},
    }

    @book{C1950,
        author = {Coathor, C.},
        year = {1950},
        title = {Alphabetical last \& Year first},
        publisher = {Publisher},   
    } 
}
\end{filecontents}

\begin{document}
    In-line citations should be manually sorted in the desired order, and the References list will be alphabetically sorted.

    Example: 
    \begin{itemize}
        \item This citation is manually year-sorted \citep{B1900, C1950, A2000},
        \item this one is manually shuffled \citep{C1950, B1900, A2000}.
    \end{itemize}

    \printbibliography[]
\end{document}
ebosi
  • 11,692
  • Quick for sure but definitively not dirty (@moewe one is the dirty ones as they explained), so works very well for me (requires ordering the citation yourself of course, so does not totally answer the “even if citep is random” part, but that is much more convenient actually). – Archange Mar 26 '19 at 15:39