%% $Id: abntcite.sty,v 1.13 2002/11/25 09:46:11 mvsfrasson Exp $
%% name of this file abntcite.sty
%% Copyright 2001-2002 by the abnTeX group at http://abntex.codigolivre.org.br
%%
%% This file is distributed under the LaTeX-Project Public License (LPPL)
%%            http://www.latex-project.org/lppl.html 
%% You are free to modify this file under the LPPL.


% How this package is organized:
% ------------------------------
% - Implementing `search for substring'
% - \AfterTheBibliography{} hook
% - Options declaration (search for substring needed here)
%     alf, num (default)  
%     alfantigo, experimental (default)
%     biblabel-on-margin
%     sem-bibtex (apply settings without count with code in .bbl)
%     no-abnt-option-file (in case one defines its own options file)
%     recuo (then makes bibitem indent 1.8em) (default: NO indentation)
%     recuo=<length> (sets custom indentation)
%     indent   is an alias for recuo (indent or indent=<length>)
%     other options are considered "\citeoption's" (mechanism includes a
%       list that stores non defined options, and in \AtBeginDocument,
%       \citeoption acts over this list)
% - allocing auxiliar variables
% - \citeoption    
%     Similar to \nocite, but does not complain about obviously not defined
%     citations. Intented to access options of abnTeX's bibtex styles.
% - \bibliography
%     extra care to auto include abnt-options as bibliography database (if
%     needed)
% - \bibliographystyle
%     if none is given, the style correspondent to the package style in
%     use (alf or num) is given.
% - Style customization
%     \authorcapstyle, \authorstyle, \yearstyle, \optionaltextstyle
%     (detail: \authorcapstyle's default setting uses \smaller from relsize
%     package, but only if package is present.)
% - \bibitem
%     defines action of \bibitem by its subcommands. Comatibility with old
%     abnt-alf is present.
% - \abntrefinfo
%     From the creation of abntcite, options are present as a separated
%     command. This enables compatibility with any bibtex style.
%
% - \cite
%     This command depends on if the style is alf or num
%     If alf, then \cite produces (<explicit (capital)>, <year>)
%     If num, then \cite produces a superscript with [<implicit (lowercase)>]
% - citeonline
%     This command depends on if the style is alf or num
%     If alf, \citeonline produces <implicit (lowercase)>, (<year>)
%     If num, \citeonline then produces <implicit (lowercase)>
% - citeyear
%     Produces <year>
% - citeauthor
%     Produces <explicit (capital) author>
% - citeauthoronline
%     Produces <implicit (lowercase) author>
%
% - implementing `recuo' (bibindent style)
% - Macros for output sorted and compressed numerical citations
%     [9,2,3,1,4,11,7,10] becames [1--4,7,9--11]


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{abntcite}[2002/11/20]

\RequirePackage{ifthen,calc}


%%%%%%  Options Declaration  %%%%%%


%----------------------------------------------------------------
%    Extracted from substr.sty (2000-06-15) 
%----------------------------------------------------------------
% Copyright 2000 Harald Harders <h.harders@tu-bs.de>
% Look at www.ctan.org for original package
%
% Commands:
%
% \IfSubStringInString{substring}{string}{true part}{false part}
% This command searches <substring> in <string> and executes the 
% <true part> if it is and else the <else part>

% expands the first and second argument with
% \protected@edef and calls #3 with them:
\newcommand{\su@ExpandTwoArgs}[3]{%
  \protected@edef\su@SubString{#1}%
  \protected@edef\su@String{#2}%
  \expandafter\expandafter\expandafter#3%
  \expandafter\expandafter\expandafter{%
    \expandafter\su@SubString\expandafter
  }\expandafter{\su@String}%
}
%%
%% tests if #1 in #2. If yes execute #3, else #4
\newcommand*{\IfSubStringInString}[2]{%
  \su@ExpandTwoArgs{#1}{#2}\su@IfSubStringInString
}
%%
\newcommand*{\su@IfSubStringInString}[2]{%
  \def\su@compare##1#1##2\@nil{%
    \def\su@param{##2}%
    \ifx\su@param\@empty
      \expandafter\@secondoftwo
    \else
      \expandafter\@firstoftwo
    \fi
  }%
  \su@compare#2#1\@nil
}

%----------------------------------------------------------------


% alf, num: main options
\DeclareOption{alf}{\def\AbntCitetype{alf}}
\DeclareOption{num}{\def\AbntCitetype{num}}
\ExecuteOptions{num}

% compatibility with old abnt-alf.bst
\newboolean{ABCIcompoldalf}
\DeclareOption{alfantigo}{\setboolean{ABCIcompoldalf}{true}}
\DeclareOption{experimental}{\setboolean{ABCIcompoldalf}{false}}
\ExecuteOptions{experimental}

% biblabel-on-margin
\newboolean{ABCIbibjustif}
\DeclareOption{bibjustif}{\setboolean{ABCIbibjustif}{true}}
\DeclareOption{bibleftalign}{\setboolean{ABCIbibjustif}{false}}
\ExecuteOptions{bibleftalign}

% biblabel-on-margin
\newboolean{ABCIbiblabelonmargin}
\DeclareOption{biblabel-on-margin}
               {\setboolean{ABCIbiblabelonmargin}{true}}
\DeclareOption{biblabel-not-on-margin}
               {\setboolean{ABCIbiblabelonmargin}{false}}
\ExecuteOptions{biblabel-not-on-margin}

% \bibliography includes abnt-options automatically unless next option is used 
\newboolean{ABCIauto-abntoptions}
\DeclareOption{abnt-option-file}{\setboolean{ABCIauto-abntoptions}{true}}
\DeclareOption{no-abnt-option-file}{\setboolean{ABCIauto-abntoptions}{false}}
\ExecuteOptions{abnt-option-file}

% recuo : compatibility with old norms
\setlength{\bibindent}{0em}
\DeclareOption{recuo}{\setlength{\bibindent}{1.8em}}

% indent : an alias to recuo
\DeclareOption{indent}{\setlength{\bibindent}{1.8em}}


% 
%  Allocing variables
%

% used for \@biblabel in num
\newlength{\minimumbiblabelwidth}
\newlength{\ABCIauxlen}

% auxiliar counters used in `sort and group' mechanism
\newcounter{ABCIaux}
\newcounter{ABCImax}

% Here I define a LaTeX list that will contain all options not defined
% before. 
%
% A LaTeX list is a command containing a comma separated list of
% itens. The list is processed by command \@for, as in \citeoption.
\def\citeoptionlist{}
% This command adds a string to the list.
\newcommand{\addtociteoptionlist}[1]{
  \ifthenelse{\equal{\citeoptionlist}{}}
    {\edef\citeoptionlist{#1}}
    {\edef\citeoptionlist{\citeoptionlist,#1}}
}


% suporting options in a keyval style
% recuo=<length> gives indentation of \bibitem
% other options: passed thought \citeoption (stored now in \citeoptionlist
%   and after (in \AtBeginDocument hook) \citeoption will act on it)

% To grab 6 letters. (recuo=)
\newcommand{\grabsix}[6]{}

% To grab 7 letters. (indent=)
\newcommand{\grabseven}[7]{}

\DeclareOption*{
  \IfSubStringInString{recuo=}{\CurrentOption}
     {\setlength{\bibindent}{\expandafter\grabsix\CurrentOption}}
     {\IfSubStringInString{indent=}{\CurrentOption}
        {\setlength{\bibindent}{\expandafter\grabseven\CurrentOption}}
        {\addtociteoptionlist{\CurrentOption}}}
}

\ProcessOptions

% Now, after all options are processed, \citeoption process the list of
% options given as package options.
%
\ifthenelse{\equal{\citeoptionlist}{}}
  {}
  {\AtBeginDocument{\citeoption{\citeoptionlist}}}


% \citeoption - equal to \nocite  (I copied source from \nocite) 
%               but not complains about this option be obviously undefined
\newboolean{ABCIciteoptionwasused}
\setboolean{ABCIciteoptionwasused}{false}
\def\citeoption#1{\@bsphack
  \@for\@citekey:=#1\do{%
    % next line eliminates white space before citation name
    \edef\@citekey{\expandafter\@firstofone\@citekey}%
    \if@filesw\immediate\write\@auxout{\string\citation{\@citekey}}\fi
  }%
  \@esphack%
  \setboolean{ABCIciteoptionwasused}{true}%
}


%
% \bibliography{list of files .bib to be processed by bibTeX}
%
%   I had to redefine this command such that abnt-options.bib is
%   automatically used in case that some \citeoption was given as package
%   option. An entry
%      \bibliography{abnt-options,abnt-options,..}
%   produces an error in bibTeX. So I had to check if user already
%   includes abnt-options by (him/her)self.
%
%   implementation:
%
%     \citeoption was used?
%     If YES, (abnt-options must be given to bibtex!)
%       checks for abnt-options into parameter given to \bibliography
%       if present
%          user have included, so I don't add it once more
%       not present
%          I add necessary abnt-options
%     If NO
%       no changes to \bibliography

\def\bibliography#1{%
  \ifthenelse{\boolean{ABCIciteoptionwasused}
               \and\boolean{ABCIauto-abntoptions}}
   {
   % this system of checking is not good...
    \IfSubStringInString{abnt-options}{#1}
      {\if@filesw\immediate\write\@auxout{\string\bibdata{#1}}\fi}
      {\if@filesw\immediate\write\@auxout{\string\bibdata{abnt-options,#1}}\fi}
   }
   {
    \if@filesw
       \immediate\write\@auxout{\string\bibdata{#1}}%
    \fi
   }
 \@input@{\jobname.bbl}}


%
% The next piece of code looks if bibtexstyle is used or not, and if not,
% includes the respective style from the abntcite mode (alf or num)

\newboolean{ABCIbibtexstyleused}
\setboolean{ABCIbibtexstyleused}{false}

%
\def\bibliographystyle#1{%
  \ifx\@begindocumenthook\@undefined\else
    \expandafter\AtBeginDocument
  \fi
    {\if@filesw
       \immediate\write\@auxout{\string\bibstyle{#1}}%
     \fi}
  \setboolean{ABCIbibtexstyleused}{true}
  }

% \AtEndDocument is the last chace for include bibliography style..
\AtEndDocument{
  \ifthenelse{\not\boolean{ABCIbibtexstyleused}}
    {\bibliographystyle{abnt-\AbntCitetype}}
    {}
}


% relsize used in \authorcapstyle. If package not present, \smaller=\relax
\IfFileExists{relsize.sty}
  {\RequirePackage{relsize}}
  {\let\smaller\relax}

%%%%%%%%%%%%%% Style %%%%%%%%%%%%%%%%%



\newcommand{\authorcapstyle}{\smaller}
\newcommand{\authorstyle}{\relax}
\newcommand{\yearstyle}{\relax}
\newcommand{\optionaltextstyle}{\relax}
\newcommand{\citenumstyle}{\large}

\newlength{\biblabelsep}
\setlength{\biblabelsep}{1ex}

%%%%%%%%%%%%%%%%%%%%%  Implementation  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% automatic care for commas inside references
\providecommand{\ABCIccomma}{}
\newcommand{\ABCIcitecommadefault}{,\penalty\@m\ }
\newcommand{\ABCIcitecomma}{\ABCIccomma\let\ABCIccomma\ABCIcitecommadefault}
\newcommand{\ABCIinitcitecomma}{\def\ABCIccomma{}}
\newcommand{\ABCIcitecolondefault}{; }%{;\penalty\@m\ }


% How \bibitem works? 
%   definition of \bibitem
%   \def\bibitem{\@ifnextchar[\@lbibitem\@bibitem}

\ifthenelse{\boolean{ABCIcompoldalf}}
{

% In the old mplementation on abnt-alf, the command \bibitem should be 
%   \bibitem[{explicit}{implicit}{year}]{key}

  % if \bibitem has optional parameter, uses it (three pieces of data
  % stored into .aux file)

  \def\@lbibitem[#1#2#3]#4{
    \gdef\abntnextkey{#4}
    \item[\@biblabel{#1}\hfill]%
    \if@filesw
       {
        \let\protect\noexpand
        \immediate\write\@auxout{\string\bibcite{#4++EXPL}{#1}}
        \immediate\write\@auxout{\string\bibcite{#4++IMPL}{#2}}
        \immediate\write\@auxout{\string\bibcite{#4++YEAR}{#3}}
       }
    \fi%
    \ignorespaces
  }

  % if no optional is used, uses a list counter (stores its value into .aux)
  \def\@bibitem#1{
    \gdef\abntnextkey{#1}
    \item%
    \if@filesw
       {\immediate\write\@auxout{\string\bibcite{#1}{\the\value{\@listctr}}}}
    \fi%
    \ignorespaces
  }

}
{ %if normal mode (non-compatible with old alf)

  \def\@lbibitem[#1]#2{
    \gdef\abntnextkey{#2}
    \item[\@biblabel{#1}\hfill]%
    \if@filesw
       {
        \let\protect\noexpand
        \immediate\write\@auxout{\string\bibcite{#2}{#1}}
       }
    \fi%
    \ignorespaces
  }

  \def\@bibitem#1{
    \gdef\abntnextkey{#1}
%    \ifthenelse{}{\item}{\item}
    \item%
    \if@filesw
       {\immediate\write\@auxout{\string\bibcite{#1}{\the\value{\@listctr}}}}
    \fi%
    \ignorespaces
  }

}

\newcommand{\abntrefinfo}[3]{
  \if@filesw
    {
     \let\protect\noexpand
     \immediate\write\@auxout{\string\bibcite{\abntnextkey++EXPL}{#1}}
     \immediate\write\@auxout{\string\bibcite{\abntnextkey++IMPL}{#2}}
     \immediate\write\@auxout{\string\bibcite{\abntnextkey++YEAR}{#3}}
    }
  \fi
}



\def\AbntCitetypeALF{alf}


%%%%%%%%%%%%%%%%%%%%  abnt-alf  %%%%%%%%%%%%%%%%%%%%%%%

% the next line was before \ifthenelse{\equal{\AbntCitetype}{alf}}


\ifx\AbntCitetype\AbntCitetypeALF % if alf

  \newcommand{\citeopen}{(}
  \newcommand{\citeclose}{)}

  
  \renewcommand{\@biblabel}[1]{}
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  %               \cite[optional]{list of keys}
  %
  %  Implicit citation - author in capital letters and year; 
  %                       mechanism to deal with repeated names;
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  \renewcommand{\@cite}[2]%
    {\citeopen{#1\ifthenelse{\equal{#2}{}}{}{, 
          {\optionaltextstyle#2}}}\citeclose}
  
  \DeclareRobustCommand{\cite}[2][]{%
  %
    \let\@citeprevious\@empty%
    \@cite{%
      \@for\@citekey:=#2\do%  \@citekey contains the key
        {%
         \edef\@citekey{\expandafter\@firstofone\@citekey\@empty}%
         %
         % here, a tricky \let\@citename\"b@\@citekey++UP"
         \expandafter\let\expandafter\@citename%
             \csname b@\@citekey++IMPL\endcsname%
         \expandafter\let\expandafter\@citeyear%
             \csname b@\@citekey++YEAR\endcsname%
         \ifx\@citeprevious\@empty%
         \else%
           \ifx\@citename\@citeprevious%
              \ABCIcitecommadefault% echoing ", "
           \else%
              \ABCIcitecolondefault% echoing "; "
           \fi%
         \fi%
         % writing on aux file
         \if@filesw\immediate\write\@auxout{\string\citation{\@citekey}}\fi%
         % if undefined bibitem
         \@ifundefined{b@\@citekey}%
           {% if undefined
               {\bf?}% echoing ?
            \G@refundefinedtrue%
            \@latex@warning%
              {Citation `\@citekey' on page \thepage \space undefined}%
           }%
           {% if already defined
             \ifx\@citename\@citeprevious%
               {\yearstyle\@citeyear\@}\@%
             \else%
               {\authorcapstyle\@citename}, %
               {\yearstyle\@citeyear\@}%
             \fi%
             \let\@citeprevious\@citename%
           }%
        }% end for
    }{#1}%
  }
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  %             \citeonline[optional]{list of keys}
  %
  %  Inline (explicit) citation - author in "inline style" and year; 
  %                               mechanism to deal with repeated names;
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  \newcommand{\@citeonline}[2]%
    {{#1\ifthenelse{\equal{#2}{}}{}{, {\optionaltextstyle#2}}}\citeclose}
  
  \DeclareRobustCommand{\citeonline}[2][]{%
  %
    \leavevmode
    \let\@citeprevious\@empty%
    \@citeonline{%
      \@for\@citekey:=#2\do%\@citekey contains the key
        {%
         \edef\@citekey{\expandafter\@firstofone\@citekey\@empty}%
         %
         % here, a tricky \let\@citename\"b@\@citekey++UP" :-)
         \expandafter\let\expandafter\@citename%
             \csname b@\@citekey++EXPL\endcsname%
         \expandafter\let\expandafter\@citeyear%
             \csname b@\@citekey++YEAR\endcsname%
         \ifx\@citeprevious\@empty%
         \else%
           \ifx\@citename\@citeprevious%       
             \ABCIcitecommadefault% echoing ", "
           \else%
             \citeclose, % echoing "), "
           \fi%   
         \fi%
         % writing in aux file
         \if@filesw\immediate\write\@auxout{\string\citation{\@citekey}}\fi%
         \@ifundefined{b@\@citekey}%
           {% if undefined
            {\bf?}% echoing ?
            \G@refundefinedtrue%
            \@latex@warning%
              {Citation `\@citekey' on page \thepage \space undefined}%
           }%
           {% if really defined
            \ifx\@citename\@citeprevious%
            \else%
               {\authorstyle\@citename\@{}} \citeopen%
            \fi%
            {\yearstyle\@citeyear\@}%
            \let\@citeprevious\@citename%
           }%
        }% end for
    }{#1}%
  }
  
  % "overcite" compatibility
  \let\citen\citeonline
  \let\citenum\citeonline

% end alf
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%  abnt-num  %%%%%%%%%%%%%%%%%%%%%%%%%%
%
%

\else % if not alf

  \ifthenelse{\equal{\AbntCitetype}{num}}
  {

     % this only works with proper settings in \ABCIthebibliformat
     \ifthenelse{\boolean{ABCIbiblabelonmargin}}
     {
     \renewcommand{\@biblabel}[1]%
        {\ifthenelse{\equal{#1}{}}{}{{\citenumstyle #1\hspace{\biblabelsep}}}}
     }
     {
     \renewcommand{\@biblabel}[1]%
        {%
         \ifthenelse{\equal{#1}{}}
           {}
           {%
            \def\biblabeltext{{\citenumstyle #1\hspace{\biblabelsep}}}%
            \settowidth{\ABCIauxlen}{\biblabeltext}%
            \ifthenelse{\lengthtest{\ABCIauxlen<\minimumbiblabelwidth}}
               {\setlength{\ABCIauxlen}{\minimumbiblabelwidth-\ABCIauxlen}}
               {\setlength{\ABCIauxlen}{0cm}}%
            {\biblabeltext\hspace{\ABCIauxlen}}%   
           }%
        }%
     }

     \newcommand{\citeopen}{[}
     \newcommand{\citeclose}{]}
     \let\ABCIscriptfont\scriptsize

     \def\ABCI@cite#1#2{\raisebox{1.4ex}%
         {\ABCIscriptfont\citeopen{#1\if@tempswa, #2\fi}\citeclose}}

     \def\ABCI@citeonline#1#2{{#1\if@tempswa, #2\fi}}

     \DeclareRobustCommand\cite{%
        \let\@cite\ABCI@cite%
        \@ifnextchar[{\@tempswatrue\@citex}{\@tempswafalse\@citex[]}} 

     \DeclareRobustCommand\citeonline{%
        \leavevmode\let\@cite\ABCI@citeonline%
        \@ifnextchar[{\@tempswatrue\@citex}{\@tempswafalse\@citex[]}}

     \renewcommand{\@openbib@code}%
     {\setlength{\leftmargin}{0pt}\addtolength{\itemindent}{\labelsep}}


     \def\@citex[#1]#2{%
       \def\@safe@activesfalse{}%
       \ABCIinitcitecomma%
       \def\ABCIcitelist{}%
       \@cite{%
         \@for\@citeb:=#2\do%
            {%
             \edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
             \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi%
             \@ifundefined{b@\@citeb}%
                {%
                 \mbox{\reset@font\bfseries ?}%
                 \G@refundefinedtrue%
                 \@latex@warning%
                   {Citation `\@citeb' on page \thepage \space undefined}%
                }%
                {%
                 \expandafter\let\expandafter\ABCIcitation%
                               \csname b@\@citeb\endcsname%
                 % test for positive number
                 \ifcat _\ifnum\z@<0\ABCIcitation _\else A\fi
                    \ABCIaddtocitelist{\ABCIcitation}%
                 \else% else \ifcat
                    \ABCIcitecomma\hbox{\ABCIcitation}%
                 \fi% end \ifcat
                }%
            }%
         \ABCIoutputgroupedcitelist%
       }{#1}%
     }

   \newcommand{\ABCIaddtocitelist}[1]{%
     \ifthenelse{\equal{\ABCIcitelist}{}}%
       {\edef\ABCIcitelist{#1}}
       {\edef\ABCIcitelist{\ABCIcitelist,#1}}%
   }%

  }{}
  
\fi % end if alf


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                \citeyear{list of keys}
%
%  Citation by year 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareRobustCommand{\citeyear}[1]{%
%
%  \let\@safe@activesfalse\@empty%
  \let\@citeprevious\@empty%
  \@for\@citekey:=#1\do %\@citekey contains the key
    {%
%     \edef\@citekey{\expandafter\@firstofone\@citekey\@empty}%     
     \expandafter\let\expandafter\@citename%
          \csname b@\@citekey++YEAR\endcsname%
     %compare previous name with current name and decide the punctuation marks
     %if first use do nothing
     \ifx\@citeprevious\@empty%
     \else%
        , % echoing ", 
     \fi%
     % writing on aux file
     \if@filesw\immediate\write\@auxout{\string\citation{\@citekey}}\fi%
     % if undefined bibitem
     \@ifundefined{b@\@citekey}%
       {% if undefined
        {\bf?}% echoing ?
        \G@refundefinedtrue%
        \@latex@warning%
          {Citation `\@citekey' on page \thepage \space undefined}%
       }%
       {% if already defined
         \ifx\@citename\@citeprevious%
         \else%
           {\@citename\@}%
         \fi%
         \let\@citeprevious\@citename%
       }%
    }% end for
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%               \citeauthor{list of keys}
%
%  Implicit citation (capital letters)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



\DeclareRobustCommand{\citeauthor}[1]{%
%
%  \let\@safe@activesfalse\@empty%
  \let\@citeprevious\@empty%
  \@for\@citekey:=#1\do %\@citekey contains the key
    {%
%     \edef\@citekey{\expandafter\@firstofone\@citekey\@empty}%
     \expandafter\let\expandafter\@citename\csname b@\@citekey++IMPL\endcsname%
     % writing on aux file
     \if@filesw\immediate\write\@auxout{\string\citation{\@citekey}}\fi%
     % if undefined bibitem
     \@ifundefined{b@\@citekey}%
       {% if undefined
        {\bf?}%
        \G@refundefinedtrue%
        \@latex@warning%
          {Citation `\@citekey' on page \thepage \space undefined}%
       }%
       {% if already defined
         \ifx\@citename\@citeprevious%
         \else%
            \ifx\@citeprevious\@empty%
            \else%
              ; % echoing "; "
            \fi%
            {\authorcapstyle\@citename\@}%
         \fi%
         \let\@citeprevious\@citename%
       }%
    }% end for
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%             \citeauthoronline{list of keys}
%
%  Explicit citation (non capital letter)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\DeclareRobustCommand{\citeauthoronline}[1]{%
%
%  \let\@safe@activesfalse\@empty%
  \let\@citeprevious\@empty%
  \@for\@citekey:=#1\do %\@citekey contains the key
    {%
%     \edef\@citekey{\expandafter\@firstofone\@citekey\@empty}%
     \expandafter\let\expandafter\@citename\csname b@\@citekey++EXPL\endcsname%
     % writing on aux file
     \if@filesw\immediate\write\@auxout{\string\citation{\@citekey}}\fi%
     % if undefined bibitem
     \@ifundefined{b@\@citekey}%
       {% if undefined
        {\bf?}%
        \G@refundefinedtrue%
        \@latex@warning%
          {Citation `\@citekey' on page \thepage \space undefined}%
       }%
       {% if already defined
         \ifx\@citename\@citeprevious%
         \else%
            \ifx\@citeprevious\@empty%
            \else%
              ; % echoing "; "
            \fi%
            {\authorstyle\@citename\@}%
         \fi%
         \let\@citeprevious\@citename%
       }%
    }% end for
}



%
% Setting up `recuo' (=indent in Portuguese)
%

% To ensure that the margin settings of this package works, \leftmargin
% (inside thebibliography) must be 0cm. \@openbib@code is a hook inside
% thebibliography.
\let\@openbib@codeBACKUP\@openbib@code
\def\@openbib@code{\@openbib@codeBACKUP\setlength{\leftmargin}{0cm}}


% adding things after \begin{thebibliography}{}

\let\thebibliographyBkUp\thebibliography
\def\thebibliography#1{\thebibliographyBkUp{#1}\ABCIthebibliformat}

%
% \AfterTheBibliography{} hook
%

\newtoks\ABCItoken

\newcommand{\ABCIthebiblihook}{}

\newcommand{\AfterTheBibliography}[1]{
  \ABCItoken=\expandafter{\ABCIthebiblihook}
  \expandafter\def\expandafter\ABCIthebiblihook\expandafter{\the\ABCItoken#1}
}


\ifthenelse{\boolean{ABCIbibjustif}}
  {}
  {\AfterTheBibliography{\setlength{\rightskip}{0pt plus 2cm}}}

\ifthenelse{\equal{\AbntCitetype}{alf}}
  {% mode alf
   \newcommand{\ABCIthebibliformat}{
     \setlength{\leftskip}{\bibindent}
     \setlength{\labelwidth}{0cm}
     \setlength{\labelsep}{0cm}
     \setlength{\itemindent}{-\bibindent}
     \setlength{\listparindent}{0cm}
     \ABCIthebiblihook
     }     
  }
  {% mode num
   \ifthenelse{\boolean{ABCIbiblabelonmargin}}
     {
       \newcommand{\ABCIthebibliformat}{
         \setlength{\leftskip}{\bibindent}
         \setlength{\labelwidth}{10cm}
         \setlength{\labelsep}{0cm}
         \setlength{\itemindent}{-\bibindent}
         \setlength{\listparindent}{0cm}
         \setlength{\biblabelsep}{1.5ex}
         \ABCIthebiblihook
       }
     }
     {
       \newcommand{\ABCIthebibliformat}{
         \setlength{\leftskip}{\bibindent}
         \setlength{\labelwidth}{0cm}
         \setlength{\labelsep}{0cm}
         \setlength{\itemindent}{-\bibindent}
         \setlength{\listparindent}{0cm}
         \setlength{\minimumbiblabelwidth}{\leftskip}
         \ABCIthebiblihook
         }
         \AfterTheBibliography{
           \ifthenelse{\lengthtest{\minimumbiblabelwidth=0pt}}
             {\setlength{\biblabelsep}{1.8ex}}
             {\setlength{\biblabelsep}{1ex}}
         }
     }   
  }




%---------------------------------------------------------------%
%  Macros for output sorted and compressed numerical citations  %
%---------------------------------------------------------------%


% Store to maximum of a given list to the counter "ABCImax"
\newcommand{\maximuminlist}[1]{%
  \setcounter{ABCImax}{0}%
  \@for\ABCIaux:=#1\do%
     {%
      \setcounter{ABCIaux}{\ABCIaux}%
      \ifnum \value{ABCIaux}>\value{ABCImax}%
         \setcounter{ABCImax}{\value{ABCIaux}}%
      \fi%
     }%
}

% It takes a command containing a comma separeted list of
% positive numbers and redefines the original command with
% the list without its maximum element.
\newcommand{\ABCIlistwithoutmaximum}[1]{%
  \maximuminlist{#1}%
  \let\ABCItemplist#1%
  \def#1{}%
  \def\ABCIcomma{}%
  \@for\ABCIaux:=\ABCItemplist\do%
    {%
      \ifnum\ABCIaux<\value{ABCImax}%
        \edef#1{#1\ABCIcomma\ABCIaux}%
        \def\ABCIcomma{,}%
      \fi%
    }%
}

% It requires a command containing a comma separeted list of
% positive numbers. It redefines that command with the same
% list, but sorted.
\newcommand{\ABCIsortlist}[1]{%
  \edef\ABCItempslist{#1}%
  \def#1{}%
  \def\ABCIcomma{}%
  \whiledo{\not\equal{\ABCItempslist}{}}%
    {%
     \maximuminlist{\ABCItempslist}%
     \edef#1{\arabic{ABCImax}\ABCIcomma#1}%
     \def\ABCIcomma{,}%
     \ABCIlistwithoutmaximum{\ABCItempslist}%
    }%
}

% \ifconsecutive{num1}{num2}{block1}{block2}
% executes block1 if num1 = num2+1 and block2 otherwise.
\newcommand{\ifconsecutive}[4]{%
  \@tempcnta#1%
  \@tempcntb#2%
  \advance\@tempcntb 1 %
  \ifnum\@tempcnta=\@tempcntb#3\else#4\fi%
}

%
% It sorts and outputs in grouped way the list \ABCIcitelist generated in
% \cite when in `num' mode.
%
\newcommand{\ABCIoutputgroupedcitelist}{%
  \ifthenelse{\equal{\ABCIcitelist}{}}%
    {}%
    {%
     \ABCIsortlist{\ABCIcitelist}%
     \setcounter{ABCIaux}{0}%
     \@for \ABCIaux:=\ABCIcitelist\do%
       {%
        \ifthenelse{\value{ABCIaux}=0}%
          {%
           \let\ABCIfirst\ABCIaux%
           \let\ABCIlast\ABCIaux%
           \setcounter{ABCIaux}{1}%
          }%
          {%
           \ifconsecutive{\ABCIaux}{\ABCIlast}%
             {%
              \let\ABCIlast\ABCIaux%
              \addtocounter{ABCIaux}{1}%
             }%
             {%
              \ifthenelse{\value{ABCIaux}=1}%
                {%
                 \ABCIcitecomma\ABCIfirst%
                }%
                {%
                 \ifthenelse{\value{ABCIaux}=2}%
                   {%
                    \ABCIcitecomma\ABCIfirst%
                    \ABCIcitecomma\ABCIlast%
                   }%
                   {%
                    \ABCIcitecomma\ABCIfirst--\ABCIlast%
                   }%
                }%
              \let\ABCIfirst\ABCIaux%
              \let\ABCIlast\ABCIaux%
              \setcounter{ABCIaux}{1}%
             }%
          }%
       }%
     \ifthenelse{\value{ABCIaux}=1}%
       {%
        \ABCIcitecomma\ABCIfirst%
       }%
       {%
        \ifthenelse{\value{ABCIaux}=2}%
          {%
           \ABCIcitecomma\ABCIfirst%
           \ABCIcitecomma\ABCIlast%
          }%
          {%
           \ABCIcitecomma\ABCIfirst--\ABCIlast%
          }%
       }%
     }%
}

\endinput



