Here's a way to remove duplicates, using all the \cite?
commands of natbib
:
\begin{filecontents*}{\jobname.bib}
@article{key1,
author={A. Author},
title={Title},
journal={Journal},
year={2000},
}
@article{key2,
author={A. Buthor},
title={Title},
journal={Journal},
year={2000},
}
@article{key3,
author={A. Cuthor},
title={Title},
journal={Journal},
year={2000},
}
@article{key4,
author={A. Duthor},
title={Title},
journal={Journal},
year={2000},
}
@article{key5,
author={A. Euthor},
title={Title},
journal={Journal},
year={2000},
}
\end{filecontents*}
\documentclass{article}
\usepackage[sort&compress]{natbib}
\usepackage{expl3}
\ExplSyntaxOn
\AtBeginDocument{
\cs_set_eq:Nc \adp_orig_citex:wwn { @citex }
\cs_set:cpn { @citex } [ #1 ] [ #2 ] #3
{
\clist_set:Nx \l_tmpa_clist { #3 }
\clist_remove_duplicates:N \l_tmpa_clist
\adp_orig_citex:nnV { #1 } { #2 } \l_tmpa_clist
}
} % end of \AtBeginDocument
\cs_new:Npn \adp_orig_citex:nnn #1 #2 #3
{
\adp_orig_citex:wwn [ #1 ] [ #2 ] { #3 }
}
\cs_generate_variant:Nn \adp_orig_citex:nnn { nnV }
\ExplSyntaxOff
\begin{document}
\def\grpA{key1,key2,key3}
\def\grpB{key3,key4,key5}
The first set \citep{\grpA} and the second set \citep{\grpB} discuss XYZ.
In total \citep{\grpA,\grpB}, the bla bla bla is in common.
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}
The code works as follows:
\cs_set_eq:Nc \adp_orig_citex:wwn { @citex }
saves a copy of \@citex
, which is the internal command used by natbib
the code
\cs_set:cpn { @citex } [ #1 ] [ #2 ] #3
{
\clist_set:Nx \l_tmpa_clist { #3 }
\clist_remove_duplicates:N \l_tmpa_clist
\adp_orig_citex:nnV { #1 } { #2 } \l_tmpa_clist
}
redefines \@citex
to do a couple of steps more; it sets the variable \l_tmpa_clist
to the expanded contents of the third argument (the list of keys), then removes duplicates from it and feeds it to the following instruction
\adp_orig_citex:nnV { #1 } { #2 } \l_tmpa_clist
is equivalent to
\adp_orig_citex:nnn { #1 } { #2 } {<contents of \l_tmpa_clist>}
because of the \cs_generate_variant:Nn
command below
\adp_orig_citex:nnn
just call the original copy of \@citex
sort&compress
works fine as far as it goes, but there is no 'unique set' stage. Thus if you want to allow removal of duplicate keys you'll need a preprocessing step. Are we allowed to load for exampleexpl3
to avoid needing to define a suitable 'remove duplicates' command?expl3
, so I am open ears....sort&compress
still works, however,[1-4]
gets changed to[1-3,3,4]
if there are two instances of3
, rather than realizing that3,3
is still within the range[1-4]
.