I'm using natbib package with compress option, which works well in the case [1-3]. However, I hope that [1,2] could also be compressed as [1-2].
Asked
Active
Viewed 1,463 times
7
1 Answers
12
The built-in mechanism in natbib does not make range for two numbers. To do that, we have to patch the code:
\begin{filecontents}{\jobname.bib}
@article{demo1,
title = {Some things {I} did},
author = {Other, A. N.},
journal = {J. Irrep. Res.},
year = {2012},
pages = {x-y}
}
@article{demo2,
title = {Cool!},
author = {Nobacon, D.},
journal = {Ann. Improb. Res.},
year = {2012},
pages = {a-b}
}
\end{filecontents}
\documentclass{article}
\usepackage[numbers,sort&compress]{natbib}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\NAT@citexnum}
{%
\ifx\NAT@last@yr\relax
\def@NAT@last@yr{\@citea}%
\else
\def@NAT@last@yr{--\NAT@penalty}%
\fi
}
{%
\def@NAT@last@yr{--\NAT@penalty}%
}
{}{\FAIL}
\makeatother
\bibliographystyle{unsrtnat}
\begin{document}
\cite{demo1,demo2}
\bibliography{\jobname}
\end{document}
All I'm doing there is taking out the test which seems to generate the comma (using \@citea): natbib is very careful not to use too many macros and registers, but that makes the code a bit tricky to read!
Joseph Wright
- 259,911
- 34
- 706
- 1,036
-
Note that a different solution would be needed for the
citepackage (another patch) or forbiblatex. – Joseph Wright Dec 14 '12 at 08:46 -
Is there a similar way to change the default "--" between compressed entries to "-"? – Mirai May 16 '17 at 09:29
-
@Mirai That's what the
--in the above provided: the ligature for an en-dash. Using a hyphen here isn't typographically correct but of course it's your call ultimately ... – Joseph Wright May 16 '17 at 09:47 -
Thank you for help, but you are only partly correct, a hyphen is correct in some styles and some languages - but one type of "range delimiter" should be used along in a single work – Mirai May 16 '17 at 10:16
natbibandcite, but they can't handle this situation. – Haocheng Dec 14 '12 at 07:25