% Date:    Thu, 25 Jun 92 22:19:49 -0500
% From:    karney@gov.pppl.theory (Charles Karney)
% Subject: Re: BibTeX is driving me nuts
% Keywords: BibTeX, LaTeX, numbered references, ranges
% 
% In TeXhax V92 #012, carlos@argon.berkeley.edu asks for BibTeX to collapse
% multiple citations into ranges to give [1-4] instead of [1,2,3,4].  This
% is, of course, nothing to do with BibTeX, which deals only with the stuff
% in the bibliography environment.
% 
% However, the following definition of \@citex will do the job.  I lifted
% this out of my style for Nuclear Fusion nf.sty (available via anonymous ftp
% from lyman.pppl.gov:pub/tex).  Stick this definition into a file
% 'citecollapse.sty' and give 'citecollapse' as an optional argument to
% \documentstyle.
% 
% Collapse citation numbers to ranges.  Non-numeric and undefined labels
% are handled.  No sorting is done.  E.g., 1,3,2,3,4,5,foo,1,2,3,?,4,5
% gives 1,3,2-5,foo,1-3,?,4,5
\newcount\@tempcntc
\def\@citex[#1]#2{\if@filesw\immediate\write\@auxout{\string\citation{#2}}\fi
  \@tempcnta\z@\@tempcntb\m@ne\def\@citea{}\@cite{\@for\@citeb:=#2\do
    {\@ifundefined
       {b@\@citeb}%
        {\@citeo\@tempcntb\m@ne\@citea\def\@citea{,\penalty\@m\ }{\bf ?}%
        \@warning{Citation `\@citeb' on page \thepage \space undefined}}%
    {\setbox\z@\hbox{\global\@tempcntc0\csname b@\@citeb\endcsname\relax}%
     \ifnum\@tempcntc=\z@ \@citeo\@tempcntb\m@ne
       \@citea\def\@citea{,\penalty\@m\ }\hbox{\csname b@\@citeb\endcsname}%
     \else
      \advance\@tempcntb\@ne
      \ifnum\@tempcntb=\@tempcntc
      \else\advance\@tempcntb\m@ne\@citeo
      \@tempcnta\@tempcntc\@tempcntb\@tempcntc\fi\fi}}\@citeo}{#1}}
\def\@citeo{\ifnum\@tempcnta>\@tempcntb\else\@citea\def\@citea{,\penalty\@m\ }%
  \ifnum\@tempcnta=\@tempcntb\the\@tempcnta\else
   {\advance\@tempcnta\@ne\ifnum\@tempcnta=\@tempcntb \else \def\@citea{--}\fi
    \advance\@tempcnta\m@ne\the\@tempcnta\@citea\the\@tempcntb}\fi\fi}

%   Charles Karney
%   Plasma Physics Laboratory      E-mail:  Karney@Princeton.EDU
%   Princeton University           Phone:   +1 609 243 2607
%   Princeton, NJ 08543-0451       FAX:     +1 609 243 2662

