如何实现引用连续的数字 使用短线连接 即:[1,2] 变成 [1-2]

发布于 2021-06-16 16:11:23

我们有些时候希望这样的效果,下面是hack实现的代码,大家可以拿去用了:

\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}

效果图:
image.png

选自:https://tex.stackexchange.com/questions/86987/cite-like-1-2-instead-of-1-2/86991#86991

0 条评论

发布
问题