我使用natbib
带compress
选项的包,在 [1-3] 的情况下效果很好。但是我希望 [1,2] 也能压缩为 [1-2]。
答案1
内置机制natbib
不会为两个数字确定范围。为此,我们必须修补代码:
\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}
我所做的就是取出似乎生成逗号的测试(使用\@citea
):natbib
非常小心不要使用太多的宏和寄存器,但这会使代码有点难以阅读!