%% BIBTOPICS.STY, a  package following from the
%% BIBUNITS.STY	by Jose Alberto Fernandez (alberto@cs.umd.edu)
%%
%% BIBTOPICS.STY is a style to generate a single bibliography
%% made up of several topics. For instance: books, procedings
%% articles, thesis, .. However we want the bibitems alphabetically 
%% sorted by authors but a single numeration 
%% for all this bibliography. This is the main difference with
%% BIBUNITS.STY, because at the beginning of each unit, corresponding 
%% to a new topic, the counter of the bibitems is not reinitialized.
%%
%%
%% The environment \begin{bibunit}[<style>] .. \end{bibunit} allows 
%% the creation of a unit. The 
%% optional parameter <style> specifies a style for the bibliography 
%% different from the default if any. 
%%
%% The \bibliographyunit  command has been removed because not useful.
%%
%% Use \cite and \nocite to generate citations. \cite* and \nocite* 
%% can be use to generate citations to be added in the local 
%% bibliography  inside a unit. 
%%
%% You must insert the command \putbib[<bibtex-files>] before the end 
%% of each <unit> in the location where you want the bibliography to 
%% be inserted. If the optional argument is ommited, \putbib uses the 
%% default bibtex-files if any.
%%
%% BIBTEX processing:
%% For each <unit>, in sequence, there is now a corresponding file 
%% \jobname.\the\@bibunitauxcnt.aux that need to be compiled though 
%% BIBTEX as well as the \jobname.aux that generates the global 
%% database.
%%
%% The global bibliography is given in a \bibliography command.
%% In the same way, a global \bibliographystyle command.
%% Both are needed for the creation of the \jobname.aux file
%% But the \bibliography command is modified so that the file 
%% \jobname.bbl is not read.
%%
%% Suppose your document is foo.tex, you must now invoke bibtex not only 
%% on foo, but also on foo.1, ..., foo.n given that there are n 
%% different <units> with separate bibliography. 
%%
%% Pierre Basso
%% Laboratoire Informatique de Marseille - CNRS URA 1787
%% email: basso@lim.univ-mrs.fr

\newwrite\@bibunitaux	%File desc.
\newcount\@bibunitauxcnt \@bibunitauxcnt = 0	%Units cnt.
\newif\if@localcite	%Local/Global cite
\newif\if@globalbiblio %%%%%% 
\@globalbibliotrue     %%%%%%%
\def\@localcitedflt{\@localcitetrue} \@localcitedflt 
%
% \cite definition
%
%\let\REALcite\cite% --bg
\def\NEWcite{
\@ifstar{\@localcitefalse\REALcite}
{\@localcitetrue\REALcite}}
%
% \@cite new definition
\let\REAL@citex\@citex
\def\NEW@citex[#1]#2{\if@filesw
\immediate\write\@bibunitaux{\string\citation{#2}}\fi \if@localcite
\if@filesw
\@fileswfalse\REAL@citex[#1]{#2}\@fileswtrue \else\REAL@citex[#1]{#2}\fi
\else\REAL@citex[#1]{#2}\fi
\@localcitedflt}
%
% \nocite definition
%
%\let\REALnocite\nocite% --bg
\def\NEWnocite{
\@ifstar{\@localcitefalse\@NEWnocite}
{\@localcitetrue\@NEWnocite}}
\def\@NEWnocite#1{\@bsphack
\if@filesw
\immediate\write\@bibunitaux{\string\citation{#1}}\fi 
\@esphack
\if@localcite\else\REALnocite{#1}\fi\@localcitedflt} 
%
% \bibliography definition
%
\def\bibliography#1{%
  \if@filesw
    \immediate\write\@auxout{\string\bibdata{#1}}%
  \fi
    \if@globalbiblio\null
    \else\@input@{\jobname.bbl}\fi}
\def\@bibdata{}	%Data files store
\let\REALbibliography\bibliography
\def\NEWbibliography{
\@ifstar{\@localcitetrue\@NEWbibliography} 
{\@localcitefalse\@NEWbibliography}}
\def\@NEWbibliography#1{
\if@filesw
\immediate\write\@auxout{\string\gdef\string\@bibdata{#1}}\fi 
\if@localcite\else\REALbibliography{#1}\fi\@localcitedflt} 
%
% \bibliographystyle definition
%
\def\@bibstyle{}	%Style store
\let\REALbibliographystyle\bibliographystyle 
\def\NEWbibliographystyle{
\@ifstar{\@localcitetrue\@NEWbibliographystyle} 
{\@localcitefalse\@NEWbibliographystyle}} 
\def\@NEWbibliographystyle#1{
  \if@filesw
  \immediate\write\@auxout{\string\gdef\string\@bibstyle{#1}}\fi 
  \if@localcite\else\REALbibliographystyle{#1}\fi\@localcitedflt} 
%
% \begin{bibunit} .. \end{bibunit} definition 
%
\def\bibunit{\global\advance\@bibunitauxcnt 1 \let\REALcite\cite% --bg
\let\REALnocite\nocite% --bg
\global\let\cite\NEWcite
\global\let\@citex\NEW@citex
\global\let\nocite\NEWnocite
\if@filesw
\immediate\openout\@bibunitaux \jobname.\the\@bibunitauxcnt.aux 
\fi\@ifnextchar[{\@bibunitx}{\@bibunitx[\@bibstyle]} }
\def\@bibunitx[#1]{\if@filesw
\immediate\write\@bibunitaux{\string\bibstyle{#1}}\fi} 
\def\endbibunit{
  \ifx\undefined\REALcite\else% --bg
    \global\let\cite\REALcite
     \global\let\@citex\REAL@citex
     \global\let\nocite\REALnocite
  \fi% --bg
  \if@filesw
  \immediate\closeout\@bibunitaux\fi}


%
% \begin{thebibliography} .. .. \end{thebibliography} new definition 
%
\newcounter{startbiblabel}
\newcounter{labelabsolu}
\def\@biblabel#1{[#1]}

\renewenvironment{thebibliography}[1]
     {\list{\setcounter{labelabsolu}{\value{startbiblabel}}
            \addtocounter{labelabsolu}{\value{enumiv}}
            \@biblabel{\arabic{labelabsolu}}
            }%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \if@openbib
              \advance\leftmargin\bibindent
              \itemindent -\bibindent
              \listparindent \itemindent
              \parsep \z@
            \fi
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\arabic{enumiv}}
            }%
      \if@openbib
        \renewcommand\newblock{\par}
      \else
        \renewcommand\newblock{\hskip .11em \@plus.33em \@minus.07em}%
      \fi
      \sloppy\clubpenalty4000\widowpenalty4000%
      \sfcode`\.=\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
       \setcounter{startbiblabel}{\value{labelabsolu}}
      \endlist}%

%
% \bibitem new definition
%
\newcounter{absolu@listctr}
\def\bibitem{\@ifnextchar[\@lbibitem\@bibitem}
\def\@lbibitem[#1]#2{\item[\@biblabel{#1}\hfill]\if@filesw
      {\let\protect\noexpand
       \immediate
       \write\@auxout{\string\bibcite{#2}{#1}}}\fi\ignorespaces}

\def\@bibitem#1{\item\if@filesw 
             \setcounter{absolu@listctr}{\value{\@listctr}}
             \addtocounter{absolu@listctr}{\value{startbiblabel}}
             \immediate\write\@auxout 
       {\string\bibcite{#1}{\the\value{absolu@listctr}}}\fi\ignorespaces}


%
% \putbib definition
%
\def\putbib{\@ifnextchar[{\@putbib}{\@putbib[\@bibdata]}} 
\def\@putbib[#1]{\if@filesw
\immediate\write\@bibunitaux{\string\bibdata{#1}}\fi 
\@input{\jobname.\the\@bibunitauxcnt.bbl}} 

\endinput
