1

There may be similar question, but none of them does really anwer the following one. I would like to have the references sorted by firt author's surname, but the number should correspond to the appearnce in the text using natbib, e.g., it should look like

[42]  A. Einstein, Relativity, 1927...
[1]   R. P. Feynmann, Magnetism, 1957...
[102] J. C. Maxwell, Electricity, 1873...

Thanks for your help.

3
  • 2
    Welcome. // You can increase the chances for replies, when you add code to your request, which we can use to copy, compile and learn about your current attempts. Thanks. // See also the Related links to the right, or try the search options on this site.
    – MS-SPO
    Commented Sep 22, 2022 at 7:42
  • In particular, do please tell if you employ the natbib citation management package and, if so, the options you employ for this package. Next, which bibliography style do you use?
    – Mico
    Commented Sep 22, 2022 at 7:49
  • 1
    are you sure you want that? I'm sure it would be possible to force it but I have never seen such a scheme, and it will be rather confusing to your readers. Especially on paper where you can not just follow a link, how is a reader supposed to find the citation referenced by [42] if the list is not in order? Commented Sep 22, 2022 at 8:04

1 Answer 1

0

The following contains a hack to change the numbers. Note that it is rather confusing to the readers.

The idea is to sort alphabetically using the plainnat style, but change the code that assigns the number to each alphabetically ordered item.

To do so, the code should keep track of the cite order in the document. This is done by adding some code to the \cite command that stores a counter using the cite key as a macro name. This counter should only be assigned on the first mention of a citation, so you can check for existence of the macro named using the cite key.

Then the bibliography code is modified (specifically \@lbibitem) to use the citekey-macro as the number instead of the regular NAT@ctr counter.

Modifying the commands can be done using \patchcmd and \apptocmd from etoolbox, which also provides \csxdef and \ifcsundef.

MWE:

\begin{filecontents}{\jobname.bib}
@misc{relativity,
  author = {Einstein, A.},
  title = {Relativity},
  year = {1927}
}
@misc{magnetism,
  author = {Feynmann, R.P.},
  title = {Magnetism},
  year = {1957}
}
@misc{electricity,
  author = {Maxwell, J.C.},
  title = {Electricity},
  year = {1873}
}
\end{filecontents}
\documentclass{article}
\usepackage{hyperref}
\usepackage[numbers]{natbib}
\usepackage{etoolbox}

% counter to keep track of document citation order
\newcounter{myunsrt}
\setcounter{myunsrt}{1}

\makeatletter
% use the cite key macro (argument #2 of \bibitem and \@lbibitem) as number
% instead of a sequential NAT@ctr
\patchcmd{\@lbibitem}{\global\advance\c@NAT@ctr\@ne}{\setcounter{NAT@ctr}{\csname#2\endcsname}}{}{}

% store the current value of the myunsrt counter in a macro
% using the cite key (argument #3) as macro name
\apptocmd{\NAT@citexnum}{%
\ifcsundef{#3}% don't renumber if it already exists
{\csxdef{#3}{\themyunsrt}\stepcounter{myunsrt}}%
{}%
}{}{}
\makeatother
\begin{document}
\cite{magnetism}, \cite{relativity}, \cite{electricity}
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

Result:

enter image description here

Surprisingly, it worked with hyperref out of the box, producing correct links.

Note that the cite keys have to be suitable as macro names to use this hack.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .