# Sorting citations using \cites command in biblatex

I am using biblatex for citations and bibliography. Now I often have lists of multiple citations, for which biblatex has the \cites command. Now I can get citations to automatically sort if I don't want page numbers, but I cannot get it right with page numbers.

What I want is all citation ordered chronological (oldest first). Is this possible, or must I just do this by hand?

Here is an example.

\documentclass[11pt,oneside]{memoir}

\usepackage[
style=authortitle,
useprefix=false,
maxnames=2,
firstinits=true,
sortcites=true,
uniquename=init,
abbreviate=true,
backend=biber,
sorting=ynt
]{biblatex}

\begin{filecontents}{jobname.bib}
@book{tom1,
author = {Author, A.},
year = {2001},
title = {This book is from 2001},
shorttitle = {2001},
}
@book{tom2,
author = {Author, A.},
year = {1999},
title = {This book is from 1999},
shorttitle = {1999},
}
\end{filecontents}

\begin{document}
This one is corrent.\footnote{\Cites{tom1,tom2}.}
This one is corrent.\footnote{\Cite{tom1,tom2}.}

This one is incorrent.\footnote{\Cites{tom1}{tom2}.}
This one is incorrent.\footnote{\Cites[1]{tom1}[12]{tom2}.}
\end{document}

-
Technically, the "incorrect" examples are correct as \Cites is a "multicite" command where each key is a stand-alone citation, not part of a list of citations. It's used precisely for manually ordering etc. You're right that there seems to be no cite ordering possible when you need to give pages for each key. –  PLK Aug 4 '12 at 14:35
Yeah, I realise that they are technically correct. Just not exactly what I want... I was hoping there was a way to solve this. –  Tom de Bruin Aug 5 '12 at 9:42

The multicite commands take arbitrarily many arguments. They aren't designed to output a sorted citation list. It is possible to construct a two-pass version of \cites that will generate a sorted, qualified citation list.

\documentclass{article}
\usepackage[style=authortitle,sorting=ynt,sortcites]{biblatex}

\makeatletter

% original definition of \cites
\DeclareMultiCiteCommand{\cbx@cites}{\cite}{\multicitedelim}

% new definition
\DeclareMultiCiteCommand{\cites}[\cbx@cite@wrapper\cbx@cites]{\cbx@cite}{}

% first pass saves keys, prenotes, postnotes
\DeclareCiteCommand{\cbx@cite}
{\csxdef{prenote:\thefield{entrykey}}{\thefield{prenote}}}
{}
{\csxdef{postnote:\thefield{entrykey}}{\thefield{postnote}}}

% second pass outputs sorted citation list
\newrobustcmd{\cbx@cite@wrapper}[2]{%
\def\cbx@savekeys{}%
\def\cbx@citecall{#1}%
#2\cbx@sortkeysinit\cbx@citesort\cbx@citecall}

% internal list of saved keys => sorted argument list
\def\cbx@citesort{%
\def\do##1{%
\ifinlist{##1}{\cbx@savekeys}
{\protected@xappto\cbx@citecall{%
[\csuse{prenote:##1}][\csuse{postnote:##1}]{##1}}}
{}}%
\dolistloop{\cbx@sortkeys}}

% internal list of sorted entry keys
\def\cbx@sortkeysinit{%
\ifcsundef{blx@sort@\the\c@refsection}
{\global\letcs{\cbx@sortkeys}
{blx@slists@\the\c@refsection @entry@\blx@sorting}}% Biber
{\global\letcs{\cbx@sortkeys}{blx@sort@\the\c@refsection}}}% BibTeX
\def\cbx@sortkeys{}

\makeatother

\newcommand{\cmd}[1]{\texttt{\textbackslash #1}}
\setlength{\parindent}{0pt}
\begin{document}
\cmd{cite}: \cite{companion,ctan,vangennep,markey} \\
\cmd{cites}: \cites[e.g.][10]{companion}{ctan}{markey}[10--11]{vangennep} \\
\cmd{Cites}: \Cites{ctan}{markey}[e.g.][5--10]{companion}[10--11]{vangennep}
\printbibliography
\end{document}


Note that this new version of \cites does not properly support multiprenote and multipostnote arguments of the form

\cites(<pre>)(<post>)...


A tidy resolution will probably warrant extension of biblatex internals. This is something we should consider as \textcites in the verbose styles suffers from the same limitation.

-
Ah, thanks for that. I've actually subsequently gone back and edited every use of \cites in my PhD... Oh well. –  Tom de Bruin Oct 4 '12 at 14:21
What is the answer for this thread? I am getting stuck with this issue too. I want to cite two authors in a row but wanting to add their appropriate page numbers. –  user24947 Jan 24 '13 at 19:43
@expling I've given an answer above. If you don't want the citations printed in the same order as the bibliography, then refer to this post: tex.stackexchange.com/q/18910. –  Audrey Jan 26 '13 at 17:37
With bibtex this works, but when using biber I get undefined control sequence \cbx@sortkeys. –  user4514 Mar 5 at 17:00
@user4514 That error resolves after you run biber the first time. To avoid it entirely replace the biber-specific code with \ifcsundef{blx@slists@\the\c@refsection @entry@\blx@sorting}{}{\global\letcs{\cbx@sortkeys}{blx@slists@\the\c@refsection @entry@\blx@sorting}}. –  Audrey Mar 5 at 20:12
show 1 more comment