%%    Authorder.tex
%%      Edited from Reforder.ez  8/30/12   David T.
%%      Output is in jobname.ref
%%      Format of affiliations is to be close to what the journal requires. 
%%      The numbers are correct. The \refto{CT} will have to be changed to $^{1}$ etc.
%%      \affilfile is \let to \referencefile
%%
%%      Comments below from reforder:
%%      Parts written            6/7/85    Doug E. (REFORDER.TEX)
%%      Other parts written      89--92    Donald A. (CITE.STY)
%%
\csname reforderloaded\endcsname % Only execute me once.
\edef\reforderloaded{\the\catcode`\@}% minor trick to remember the catcode
%%
%%    This macro package is intended for use with JNL and EZJNL.
%%    It will automatically order and sort the references in a paper
%%    by order of first citation.(!!)  To use, say \input reforder
%%    after \input ez (and after all definitions of \ref etc.,
%%    and in particular AFTER any use of the \refstyleXX macros)
%%    but before any use of \refto etc.  Use \refto{}, or \ref{}, or
%%    \Ref{}, to cite references in the text.  Use \refis{} to supply
%%    the references.  SKIP A LINE after each reference.  Open the
%%    reference listing with \references and close it with \endreferences.
%%
%%    REFORDER depends on the
%%    JNL macros \refto{}, \ref{}, \Ref{} to identify citation of references.
%%    REFORDER also contains a macro \cite{} which can be used to cite
%%    references; e.g., ``Reference \cite{19} blah...'' will produce
%%     output ``Reference 19 blah''.  Multiple citations can be separated
%%    by commas.  E.g., \refto{24,26,27} and \cite{3,7}
%%    are legal.  Also legal is \refto{3-7}, which expands to mean the same
%%    as \refto{3,4,5,6,7}.  Reference ``numbers'' can in general be any
%%    alphanumeric string; e.g. BjorkenAndDrell is perfectly OK used in
%%    the form\ref{BjorkenAndDrell};  such strings should contain no blanks.
%%
%%    If you have your own pet macros to cite references such as, e.g.,
%%    \def\referpet#1{$^{(#1)}$)}, you can bring it to the attention of
%%    REFORDER so all \referpet's will be properly cited simply by
%%    declaring ``\citeall\referpet'' once near the beginning, after
%%    \referpet is defined and after \input reforder.  This has the
%%    effect of redefining the macro as e.g., 
%%       \def\referpet#1{$^{(\cite{#1})}$}.  
%%    (Such \citeall'ed macros must have exactly one argument #1, as in
%%    \referpet.)  See e.g., the end of this file where \ref and \Ref are
%%    \citeall'ed (but commented out.)
%%
%%    REFORDER depends on the macro \refis{} for listings.  In fact,
%%    \refis{} can be used to supply a reference anywhere in the paper 
%%    after the first citation.  The macro \endreferences actually triggers
%%    sorting and listing of references.  Skip a line after a reference
%%    listing (or, alternatively, end each listing with <space>\par).
%%
%%    This version of reforder also includes:
%%    %%%%%%%%%%%part of%%%%%%%%%%%%%%%%%%%%%%%%%
%%    C I T E . S T Y version 3.0  (Oct 1992)
%%    Compressed, sorted lists of numerical citations: [11-16]
%%    Copyright (C) 1989-1992 by Donald Arseneau
%%    These macros may be freely transmitted, reproduced, or modified for
%%    non-commercial purposes provided that this notice is left intact.
%%
%%    \citen contains the code that parses the list of names, ignoring
%%    spaces after commas, and formats the
%%    number list. It will:
%%      o Sort citation numbers into ascending order.
%%      o Compress lists of three or more consecutive numbers to one
%%        number range which can be split, with difficulty, after the dash.
%%    E.g., if you used to get [7,5,6,4,9,8], then this style
%%    will give you [4-9].
%%
%%    Although each \cite command sorts its numbers, better compression
%%    into ranges can usually be achieved by carefully selecting the order
%%    of the entries or the order of initial citations.
%%    Having the entries presorted will also save processing time,
%%    especially for long lists of numbers.
%%
%%    Version 1991: Move most of \citen into \@cmpresscites for speed.
%%    Version 1992: Use \citepunct for commas so it is easier to change.
%%    Version 3.0 (1992):  Rewrite, including sorting.
%%
%%    See cite30.sty for some history on these versions.
%%    Donald Arseneau       asnd@reg.triumf.ca
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%    Note (DT):
%%
%%    If you want to cite things out of order[21,4] in an attempt to
%%    suggest precedence, you will have to use several \reftos. The 
%%    editor will probably change this anyway. Rewrite your sentence.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%    Changes:
%%
%%    5-11-90 dt. Big change of how references are listed in the paper.
%%    This version of REFORDER will only output references which have
%%    been cited in the text with \refto or \ref or \Ref or \cite.
%%    Those which have not been cited up to now will be noted on the
%%    terminal and logfile but will be omitted in the paper.  Thus, you
%%    can keep a huge file of \refis{} listings and REFORDER will pick
%%    out those used in the paper and omit the rest.
%%       The count \r@funused is used to keep track of the unused references
%%    and subtracted from \r@fcount just before the reference listing begins.
%%    \global added to \advance\r@funused 17-Sep-1990. dt
%%    The logic of this was modified again 06-Oct-1990 17:03:18 to avoid
%%    defining the control sequence \r@ftext... for the unused references,
%%    making the memory requirements much less if the list is extremely long.
%%
%%    \citeall\refto removed and \refto redefined in EZJNL so that the 
%%    \spacefactor could be passed around the \refto. 14-Nov-1990
%%    This needs to be changed if reforder is going to be used with JNL!
%%    Uncomment the line below:
%%     % \citeall\refto     % Uncomment if used with JNL.
%%    ditto for \ref and \Ref 19-Aug-1992
%%     % \citeall\ref       % Uncomment if used with JNL.
%%     % \citeall\Ref       % Uncomment if used with JNL.
%%
%%    The the message for ``not cited up to now'' changed 10-Feb-91
%%    so that the reference file <filename>.ref can be as written.
%%    Text changed 06-Oct-1993 21:59:13 to "Skipping unused ref nnn"
%%
%%    \w@rnwrite replaced by \message, since that's all it was.
%%    09-Sep-1991 22:42:03
%%
%%    Add space after \refis{##1} in definition of \referencefile
%%    31-Oct-1991 19:25:35
%% 
%%    Added ^^J as in TOC.TEX to put blank lines in the reference file,
%%    making it more readable. 26-Jan-1992 23:31:18
%% 
%%    \citer@nge (cite range m-n) removed 18-Feb-1993 22:54:02
%% 
%%    \input once only with \csname .. \endcsname 05-Feb-1993 17:29:48
%%
%%    Simplify macros which assign reference numbers 17-Feb-1993
%%
%%    Add parts of cite30.sty 18-Feb-1993 22:48:57
%%
%%    Removed the \writer@f##1>> from \ifuncit@d so no mention of uncited
%%    references appear in the <jobname>.ref file
%%
%%    Put this back in but with an \ifwritetobe, normally false
%%    can activate it with a \writetobetrue.
%%
%%    Removed unnecessary \ifr@ffile ... \fi in definition of 
%%    \referencefile. 12-May-1997 14:29:03
%%
%%    Changed \cite to \cit@ to avoid problems if \cite is \let to 
%%    \refto 27-Sep-2005  0:57:24 dt
%%=======================================================================
\catcode`\@ 11 % allow access to private macros
% Some definitions from jnl
\def\refto#1{%
    \ifhmode
       \edef\@sf{\spacefactor\the\spacefactor}\/% trick from \footnote
    \else
       \let\@sf\relax
    \fi
    \refer{#1}\@sf\relax}%
\def\refer#1{\unskip$^{\cit@{#1}}$}% the style part of \refto
\def\cit@#1{{#1}}%            %    for use with reforder.
\let\cite\cit@
\def\references{\begingroup
    \frenchspacing
    \setbox\z@ \hbox{000.\enspace}% probably no more than 999 references.
    \ifdim \wd\z@>\parindent \parindent \wd\z@ \fi
    \everypar{\hangindent\parindent}}%\newcount\r@fcount \r@fcount 0
\def\endreferences{\everypar{}\endgroup}% Should remember, restore  7-Nov-2002 dt
\def\refis#1{\item{#1.}}%              % Ref list numbers.
%
% From LaTeX.
%
\def\@nnil{\@nil}%
\def\@fornoop#1\@@#2#3{}%
%
\def\for#1:=#2\do#3{\edef\@fortmp{#2}\ifx\@fortmp\empty \else
      \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}\fi}%
%
\def\@forloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@iforloop #3\@@#4{#5}\fi\fi}%
%
\def\@iforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
       \let\@nextwhile\@fornoop \else
      #4\relax\let\@nextwhile\@iforloop\fi\@nextwhile#2\@@#3{#4}}%
%
\def\gobble#1{}%                              % Swallow parameters.
%
\newlinechar `^^J                             % For returns in error messages.
%
\newcount\r@fcount \r@fcount 0
\newcount\r@fcurr
\newcount\r@funused \r@funused 0
%
%% This works but requires different names if same references are used
%% in different sections:
%\def\resetrefnumbers{\global\r@fcount 0 \global\r@funused 0 }%
%
\newwrite\reffile
\newif\ifr@ffile\r@ffilefalse
\newif\ifwritetobe\writetobefalse
{\catcode`\%=12\gdef\percentchar{%}}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reference file stuff
%
\def\writer@f#1>>{}%
%
{\catcode`\^ 7 % circumflex for superscripts
\newlinechar `^^J
\gdef\referencefile{%              Stuff to write .REF file
    \r@ffiletrue\immediate\openout\reffile \jobname.ref
    \def\writer@f##1>>{\immediate\write\reffile
       {^^J\noexpand\refis{##1} %
        \expandafter\expandafter\expandafter\strip@t\expandafter
        \meaning\csname r@ftext\csname r@fnum##1\endcsname\endcsname
%        \percentchar\jobname:ref.\csname r@fnum##1\endcsname%
}}%
    \def\strip@t##1>>{}%
    }%
}%
%
\let\referencelist \referencefile
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% assign the reference numbers
%
\newtoks\citenumbers
%
\def\cit@#1{\citenumbers{}% Variable No. of args, separated by ","
    \firstr@f#1,\end,%
    \expandafter\citen\expandafter{\the\citenumbers}}%
\let\cite\cit@
%
\def\firstr@f#1,{\r@featspace#1 \applyr@f}% space after `#1' req'd
%
\def\applyr@f#1,{%
    \ifx\end#1%
       \let\next \relax
    \else
       \expandafter\citenumbers\expandafter{\the\citenumbers,}%
       \r@featspace#1 \let\next \applyr@f % space after `#1' req'd
    \fi
    \next}%
%
\def\r@featspace#1#2 {\r@fcite#1#2,}% Eat spaces at beginning or end of arg
%
\def\r@fcite#1,{%
    \ifuncit@d{#1}%           Cite individual reference
       \ifwritetobe
          \expandafter\gdef\csname r@ftext\number\r@fcount\endcsname{%
          \message{Reference #1 to be supplied.}%
          \writer@f#1>>#1 to be supplied.\par}% 
       \else 
          \expandafter\gdef\csname r@ftext\number\r@fcount\endcsname{%
          \message{Reference #1 to be supplied.}%
          #1 to be supplied.\par}%
       \fi
    \fi
    \citenumbers \expandafter{\the\citenumbers\csname r@fnum#1\endcsname}}%
%
\def\ifuncit@d#1{\expandafter\ifx\csname r@fnum#1\endcsname\relax%
    \global\advance\r@fcount\@ne
    \expandafter\xdef\csname r@fnum#1\endcsname{\number\r@fcount}}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sort into acending order and compress. (CITE30.STY)
%
\newcount \refnumbr % tempcnta
\newcount \maxrefno % tempcntb
%
\def\citen#1{%
    \def\citegroup{#1}%
    \maxrefno -1           % \maxrefno tracks higest number
    \let\held\relax        % nothing held from list yet
    \let\citea\empty       % no punctuation preceding first
    \let\celt\over         % not expandable, but identifiable
    \def\citelist{}%       % empty list to start
    \for                   % make a sorted list of numbers
       \eachcite:=\citegroup
       \do{\makecitelist}%
    \refnumbr -1           % After sorted citelist is made,
    \let\celt\compresscite % execute it to compress citation ranges.
    \citelist              % output number list with compression
    \held}%                % output anything held over
%
% For each citation, insert it in the sorted \citelist
%
\def\makecitelist{%
    \refnumbr\eachcite \relax
    \ifnum \refnumbr>\maxrefno % new highest, add to end (efficiently)
       \edef\citelist{\citelist \celt{\eachcite}}%
       \maxrefno\refnumbr
    \else % arbitrary number: insert appropriately
       \edef\citelist{\expandafter\sortcelt \citelist \gobble @}%
    \fi}%
%
% Check if each number follows previous and can be put in a range
%
\def\compresscite#1{%     % This is executed for each number
    \advance\refnumbr 1   % Now \refnumbr is one more than the previous number
    \ifnum #1=\refnumbr   % Number follows previous---hold on to it
       \ifx\held\relax    % first pair of successives
          \edef\held{\citea #1}%
       \else              % compressible list of successives
          \ifmmode        % math==superscript
             \edef\held{\noexpand\hbox{\the\scriptfont0--}#1}%
          \else           % text mode
             \edef\held{\noexpand\hbox{--}#1}%
          \fi
       \fi
    \else                 %  non-successor---dump what's held and do this one
       \held\citea #1\let\held\relax
    \fi 
    \refnumbr#1\let\citea\citepunct}%
%
% \sortcelt inserts number (\refnumbr) into list of \celt{num} (#1{#2})
% \celt must not be expandable; list should end with two vanishing tokens.
%
\def\sortcelt#1#2{%
    \ifx \celt #1% parameters are \celt{num}
       \ifnum #2<\refnumbr % number goes later in list
          \celt{#2}%
          \expandafter\expandafter\expandafter\sortcelt % continue
       \else % number goes here
          \celt{\number\refnumbr}\celt{#2}% stop comparing
       \fi
    \fi}%
%
%    To put space after the comma, use:
%
% \def\citepunct{,\hskip.13emplus.1emminus.1em}%
%
%    For no space after comma, use:
%
  \def\citepunct{,}%
%
% The following is defined in ezjnl:
%
%\def\gobble#1{}% eat up argument.
% %%%%%%%%%%%%%%%% from latex %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove comments if REFORDER is used stand-alone.
% % \for NAME := LIST \do {BODY}
% %
% \def\@nnil{\@nil}%
% \def\@fornoop#1\@@#2#3{}%
% %
% \def\for#1:=#2\do#3{\edef\@fortmp{#2}\ifx\@fortmp\empty \else
%     \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}\fi}%
% %
% \def\@forloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
%        #5\def#4{#2}\ifx #4\@nnil \else#5\@iforloop #3\@@#4{#5}\fi\fi}%
% %
% \def\@iforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
%        \let\@nextwhile=\@fornoop \else
%       #4\relax\let\@nextwhile=\@iforloop\fi\@nextwhile#2\@@#3{#4}}%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define entries in list of references
%
\let\r@fis \refis                      % Save old \refis, redefine
\def\refis#1#2#3\par{%                 % Use two params #2 #3 to strip blank
    \expandafter\ifx\csname r@fnum#1\endcsname\relax%
      \global\advance\r@fcount \@ne \global\advance\r@funused \@ne
      \message{Skipping unused ref #1 = \number\r@fcount.}%
    \else
      \expandafter\gdef\csname r@ftext\csname r@fnum#1\endcsname\endcsname
      {\writer@f#1>>#2#3}%
    \fi}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% output sorted list of references
%
\def\r@ferr{\endreferences\errmessage{I was expecting to see
\noexpand\endreferences before now;  I have inserted it here.}}%
%
\let\r@ferences \references            % Save old \references, redefine
\def\references{\r@ferences
    \let\r@ndmode\endmode
    \def\endmode{\r@ndmode\r@ferr}}%
%
\let\endr@ferences \endreferences      % Save old \endreferences, redefine
\def\endreferences{\r@fcurr 0\advance\r@fcount -\r@funused
   {\loop\ifnum\r@fcurr<\r@fcount      % Loop over refnum and produce text
    \advance\r@fcurr\@ne\relax
    \expandafter\r@fis\expandafter{\number\r@fcurr}%
    \csname r@ftext\number\r@fcurr\endcsname
    \repeat}\gdef\r@ferr{}\endr@ferences}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save old \endpaper, redefine it to write parting message.
%
\let\r@fend \endpaper
\def\endpaper{%
    \ifr@ffile
       \write\reffile{}% Make last line a blank line.
       \message{List of References written on \jobname.ref.}%
    \fi
    \r@fend}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% make macros known to reforder
%
\def\citeall#1{\xdef#1##1{#1{\noexpand\cite{##1}}}}%
%
% \citeall\refto      % Uncomment if used with JNL or PLAIN.
% \citeall\ref        % These macros will generate citations
% \citeall\Ref        %
%
% For convenience:
\let \affilfile \referencefile
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Obsolete commands:
%\def\refbunch#1{\bparse #1.}%
%\def\bparse#1+#2.{\refto{#1,#2}}%
%\def\refc{$^{,}$}%
%\def\citebunch#1{{\cparse #1.}}% double {{ }} so \citebunch can be in $^..$
%\def\cparse#1+#2.{\cite{#1,#2}}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\catcode`\@ \reforderloaded % restore catcode
\let\reforderloaded\endinput
%% END of REFORDER.EZ 13-Apr-1993 19:03:35
