% \CheckSum{611}
%
% \iffalse
%% Package `bibunits' to use with LaTeX2e.
%%
%% Copyright (C) 1999--2004 Thorsten Hansen
%%
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3 of this license
% or any later version. The latest version of this license is in
% http://www.latex-project.org/lppl.txt and version 1.3 or later is
% part of all distributions of LaTeX version 2003/12/01 or
% later.
%  
% This work has the LPPL maintenance status "maintained". The Current
% Maintainer of this work is Thorsten Hansen.
%
% This is a contributed file to the LaTeX2e system.
% 
%
%<*dtx>
          \ProvidesFile{bibunits.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{bibunits}
%<driver>\ProvidesFile{bibunits.drv}
% \fi
%         \ProvidesFile{bibunits.dtx}
          [2004/05/12 v2.4 Multiple bibliographies in one document.]
%
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage{ifthen}
%
% Sort `General´ before macros starting with `@'.
%
\makeatletter
\def\changes@#1#2#3{%
\protected@edef\@tempa{\noexpand\glossary{#1\levelchar
                                \ifx\saved@macroname\@empty
                                  \space 
                                  \actualchar
                                  \generalname
                                \else
                                  \space                   %  <-- \space added
                                  \expandafter\@gobble
                                  \saved@macroname
                                  \actualchar
                                  \string\verb\quotechar*%
                                  \verbatimchar\saved@macroname
                                  \verbatimchar
                                \fi
                                :\levelchar #3}}%
 \@tempa\endgroup\@esphack}       
\makeatother
%
% 
%
\CodelineIndex
\RecordChanges
%\OnlyDescription
\begin{document}
\DocInput{bibunits.dtx}
\end{document}
%</driver>
% \fi
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \newcommand{\class}[1]{\textsf{#1}}
% \renewcommand*\descriptionlabel[1]{\hspace\labelsep\option{#1}}
% \newcommand{\komascript}{KOMA-\textsc{Script}}
% \newcommand{\macrott}[1]{\texttt{\symbol{'134}#1}}
% \newcommand{\option}[1]{\textsf{#1}}
% \newcommand{\package}[1]{\textsf{#1}}
%
% ^^A
% ^^A Rules to separate figures from the text. (Modified from bibtopic.dtx).
% ^^A
% \newcommand\toprule[1][]{^^A
%   \ifthenelse{\equal{#1}{}}{^^A
%     \noindent{\makebox[\textwidth][t]{\hrulefill}}^^A
%     }{%
%     \noindent{\makebox[\textwidth][t]{\hrulefill^^A
%               \raisebox{-.5ex}{ \small\ttfamily#1 }\hrulefill}}^^A
%     }%
%   \par\noindent}
%
% \newcommand\bottomrule{\noindent\rule[\baselineskip]{\textwidth}{.6pt}}
%                                                                              
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \hyphenation{bib-lio-gra-phy bib-lio-gra-phies bib-unit bib-units}
%
% \GetFileInfo{bibunits.dtx}
% \title{The \package{bibunits} Package}
% \author{Thorsten Hansen\\
%         \normalsize thorsten.hansen@psychol.uni-giessen.de}
% \date{\fileversion\quad \filedate}
% \maketitle
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \GlossaryPrologue{\section*{{Change History}}%
%                   \markboth{{Change History}}{{Change History}}%
%    Version 1.0 by Jos\'e Alberto Fern\'andez, Version 1.0a till 2.3
%    by Thorsten Hansen. Version numbers suffixed with characters have
%    not been released. Since v2.1, each new version has a `General'
%    item describing the new feature or the fixed problem,
%    and local items describing the changes of the code.} 
%
% \changes{v1.0}{1991/09/07}{Initial \LaTeX\ 2.09 version.}
% \changes{v1.0a}{1998/10/30}{The same cited item can occur in more than one
%    bibliography.} 
% \changes{v1.0b}{1998/11/09}{Auxiliary files are created only for bibunits
%    which contain cite commands.}  
% \changes{v1.0c}{1998/11/15}{Extra spaces are no longer introduced,
%    upgrade to \LaTeXe.} 
% \changes{v2.0}{1999/07/22}{Documentation.}
% \changes{v2.1a}{1999/11/15}{Add bibliography name to table of
%    contents for compatibility with \komascript\ classes using the option
%    \option{bibtotoc}.}
% \changes{v2.1b}{1999/11/24}{Fixed problem with cite command after
%    \macrott{paragraph}.}
% \changes{v2.1c}{1999/11/24}{New option \option{labelstoglobalaux}.}
% \changes{v2.1d}{2000/01/27}{Global labels are no longer overwritten by 
%    local bibunit labels for the same cited item.}
% \changes{v2.1e}{2000/03/28}{Compatibility with \package{babel} using
%    the option \option{frenchb}.}
% \changes{v2.1f}{2000/04/02}{Streamlined implementation of starting
%    bibunits by cite commands.} 
% \changes{v2.1g}{2000/07/31}{Compatibility with \package{mparhack}.} 
% \changes{v2.1h}{2000/08/06}{New implementation: global labels are no
%    longer overwritten by local bibunit labels for the same cited item.}
% \changes{v2.1i}{2000/09/30}{Allow \macrott{putbib} to appear before
%    the first occurrence of a cite within a bibunit.}
% \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%    \macrott{@starredversion}, delete macro \macrott{@localcitedefault}.}
% \changes{v2.1k}{2000/10/02}{Ignore spaces in arguments for starred
%    version of \macrott{cite}.}   
% \changes{v2.1l}{2000/10/03}{New option \option{globalcitecopy}.}
% \changes{v2.1m}{2000/10/05}{Compatibility with \package{natbib} and
%    \package{babel} using the option \option{frenchb}.}
% \changes{v2.1n}{2000/10/08}{\package{jurabib} compatibility.}
% \changes{v2.1o}{2000/10/09}{Use of \macrott{@input} and
%    \macrott{@input@} consistent with \LaTeX\ base implementation.}
% \changes{v2.2}{2000/10/10}{Documentation.}
% \changes{v2.2a}{2003/07/12}{New macros \macrott{defaultbibliography}
%    and \macrott{defaultbibliographystyle}; default values of
%    \macrott{bu@bibunit} and \macrott{bu@bibstyle} changed
%    from \texttt{\{\}} to \macrott{jobname} resp.\ \texttt{plain}.} 
% \changes{v2.2b}{2003/07/12}{Compatibility with \package{cite} which
%    has integrated package \package{overcite} since v4.0 of
%    \package{cite}.}
% \changes{v2.2c}{2003/07/27}{Check whether warning ``Label(s) may
%    have changed. Rerun\dots'' should be given.}
% \changes{v2.2d}{2003/07/27}{New options \option{sectionbib} and 
%    \option{subsectionbib}.}
% \changes{v2.2e}{2003/08/18}{Compatibility patch for \package{babel}
%    together with \package{jurabib}.}
% \changes{v2.3}{2004/02/06}{Documentation.}
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% \begin{abstract}
%   The \package{bibunits} package allows separate bibliographies for
%   different units or parts of the text. The units can be chapters,
%   sections or \texttt{bibunit} environments. The package separates
%   the citations of each unit of text into a local auxiliary file to
%   be processed by \BibTeX. The same cited item can occur in more
%   than one bibliography. A global bibliography can also appear in
%   the document and citations can be placed in both at the same time.
%   The package is compatible with a wide variety of packages,
%   including, but not limited to, \textsf{cite}, \textsf{jurabib},
%   \textsf{natbib}, \textsf{overcite} and \komascript\ classes. It is
%   based on the \package{bibunits} style by Jos\'e Alberto Fern\'andez. 
% \end{abstract}
%
%
%
% \section{Introduction}
%
% Suppose, you have different units or parts within your document, and
% each unit should have its own bibliography. In this case, you can
% use the \package{bibunits} package to do the work. 
%
%
% Let's first consider a simple example to see how \package{bibunits}
% works (see Fig.~\ref{fig:Example}). We specify two bibunits using the
% \textsf{bibunit} environment. The optional parameter specifies the
% bibliography style to be used within the bibunits bibliography, in
% this case \textsf{plain} for the first and \textsf{alpha} for the
% second bibunit. Citations can be made as usual. Similar to the
% |\bibliography| command, you have to specify where the bibliography
% should appear within a bibunit. This is done using |\putbib|. The
% optional parameter specifies the \BibTeX\ files to be used for
% generating the bibunits bibliography, in this case \texttt{lit}.
%
% \begin{figure}[h]
% \hspace*{-25mm}\begin{minipage}[t]{0.6\textwidth}
% \toprule
% {\let\small\footnotesize
% \begin{verbatim}
% \documentclass{article}
% \usepackage{bibunits}
% 
% \begin{document}
% \begin{bibunit}[plain]
% References to the \TeX book \cite{Knuth:1991} 
% and to Lamport's \LaTeX\ book, which appears 
% only in the references\nocite{Lamport:1994}. 
% \putbib[lit]
% \end{bibunit}
% 
% \begin{bibunit}[alpha]
% A new bibunit with a different style and a cite
% of a Postscript tutorial \cite{Adobe:1985}.
% \putbib[lit]
% \end{bibunit}
%
% \end{document}
% \end{verbatim}}
% \bottomrule
% \end{minipage}
% \hspace{5mm}
% \begin{minipage}[t]{0.65\textwidth}
% \toprule
% \leftmargini1mm
% \begin{quote}
%   \footnotesize
%   References to the \TeX book [1]
%   and to Lamport's \LaTeX\ book, which appears 
%   only in the references.
%   \let\section\subsubsection
%   \vspace{-3mm}
%   \begin{thebibliography}{1}
%   \bibitem{Knuth:1991}
%   Donald~E. Knuth.
%   \newblock {\em The {\TeX} book}.
%   \newblock Addison-Wesley, Reading, Massachusetts, 1991.
%   \bibitem{Lamport:1994}
%   Leslie Lamport.
%   \newblock {\em {\LaTeX:} A Document Preparation System}.
%   \newblock Addison-Wesley, Reading, Massachusetts, 2 edition, 1994.
%   \end{thebibliography}     
%
%   A new bibunit with a different style and a cite
%   of a Postscript tutorial [Ado85].
%   \vspace{-3mm}
%   \begin{thebibliography}{{Ado}85}
%   \bibitem[{Ado}85]{Adobe:1985}
%   {Adobe {S}ystem {I}ncorporated}.
%   \newblock {\em Postscript Language Tutorial and Cookbook}.
%   \newblock Addison-Wesley, Reading, Massachusetts, 1985.
%   \end{thebibliography}        
%
% \end{quote}
% \vspace{2.25mm}
% \bottomrule
% \end{minipage}
% \caption{Example input and output.}
% \label{fig:Example}
% \end{figure}
% 
% For each bibunit,
% there is now a separate auxiliary file |bu|\meta{i}|.aux| to be
% processed by \BibTeX. In this example, we have two bibunits with
% auxiliary files |bu1.aux| and |bu2.aux|. To process your document,
% three runs of \LaTeX\ and two runs of \BibTeX\ are required.
%
% \vbox{%
% \begin{verbatim}
%   latex mydoc
%   bibtex bu1
%   bibtex bu2
%   latex mydoc
%   latex mydoc
% \end{verbatim}}                   
%
% This example should give a pretty good impression of what
% principally can be
% done with \package{bibunits} and how it can be done. But
% \package{bibunits} allows for many more features, e.g., bibunits
% can be specified by |\sections|, a global bibliography can be used,
% and cite command can be placed in both the local and the global
% bibliography at the same time, among other features. All these
% features and associated commands are described in the next section. 
%
%
%
%
% \section{Usage Notes}
%
% There are two ways to specify the units for which a bibliography is
% generated. You can either define your own \textsf{bibunit}
% environment, or you can specify bibunits for the \LaTeX\ units
% |\chapter| or |\section|. Both ways are mutually exclusive, meaning
% that you can choose only one and cannot mix them. 
%
% \DescribeEnv{bibunit} Use the environment
% |\begin{bibunit}|\oarg{style} to create a bibunit. The optional
% argument \meta{style} specifies a bibliography style. If the optional
% argument \meta{style} is omitted, the default style is used. How to 
% define a default style is explained below within this section.
%
% \DescribeMacro{\bibliographyunit}
% Alternatively, use |\bibliographyunit|\oarg{unit}, where \meta{unit} can be
% |\chapter| or |\section| to specify for which document unit
% a bibliography is generated, namely for every chapter or for every
% section. Use |\bibliographyunit| with no arguments to deactivate
% bibliography units. By default |\bibliographyunit| is deactivated. 
% The command |\bibliographyunit| has to be issued
% after |\begin{document}|.
%
% \DescribeMacro{\cite*}
% \DescribeMacro{\nocite*}
% You can create a global bibliography as usual with the commands\\
% |\bibliography|\oarg{BibTeX files}  and |\bibliographystyle|\oarg{style}.
% Use |\cite| and |\nocite| to generate citations that appear in the
% local bibliography. Use |\cite*| and |\nocite*| inside a unit 
% to generate citations for both the local and global bibliography.
%
% \DescribeMacro{\putbib}
% Use |\putbib|\oarg{BibTeX files} to specify where the
% bibliography should appear within a bibunit. If the optional
% argument is omitted, |\putbib| uses the default \BibTeX\ files. How to 
% define a default files is explained below within this section.
%
% \DescribeMacro{\defaultbibliography}
% \DescribeMacro{\defaultbibliographystyle}
% Use |\defaultbibliography|\marg{BibTeX files} and\\
% |\defaultbibliographystyle|\marg{style} to specify default \BibTeX\
% data files and style to be used in the local bibliographies. If no
% defaults are specified, |\jobname| is used as the default
% bibliography file and \textsf{plain} is used as the default
% bibliography style.
%
% \medskip\footnotesize
% \def\MacroFont{\fontencoding\encodingdefault
%                \fontfamily\ttdefault
%                \fontseries\mddefault
%                \fontshape\updefault
%                \footnotesize}%
%
% For compatibility to pre-v.2.3
% versions of \package{bibunits}, two other ways to define
% default \BibTeX\ styles and files for the local units are possible,
% but their use is not recommended. 
%
% If |\bibliographyunit| is active, there are two other ways to define
% default \BibTeX\ styles and files for the local units: 
% either by defining a global bibliography using |\bibliography| and
% |\bibliographystyle| or by using
% starred versions |\bibliography*| and |\bibliographystyle*|, as
% detailed below.
% 
% \DescribeMacro{\bibliography} \DescribeMacro{\bibliographystyle}
% If |\bibliographyunit| is active, the commands
% |\bibliography|\oarg{BibTeX files} and\\
% |\bibliographystyle|\oarg{style} also specify the
% \BibTeX\ files and style to be used by default in the local units.
% The commands |\bibliography| and |\bibliographystyle| have to be issued
% after |\begin{document}|.
%
% \DescribeMacro{\bibliography*}
% \DescribeMacro{\bibliographystyle*}
% If |\bibliographyunit| is active, you can use starred
% forms |\bibliography*|\marg{BibTeX files} and
% |\bibliographystyle*|\marg{style} to specify the defaults for
% the local units only. These commands do not generate a global
% bibliography or any information for the global bibliography. 
% The commands |\bibliography*| and |\bibliographystyle*| have to be issued
% after |\begin{document}|.
% 
% Note that the old pre-v2.3 ways to define default 
% style and \BibTeX\ files can only be specified while
% |\bibliographyunit| is active. Thus, if you do want default styles
% but \textsf{bibunit} environments instead of |\bibliographyunit|s,
% you have to issue a dummy |\bibliographyunit[\section]|, then
% specify defaults |\bibliography*|\marg{BibTeX files} and
% |\bibliographystyle*|\marg{style} and finally switch off the
% generation of bibunits by sections using |\bibliographyunit|.
%
% \normalsize
%
%
%
% \subsection{Package options}
% \begin{description}
% \item[globalcitecopy] Create an entry in the global bibliography for
%   each cite command within a bibunit. Default is to create an entry
%   only for the local bibliography. 
% \item[labelstoglobalaux] Write the replacement text for a label
%   inside a bibunit to the global auxiliary file.
%   Default is to write the labels to the local auxiliary file. The
%   option \option{labelstoglobalaux} is useful in some situations, e.g.,
%   \begin{itemize}
%   \item to resolve the references when a bibunit bibliography is
%     |\include|d.  
%   \item to resolve citations within a moving argument like
%     |\section|, which otherwise would result in an `Undefined
%     reference' error. 
%   \end{itemize}
%   Note that this option results in wrong labels for numerical
%   reference schemes if the same cited item appears at different 
%   positions in more than one bibliography 
%   (cf.~Sec.~\ref{sec:Limitations}).   
% \item[sectionbib] Let bibliography titles appear at the section
%   level. Useful for chapters as bibliography units.
% \item[subsectionbib] Let bibliography titles appear at the subsection
%    level. Useful for sections as bibliography units.
% \end{description}
% 
%
%
% \subsection{\textnormal{\BibTeX} processing}
%
% For each bibunit, in sequence, there is now a corresponding file
% |bu|\meta{i}|.aux| that needs to be compiled through \BibTeX. Suppose
% your document has \meta{n} different bibunits, you must now invoke
% \BibTeX\ on |bu1|, \dots , |bu|\meta{n}. This can be done by a
% csh-script.
% \begin{verbatim}
%    #!/bin/csh
%    foreach auxfile (bu*.aux)
%      echo bibtex `basename $auxfile .aux`
%      bibtex `basename $auxfile .aux`
%    end
% \end{verbatim}
%
% I you don't have the |basename| command, you can use an alternative
% script where the suffix |.aux| is removed using |sed|.
% \begin{verbatim}
%    #!/bin/csh
%    foreach auxfile (bu*.aux)
%      set auxfilebasename = `echo $auxfile | sed s/.aux//`
%      echo bibtex $auxfilebasename 
%      bibtex $auxfilebasename
%    end
% \end{verbatim}
%
% If by some strange coincidence you have named some of your files
% |bu|\meta{i}|.aux|, you have to redefine the internal command
% |\@bibunitname|, otherwise your files will be overwritten. To get
% for your document |foo.tex| filenames |foo.|\meta{i}|.aux| as for
% the first version of \package{bibunits}, you can redefine the
% internal macro |\@bibunitname|.
% \begin{verbatim}
%    \makeatletter
%    \renewcommand{\@bibunitname}{\jobname.\the\@bibunitauxcnt}
%    \makeatother
% \end{verbatim} 
%
% If you also want a global bibliography for your document |foo.tex|,
% the file |foo.aux| needs to be compiled through \BibTeX\ as well.
%
%
%
% 
% \subsection{Limitations}
% \label{sec:Limitations}
%
% \begin{itemize}
% \item A citation within a moving argument in a bibunit cannot be
%   resolved outside this bibunit, because the label is local to the
%   bibunit. This occurs, e.g., for a |\cite| within |\section|, which is
%   undefined in the table of contents. You can use the option
%   \option{labelstoglobalaux} to resolve the citations, but this
%   has two side effects.
%   \begin{itemize}
%   \item Since the cite command also appears globally in the table of
%     contents, it generates a |\citation| command for the
%     \emph{global} auxiliary file. As a result, another \BibTeX\ run
%     causes this previously local reference to creep into the global
%     bibliography.
%   \item Since the replacement text for all cited items is now
%     global, the same cited item has always the same replacement
%     text, regardless whether it appears at different positions within
%     different bibunits. This results in wrong labels for numerical
%     reference schemes if the same cited item appears at different
%     positions within the bibliographies.
%   \end{itemize} 
% \item When using \package{natbib}, |\cite*| has the
%   meaning as defined by \package{natbib}, namely to force full
%   citation of multiple authors. To get the behavior as defined
%   by \package{bibunits} (without \package{natbib}), use an
%   accompanying |\nocite| outside of a bibunit. Alternatively, if 
%   you want \emph{every} local item to appear also in the global
%   bibliography, you can use \package{bibunits} with option
%   \option{globalcitecopy}. 
% \item With \package{natbib}, you cannot use numerical and
%   author-year reference schemes together. Typographically, this
%   is regarded bad style anyway.
% \end{itemize}
%
%
%
%
% \section{Examples}
% \label{sec:Examples}
%
%
%
% \subsection{Bibunits by the \texttt{bibunit} environment}
%
% In the first example, two bibliographies are generated, the first
% using the \BibTeX\ file |texlit.bib| and the style |plain|, the
% second using the \BibTeX\ file |lit.bib| and the style |abbrv|.
% %
% \begin{verbatim}
%   \documentclass{article}
%   \usepackage{bibunits}
%
%   \begin{document}
%   \begin{bibunit}[plain]
%     some text \cite{Lamport:1994} more text more citations
%     \putbib[texlit]
%   \end{bibunit}
%   some text between the units
%   \begin{bibunit}[abbrv]
%     some text \cite{Knuth:1991} more text more citations
%     \putbib[lit]
%   \end{bibunit}
%   \end{document}
% \end{verbatim}
%      
% If all bibunits use the same \BibTeX\ files and style, you can
% specify defaults and omit the optional arguments of the |bibunit|
% environment and the |\putbib| macro. In the second example, a
% default \BibTeX\ file |texlit.bib| and a default style |abbrv| is
% defined using |\defaultbibliography| and |\defaultbibliographystyle|.
% %
% \begin{verbatim}
%   \documentclass{article}
%   \usepackage{bibunits}
%
%   \begin{document}
%   \defaultbibliography{texlit}
%   \defaultbibliographystyle{plain}
%   
%   \begin{bibunit}
%     some text \cite{Lamport:1994} more text more citations
%     \putbib
%   \end{bibunit}
%   some text between the units
%   \begin{bibunit}
%     some text \cite{Knuth:1991} more text more citations
%     \putbib
%   \end{bibunit}
%   \end{document}
% \end{verbatim}
%
% If you use, e.g., the class |article|, the heading of the bibliography has
% the same font size as a section, which might be too large, especially if
% you have bibunits \emph{within} sections or even subsections. In
% this case you may want to change the bibliography heading to have
% the same appearance as a subsection. This can be done by passing the
% option \option{subsectionbib} to \package{bibunits}.
% %
% \begin{verbatim}
%   \documentclass{article}
%   \usepackage[subsectionbib]{bibunits}
% \end{verbatim}
%
% Similarily, if you use a class such as  |book| where the
% bibliography headings appear at the chapter level by default,
% headings of the local bibliographies can be changed to appear at the
% section level  by passing the
% option \option{sectionbib} to \package{bibunits}.
% %
% \begin{verbatim}
%   \documentclass{book}
%   \usepackage[sectionbib]{bibunits}
% \end{verbatim}
%
%
%
%
% \subsection{Bibunits by chapters or sections}
%
% You can also define bibliographies for every chapter or section. In
% this case, the redefinition of |\thebibliography| by passing options
% \option{sectionbib} resp.\ \option{subsectionbib} is essential for
% the proper behavior of the \package{bibunits} package. The reason is
% quite simple: if you create bibliographies for every section,
% and |\thebibliography| also appears at the section level, a new
% unit is opened by |\thebibliography|, and the information of the
% previous unit is no longer available. (More technically: The
% auxiliary file of the previous unit is closed, and the
% replacement text for the citation which is generated inside
% |\thebibliography| cannot be written to this file. Instead, it is
% written to the global |.aux| file.) 
%
% The next example corresponds to the first example of the previous
% section with different \BibTeX\ files and styles. Note that you have
% to specify the |\defaultbibliographystyle| \emph{before} the corresponding
% section.
% %
% \begin{verbatim}
%   \documentclass{article}
%   \usepackage[subsectionbib]{bibunits}
%
%   \begin{document}
%   \bibliographyunit[\section]
%   \defaultbibliographystyle{plain}
%
%   \section{First section}
%     some text \cite{Lamport:1994} more text more citations
%     \putbib[texlit]
%   some text between the units
%   \defaultbibliographystyle{abbrv}
%   \section{Second and last section}
%     some text \cite{Knuth:1991} more text more citations
%     \putbib[lit]
%   \end{document}
% \end{verbatim}
% %
% \vspace{-1ex}
% \noindent You can also use the same \BibTeX\ file and style for all units.
% %
% \begin{verbatim}
%   \documentclass{article}
%   \usepackage[subsectionbib]{bibunits}
%
%   \begin{document}
%   \bibliographyunit[\section]
%   \defaultbibliography{texlit}
%   \defaultbibliographystyle{plain}
%
%   \section{First section}
%     some text \cite{Lamport:1994} more text more citations
%     \putbib
%   some text between the units
%   \section{Second and last section}
%     some text \cite{Knuth:1991} more text more citations
%     \putbib
%   \end{document}
% \end{verbatim}
%
%
%
%
% \subsection{Bibunits and a global bibliography}
%
% In all four examples, one can specify a global
% bibliography and its style with the usual \LaTeX\ commands.
% Citations for the global bibliography are entered using |\cite| and
% |\nocite| commands while outside a unit or using |\cite*| and
% |\nocite*| while inside a unit. The starred forms generate citations
% also for the local unit. Thus it is not possible to generate
% citations \emph{only} for the global bibliography while inside a
% unit (which seems to be a reasonable restriction). Note that if
% |\bibliographyunit| is active, \emph{and} you use a global
% bibliography, \emph{and} you use the same \BibTeX\ file and style
% for all (local as well as global) bibliographies, \emph{then} the
% |\defaultbibliography| and |\defaultbibliographystyle| commands are not
% necessary. In this case proper defaults for the local bibliographies
% are specified by the commands
% |\bibliography| and |\bibliographystyle|, too. Note that a second
% run of \LaTeX\ is necessary before \BibTeX\ processing, in case that
% the global bibliography has its usual place at the end of the
% document. 
%
% The font size used to typeset the global bibliography is the same as
% for the local bibliographies. To change this, switch off the
% bibliography units by chapters or sections using |\bibliographyunit|
% and change |\thebibliography| to its original meaning just before
% the global bibliography.
% %
% \begin{verbatim}
%   \bibliographyunit
%   \let\thebibliography\stdthebibliography
%   \bibliographystyle{plain} % global bibliography
%   \bibliography{texlit}     % global bibliography
% \end{verbatim}
%
%
%
% \section{Acknowledgments}
%
% The author thanks Jos\'e Alberto Fern\'andez for the coding of
% \package{bibunits} v1.0. Further, contributions of code fragments
% from various people are gratefully acknowledged (in alphabetical
% order): Battista Benciolini for the patch to allow |putbib| to
% appear before the first occurrence of a cite within a bibunit; Jens
% Berger for the \package{jurabib} fragment of the \package{babel}
% compatibility code; Jason Harrison for an
% alternative script to call \BibTeX\ for each local auxiliary file;
% Werner J\"urgens for the hint on how to suppress unwanted spaces;
% Frank Mittelbach for a code fragment how to make \package{bibunits} 
% ``Label(s) may have changed.'' warning appear at the end of a \LaTeX\ run; 
% Andrew E.\ Schulman for an early version of the \package{natbib}
% compatibility patch; Stefan Ullrich for an early version of the
% \package{jurabib} compatibility patch (communicated by Jens Berger)
% and the \package{mparhack} compatibility patch. Finally, the author
% acknowledges the contributions of numerous people, in particular
% Frank Mittelbach, whose suggestions and bug reports have helped to 
% improve \package{bibunits}.
%
%
% \StopEventually{}
%
%
%
%
% \section{The Macros}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%
% \subsection{Allocation}
% 
% \begin{macro}{\@bu@tempcnta}
%    \changes{v2.1m}{2000/10/05}{Counter removed.}
%
%    [Allocation of counter removed.]
% \end{macro}
%
%
%
% \subsection{Option handling}
%
%
% \begin{macro}{\iflabelstoglobalaux}
%    \changes{v2.1c}{1999/11/24}{Add code for option processing and option
%       \option{labelstoglobalaux}.}
%
%    Define a new if to switch between global definition of labels
%    within a bibliography and local labels (default). Global labels 
%    can be activated with option \option{labelstoglobalaux}.
%    \begin{macrocode}
\newif\iflabelstoglobalaux \labelstoglobalauxfalse
\DeclareOption{labelstoglobalaux}{\labelstoglobalauxtrue}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\ifglobalcitecopy}
%    \changes{v2.1l}{2000/10/03}{Add code for option \option{globalcitecopy}.}
%
%    Define a new if to switch between entering of cite commands
%    within a bibunit also to the global bibliography or only to the local
%    bibliography (default). Entering of cite commands also to the
%    global bibliography can be activated with
%    option \option{globalcitecopy}.
%
%    \begin{macrocode}
\newif\ifglobalcitecopy
\globalcitecopyfalse
\DeclareOption{globalcitecopy}{\globalcitecopytrue}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{sectionbib}
%    \changes{v2.2d}{2003/07/27}{Add code for option \option{sectionbib}.}
%
%    Let bibliography titles appear at the section level. Useful for
%    chapters as bibliography units.
%
%    \begin{macrocode}
\DeclareOption{sectionbib}{%
  \AtBeginDocument{%
    \let\stdthebibliography\thebibliography
    \def\thebibliography{%
      \let\chapter\section 
      \stdthebibliography}}}  
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{subsectionbib}
%    \changes{v2.2d}{2003/07/27}{Add code for option \option{subsectionbib}.}
%
%    Let bibliography titles appear at the subsection level. Useful for
%    sections as bibliography units.
%
%    \begin{macrocode}
\DeclareOption{subsectionbib}{%
  \AtBeginDocument{%
    \let\stdthebibliography\thebibliography
    \def\thebibliography{%
      \let\chapter\subsection 
      \let\section\subsection 
      \stdthebibliography}}}  
%    \end{macrocode}
% \end{macro}
%
%
% \noindent Finally, process all package options.
%
%    \begin{macrocode}
\ProcessOptions
%    \end{macrocode}
%
%
%
% \subsection{An auxiliary file for each bibunit is provided}
% 
% 
% \begin{macro}{\@bibunitaux}
%    Define the file descriptor |\@bibunitaux| of the auxiliary file that is 
%    generated for each bibunit.
%    \begin{macrocode}
\newwrite\@bibunitaux
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@bibunitauxcnt}
%    Define the counter for the bibunits and initialize it with zero. 
%    \begin{macrocode}
\newcount\@bibunitauxcnt \@bibunitauxcnt=0
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@bibunitname}
%    \changes{v1.0d}{1998/11/15}{New name
%       \mbox{\texttt{bu}\string\meta{i}\texttt{.aux}} for the
%       auxiliary files that are created for each bibunit.} 
%
%    The command |\@bibunitname| sets the basename of the auxiliary files
%    that are created for each bibunit to |bu| (|bu| for
%    |b|ib|u|nits), followed by the current number of the bibunit,
%    |\the\@bibunitauxcnt|. (In version 1.0 of \package{bibunits} there
%    was no such command, and the command sequence
%    |\jobname.\the\@bibunitauxcnt| was directly used.)  
%    \begin{macrocode}
\def\@bibunitname{bu\the\@bibunitauxcnt}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{New cite commands}
%
% The main idea is to save the old meaning of the command and then
% to redefine it. At the beginning of a bibunit, the new commands are
% activated, at the end of a bibunit, the old commands are restored.
% 
% \begin{macro}{\if@localcite}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion}.}
%    
%    Define a new if to store if a macro is evoked with or without a
%    star. This is used by cite commands to switch between local and
%    global cites, i.e., cites of references that occur in the local
%    or also in the global bibliography. The is also a starred version
%    of |\bibliopography| and |\bibliopographystyle| to enter
%    default \BibTeX\ data files and styles for the bibunits only.
% 
%    \begin{macrocode}
\newif\if@starredversion
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@localcitedefault}
%    \changes{v2.1j}{2000/10/02}{Macro removed.}
%
%    [Macro deleted.]
% \end{macro}
% 
%
% \begin{macro}{\std@cite}
% \begin{macro}{\bu@cite}
%    \changes{v2.0c}{1999/09/27}{Macro declared robust.}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion} and switch false and true.}
%
%    \begin{macrocode}
\let\std@cite\cite
\DeclareRobustCommand\bu@cite{%
  \@ifstar
    {\@starredversiontrue\std@cite}% 
    {\@starredversionfalse\std@cite}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\std@@citex}
% \begin{macro}{\bu@@citex}
%    \changes{v2.0a}{1999/06/02}{\package{natbib} v6.5 compatibility.}
%    \changes{v2.0b}{1999/07/06}{Implementation streamlined.}
%    \changes{v2.0f}{1999/10/05}{\package{overcite} compatibility.}
%    \changes{v2.0g}{1999/10/22}{\package{natbib} v7.0 compatibility.}
%    \changes{v2.1b}{1999/11/24}{\macrott{leavevmode} added.}
%    \changes{v2.1f}{2000/04/02}{\macrott{@startbibunitorrelax} added,
%       definition of standard version for all \package{natbib} cite
%       commands removed.}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion} and switch if branches,
%       delete \macrott{@localcitedefault}.}
%    \changes{v2.1k}{2000/10/02}{For standard definition (without
%       \package{bibunits} or \package{overcite}), replace direct
%       writing to \macrott{@bibunitaux} by use of \macrott{std@nocite}.}
%    \changes{v2.1l}{2000/10/03}{Handling of option
%       \option{globalcitecopy} added.}
%    \changes{v2.1n}{2000/10/08}{\package{jurabib} compatibility.}
%    \changes{v2.2b}{2003/07/12}{\package{cite} compatibility.}
%    
%    Define the \package{bibunits} macro |\bu@@citex| replacing the
%    internal macro |\@citex|. Macro |\@citex| is evoked by the |\cite|
%    and its various derivatives defined by other packages. Basically,
%    |\bu@@citex| the following actions take place. The macro
%    |\startbibunitorrelax| ensures that auxiliary files are created
%    only for non-empty bibunits. The macro |\leavevmode| are added to
%    fix a problem that occurs in some special situations when a
%    |\paragraph| within a bibunit is immediately followed by a cite
%    command, as explained below. Next, the standard version of
%    |\@citex|, namely |\std@@citex| is evoked with |\@auxout| locally
%    changed, so that citations are written to the local auxiliary
%    file of each bibunit. Finally, if option \option{globalcitecopy}
%    is active or the starred form of the cite command is evoked,
%    i.e., |\if@starredversion| is true, citations are
%    \emph{also} written to the global auxiliary file, so that
%    citations appear in both the local as well as the global
%    bibliography at the same time.
%    
%    After this general description, the use of |\leavevmode| is
%    further detailed. The |\leavevmode| commands are added to fix a
%    problem that occurs when a |\paragraph| within a bibunit is
%    immediately followed by a cite command, and within |\paragraph|
%    something is written to |\@auxout|, e.g., an entry for the table
%    of contents or the index. In this cases, the entry go to the
%    local auxiliary file of the corresponding bibunit instead of the
%    standard auxiliary file |\@auxout|. The reason is that the
%    argument of |\paragraph| is stored in an |\everypar|, which is
%    evaluated later. In case of a cite command that immediately
%    follows |\paragraph|, the evaluation occurs within the scope of
%    the cite, where the |\@auxout| is let to the local auxiliary file
%    of the corresponding bibunit. Consequently, the entry is written
%    to the wrong, i.e., local, auxiliary file. We add an
%    |\leavevmode| to evaluate |\everypar| before |\@auxout| is
%    redefined. Alternatively, the typesetting of |\paragraph| in
%    |\@xsect| of the \LaTeX\ kernel might be revised.
%
%    Additional to the new standard definition of |\@citex|, different
%    versions have to be defined depending other packages loaded,
%    namely \package{natbib}, \package{overcite} and
%    \package{jurabib}. These packages are mutually exclusive, with
%    the priority as defined by their order in the previous sentence.
%    That means, e.g., if you have chosen \package{natbib} together
%    with \package{bibunits}, you cannot use \package{overcite} or
%    \package{jurabib}.
%
%    All definitions are done |\AtBeginDocument| so that packages
%    loaded after \package{bibunits} can be discovered. 
%    
%    \begin{macrocode}
\AtBeginDocument{%
%    \end{macrocode}
%
%    Several modifications and additions are necessary if package
%    \package{natbib} is loaded: First macro |\NAT@set@cites| is used
%    to invoke \package{natbib}'s cite commands. This is only
%    necessary if these commands have not been set, i.e. in case
%    \package{bibunits} is evoked before \package{natbib}, but does
%    not do any harm otherwise. Second macro |\bu@@citex| is changed
%    as described below, and the various versions of
%    \package{natbib}'s cite commands are save in a |\std|\dots version.
%
%    The definition of |\bu@@citex| is changed in the following ways:
%    i) the |\let| of |\@auxout| is encapsulated using |\begingroup|
%    and |\endgroup| instead of braces, since braces results in an
%    error; ii) macro |\std@@citex| has two optional
%    arguments instead of one; iii) the code of |\if@starredversion| is
%    skipped in favor of the meaning of a starred cite in
%    \package{natbib}, namely to force full listing of multiple
%    authors.
%    
%    \begin{macrocode}
  \@ifpackageloaded{natbib}%
  {%
    \NAT@set@cites \let\std@@citex\@citex
    \def\bu@@citex[#1][#2]#3{%
      \@startbibunitorrelax
      \leavevmode  
      \begingroup\let\@auxout\@bibunitaux\std@@citex[#1][#2]{#3}\endgroup
      \ifglobalcitecopy 
        \std@nocite{#3}%
      \fi      
    }%
  }%
%    \end{macrocode}
%
%    If \package{natbib} is not loaded, we check for
%    \package{cite}. In this case, an additional command
%    |\@citew| has to be redefined which handles |\cite| without an
%    argument. Further, an internal command of \package{cite},
%    namely |\@nocite| is used to write to the standard auxiliary file. 
%
%    \begin{macrocode}
  {%
    \@ifpackageloaded{cite}%
    {%
      \let\std@@citew\@citew
      \def\bu@@citew#1{%
        \@startbibunitorrelax
        \leavevmode  
        {\let\@auxout\@bibunitaux \std@@citew{#1}}%
        \ifglobalcitecopy 
          \@nocite{#1}%
        \else 
          \if@starredversion
            \@nocite{#1}%
          \fi
        \fi
      }
      \let\std@@citex\@citex
      \def\bu@@citex[#1]#2{%
        \@startbibunitorrelax
        \leavevmode  
        {\let\@auxout\@bibunitaux \std@@citex[#1]{#2}}%
        \ifglobalcitecopy 
          \@nocite{#2}%
        \else 
          \if@starredversion
            \@nocite{#2}%
          \fi
        \fi
      }
    }%
%    \end{macrocode}
%    
%    Next we check if \package{overcite} is loaded. This is done for
%    compatibility reasons only, since \package{overcite} is
%    integrated into \package{cite} since version 4.0.---We give a
%    warning that an obsolete package has been loaded and do the
%    same definitions as for \package{cite}.
%
%    \begin{macrocode}
    {%
      \@ifpackageloaded{overcite}%
      {%
        \PackageWarningNoLine{bibunits}%
          {Obsolete package overcite loaded. Use package cite which
            option [superscript] instead}%
        \let\std@@citew\@citew
        \def\bu@@citew#1{%
          \@startbibunitorrelax
          \leavevmode  
          {\let\@auxout\@bibunitaux \std@@citew{#1}}%
          \ifglobalcitecopy 
            \@nocite{#1}%
          \else 
            \if@starredversion
              \@nocite{#1}%
            \fi
          \fi
        }
        \let\std@@citex\@citex
        \def\bu@@citex[#1]#2{%
          \@startbibunitorrelax
          \leavevmode  
          {\let\@auxout\@bibunitaux \std@@citex[#1]{#2}}%
          \ifglobalcitecopy 
            \@nocite{#2}%
          \else 
            \if@starredversion
              \@nocite{#2}%
            \fi
          \fi
        }
      }%
%    \end{macrocode}
%    
%    If neither \package{natbib} nor \package{cite} nor 
%    \package{overcite} are loaded, we
%    check for \package{jurabib}. In this case, the definition of
%    |\std@@citex| differs from the default version by a second
%    optional argument, similar to \package{natbib}.
%
%    \begin{macrocode}
      {%
        \@ifpackageloaded{jurabib}%
        {% 
           \let\std@@citex\@citex
          \def\bu@@citex[#1][#2]#3{%
            \@startbibunitorrelax
            \leavevmode  
            {\let\@auxout\@bibunitaux \std@@citex[#1][#2]{#3}}%
            \ifglobalcitecopy 
              \std@nocite{#3}%
            \else 
              \if@starredversion
                \std@nocite{#3}%
              \fi
            \fi
          }%      
        }%
%    \end{macrocode}
%
%    Finally, the default definition.
%
%    \begin{macrocode}
        {%
          \let\std@@citex\@citex
          \def\bu@@citex[#1]#2{%
            \@startbibunitorrelax
            \leavevmode  
            {\let\@auxout\@bibunitaux \std@@citex[#1]{#2}}%
            \ifglobalcitecopy 
              \std@nocite{#2}%
            \else 
              \if@starredversion
                \std@nocite{#2}%
              \fi
            \fi
          }%
        }%
      }%
    }%
  }%
}%
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\std@nocite}
% \begin{macro}{\bu@nocite}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion} and switch false and true.}
%
%    \begin{macrocode}
\let\std@nocite\nocite
\def\bu@nocite{%
  \@ifstar
    {\@starredversiontrue\@bu@nocite}%
    {\@starredversionfalse\@bu@nocite}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@bu@nocite}
%    \changes{v2.1f}{2000/04/02}{\macrott{@startbibunitorrelax} added.}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion} and switch if branches,
%       delete \macrott{@localcitedefault}.}
%    \changes{v2.1k}{2000/10/02}{Replace direct writing to
%       \macrott{@bibunitaux} by \macrott{std@nocite} within scope
%       of redefined \macrott{@auxout}, \macrott{@bsphack} and
%       \macrott{@esphack} removed.} 
%    \changes{v2.1l}{2000/10/03}{Handling of option
%       \option{globalcitecopy} added.}
%
%    \begin{macrocode}
\def\@bu@nocite#1{%
  \@startbibunitorrelax
  {\let\@auxout\@bibunitaux \std@nocite{#1}}%
  \ifglobalcitecopy 
    \std@nocite{#1}%
  \else 
    \if@starredversion
      \std@nocite{#1}%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{New bibliography and bibliographystyle commands}
%
% \changes{v1.0e}{1998/11/30}{Global definition of bibunit data and style.}
%
% In this paragraph no general description is given, rather an
% explanation why in the following two macros |\@bu@bibliography| and
% |\@bu@bibliographystyle| the command |\gdef\bu@bibdata| and
% |\gdef\bu@bibstyle| are written to the auxiliary file and are directly
% executed, too. The execution through the auxiliary file has been left
% for compatibility to older versions of \package{bibunits}: if you
% also have a global bibliography, you can specify \BibTeX\ styles and
% files for the global bibliography (using |\bibliography| and
% |\bibliographystyle|, usually at the end of the document). In this
% case, writing the execution of |\gdef| to the auxiliary file ensures
% that in subsequent runs of \LaTeX defaults for the local
% bibliographies are generated as well. Without this, the user would
% have to explicitly specify \BibTeX\ files and styles for the local
% bibliographies using the starred forms |\bibliography*| and
% |\bibliographystyle*|. On the other hand, with the direct execution
% of the |\gdef| commands, the starred forms specify defaults which can be
% used already in the first run of \LaTeX\, if put before the first bibunit.
%
%
%
% \begin{macro}{\bu@bibdata}
%    \changes{v2.2a}{2003/07/12}{Default changed from \texttt{\{\}} to
%    \macrott{jobname}.} 
%    
%    Define data files store.
%    \begin{macrocode}
\def\bu@bibdata{\jobname}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\orig@bibliography}
% \begin{macro}{\bibliography}
% \begin{macro}{\std@bibliography}
%    \changes{v2.1h}{2000/08/06}{New definition of
%       \macrott{bibliography} to open \texttt{bu.aux}, write to
%       \macrott{@mainaux} and close \texttt{bu.aux}.}    
%  
%    If |\labelstoglobalaux| is false, define a new version of
%    |\bibliography| where the replacement text for the labels is
%    written to a new file |bu.aux|. This file is input at the
%    end of each bibunit to restore the global labels which are
%    overwritten if a global item is also cited within a bibunit.
%    Input of the main auxiliary file is not appropriate, because this
%    file contains additional material which should not be input more
%    than once, e.g., commands which generate entries for the table of
%    contents.
%
%    The definition of |\std@bibliography| is used to define a 
%    version of |\bibliography| with enhanced functionality
%    |\bu@bibliography|.
% 
%    \begin{macrocode}
\AtBeginDocument{%
  \iflabelstoglobalaux
  \else
    \let\orig@bibliography\bibliography
    \def\bibliography#1{%
      \if@filesw
        \immediate\openout\@bibunitaux bu.aux
        \immediate\write\@mainaux{\string\@input{bu.aux}}%
      \fi       
      \orig@bibliography{#1}%
      \if@filesw
        \immediate\closeout\@bibunitaux
      \fi       
    }%
  \fi
  \let\std@bibliography\bibliography
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\bu@bibliography}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion}.}
%
%    \begin{macrocode}
\def\bu@bibliography{%
  \@ifstar 
    {\@starredversiontrue\@bu@bibliography}%
    {\@starredversionfalse\@bu@bibliography}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@bu@bibliography}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion}, delete \macrott{@localcitedefault}.}
%    \changes{v2.2a}{2003/07/12}{Evokes new macro
%       \macrott{defaultbibliography}.}
%
%    \begin{macrocode}
\def\@bu@bibliography#1{%
  \defaultbibliography{#1}%
  \if@starredversion
  \else
    \std@bibliography{#1}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\defaultbibliography}
%    \changes{v2.2a}{2003/07/12}{New macro.}
%
%    \macrott{defaultbibliography} allows to define default
%    bibliography files whether bibunits is active or not. Does the same as
%    \macrott{bibliography*} when bibunits is active.
%
%    \begin{macrocode}
\def\defaultbibliography#1{%
  \if@filesw
    \immediate\write\@auxout{\string\gdef\string\bu@bibdata{#1}}%
  \fi
  \gdef\bu@bibdata{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\bu@bibstyle}
%    \changes{v2.2a}{2003/07/12}{Default changed from \texttt{\{\}} to
%    \texttt{plain}.} 
%    
%    Define style store.
%    \begin{macrocode}
\def\bu@bibstyle{plain}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\std@bibliographystyle}
% \begin{macro}{\bu@bibliographystyle}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion}.}
%
%    \begin{macrocode}
\let\std@bibliographystyle\bibliographystyle
\def\bu@bibliographystyle{%
  \@ifstar
    {\@starredversiontrue\@bu@bibliographystyle}%
    {\@starredversionfalse\@bu@bibliographystyle}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@bu@bibliographystyle}
%    \changes{v2.1j}{2000/10/02}{Rename \macrott{@localcite} to
%       \macrott{@starredversion}, delete \macrott{@localcitedefault}.}
%    \changes{v2.2a}{2003/07/12}{Evokes new macro
%       \macrott{defaultbibliographystyle}.}
%
%    \begin{macrocode}
\def\@bu@bibliographystyle#1{%
  \defaultbibliographystyle{#1}%
  \if@starredversion
  \else
    \std@bibliographystyle{#1}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\defaultbibliographystyle}
%    \changes{v2.2a}{2003/07/12}{New macro.}
%
%    \macrott{defaultbibliographystyle} allows to define a default
%    bibliography style whether bibunits is active or not. Does the same as
%    \macrott{bibliographystyle*} when bibunits is active.
%
%    \begin{macrocode}
\def\defaultbibliographystyle#1{%
  \if@filesw
    \immediate\write\@auxout{\string\gdef\string\bu@bibstyle{#1}}%
  \fi
  \gdef\bu@bibstyle{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Bibunits}
%
%
% \subsubsection{Bibunits by an extra environment}
%
% The starting of a bibunit is a two-step process: in the first step
% (macro |\bibunit|), all cite commands are redefined to trigger the
% second step, namely the actual starting of a bibunit (macro
% |\@startbibunit|). This may seem weird on the first sight, but has
% the advantage that no auxiliary files are created if a bibunit
% contains no |\cite|, |\cite*|, |\nocite| or |\nocite*| commands. To
% handle citations of the same reference in different bibliographies,
% the local auxiliary file is input at the beginning of each bibunit
% (in macro |\@startbibunit|), and the replacement text is also
% written to the \emph{local} auxiliary file (in macro |\@putbib|).
%
%
% \begin{macro}{\bibunit}
%    \changes{v2.0g}{1999/10/22}{\package{natbib} v7.0 compatibility.}
%    \changes{v2.1f}{2000/04/02}{Evoking of \package{bibunits} version
%       of cite commands, let \macrott{@startbibunitorrelax} to
%       \macrott{@startbibunit} added.} 
%
%    At the beginning of a bibunit, all cite command are set to
%    their initializing versions, and |\@finishbibunit| can relax,
%    because currently no citation has appeared. Hence no local
%    auxiliary file has been opened for the current bibunit, which has to
%    be closed at the end of the bibunit. 
%    \begin{macrocode}
\def\bibunit{%
  \global\let\cite\bu@cite
  \global\let\@citex\bu@@citex
  \global\let\@citew\bu@@citew
  \global\let\nocite\bu@nocite
  \global\let\@startbibunitorrelax\@startbibunit
  \global\let\@finishbibunit\relax
  \@ifnextchar[{\@bibunitx}{\@bibunitx[\bu@bibstyle]}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@bibunitx} 
%    If the current bibunit has an extra bibstyle, it is saved in
%    |\@localbibstyle|. (In version 1.0a |\bibstyle| command has been
%    directly written to the local auxiliary file. This is now done in
%    |\@startbibunit|.)
%    \begin{macrocode}
\def\@bibunitx[#1]{\gdef\@localbibstyle{#1}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\endbibunit}
%    \changes{v2.0f}{1999/10/05}{\package{overcite} compatibility.}
%    \changes{v2.0g}{1999/10/22}{\package{natbib} v7.0 compatibility.}
%    \changes{v2.1f}{2000/04/02}{Restoration of standard versions of
%       \package{natbib} cite commands removed.} 
%    \changes{v2.1h}{2000/08/06}{Input \texttt{bu.aux}.}
%    \changes{v2.1o}{2000/10/09}{Use \macrott{@input} instead of 
%       \macrott{@input@} for reading \texttt{bu.aux}.} 
%
%    At the end of the bibunit, all cite commands are restored to
%    their former meaning (|\@citew| is used by package
%    \package{overcite}). Also the local auxiliary file is closed if
%    it has been opened. Finally, the replacement text for the global
%    citation labels (if exists) are restored using |\@input|. 
%    \begin{macrocode}
\def\endbibunit{%
  \global\let\cite\std@cite
  \global\let\@citex\std@@citex
  \global\let\@citew\std@@citew
  \global\let\nocite\std@nocite
  \@finishbibunit
  \@input{bu.aux}%  
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@startbibunit}
%    \changes{v2.0f}{1999/10/05}{\package{overcite} compatibility.}
%    \changes{v2.0g}{1999/10/22}{\package{natbib} v7.0 compatibility.}
%    \changes{v2.1d}{2000/01/27}{Remove enclosing braces around input
%       of local auxiliary file and restore catcodes and endlinechar.}
%    \changes{v2.1e}{2000/03/28}{Change catcode of `:'.}
%    \changes{v2.1f}{2000/04/02}{Evoking of \package{bibunit} version
%       of cite commands moved to \macrott{bibunit}, restoration of
%       standard versions of \package{natbib} cite commands removed,
%       let \macrott{@startbibunitorrelax} to \macrott{relax}.
%       added.}
%    \changes{v2.1m}{2000/10/05}{Change of catcode of `:' removed;
%      encapsulate setting of \macrott{endlinechar} by braces.}
%
%    This command does all the necessary initialization for a bibunit:
%    New versions of the cite commands are activated (|\@citew| is
%    used by package \package{overcite}), the macro |\@finishbibunit|
%    which is called by |\endbibunit| is let to close the local
%    auxiliary file, the number of the bibunits is incremented. Most
%    important, the local auxiliary file is read, so that citations of
%    the same reference in different bibliographies are handled
%    correctly for numerical citation schemes. Finally, the bibstyle
%    is written to the local auxiliary file.
%    \begin{macrocode}
\def\@startbibunit{%
  \global\let\@startbibunitorrelax\relax 
  \global\let\@finishbibunit\@finishstartedbibunit
  \global\advance\@bibunitauxcnt 1
  \if@filesw
%    \end{macrocode}
%    Input the local auxiliary file and prevent the creation of
%    unwanted spaces by setting |\endlinechar-1| (|\catcode`\^^M=9|
%    would also work here).
%    \begin{macrocode}
    {\endlinechar-1
    \@input{\@bibunitname.aux}}%
    \immediate\openout\@bibunitaux\@bibunitname.aux
    \immediate\write\@bibunitaux{\string\bibstyle{\@localbibstyle}}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
% \noindent When bibunits are specified by chapters or sections, a new
% chapter or section  marks both the end of the old and the start of the
% new bibunit by calling |\endbibunit|. For the first chapter or section,
% the command |\@finishbibunit| has to be defined (to do nothing),
% which is done here.
%    \begin{macrocode}
\let\@finishbibunit\relax
%    \end{macrocode}
%
%
% \begin{macro}{\@finishstartedbibunit}
%    \changes{v2.2c}{2003/07/27}{Input of \macrott{@bibunitname.aux} added.}
%
%    Close the local auxiliary file.
%    \begin{macrocode}
\def\@finishstartedbibunit{%
  \if@filesw
    \immediate\closeout\@bibunitaux
  \fi
%    \end{macrocode}
%    Check for if the labels have changed and a ``Rerun to get
%    cross-references right'' warning needs to be given. The mechanism
%    is the same as in |\enddocument|: redefine |\bibcite| to check
%    if the labels have changed and then read the auxiliary file.
%    Since we have changed |\bibcite| to call directly |\@namedef|, we
%    use a variant |\@xtestdef| of \LaTeX's\ |\@testdef| for this job.
%    Also, we do not have to |\makeatletter| before reading
%    |\@bibunitaux|, because this file is supposed to contain only
%    bibliography related macros without any |@|'s.
%    But we do have to change |\endlinechar| as above to prevent
%    the creation of unwanted spaces.
%
%    \begin{macrocode}
  \ifx\bibunits@rerun@warning\@empty
    \if@filesw
       {\endlinechar-1
       \let\@namedef\@xtestdef
       \@input{\@bibunitname.aux}}%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\bibunits@rerunwaring}
%    \changes{v2.2c}{2003/07/27}{Macro added.}
%
%    Initialize |\bibunits@rerun@warning| as empty and call it
%    |\AtEndDocument|. The definition of |\bibunits@rerun@warning| may
%    be changed in macro |\@xtestdef|.  
%    \begin{macrocode}
\let\bibunits@rerun@warning\@empty
\AtEndDocument\bibunits@rerun@warning
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@xtestdef}
%    \changes{v2.2c}{2003/07/27}{Macro added.}
%
%    Check if labels created by |\bibcite| in the local auxiliary
%    files may have have changed. Analogous to |\@testdef| of \LaTeX.
%    \begin{macrocode}
\def\@xtestdef #1#2{%
  \def\reserved@a{#2}%
  \expandafter\ifx\csname#1\endcsname\reserved@a
  \else
    \gdef\bibunits@rerun@warning{%
      \PackageWarningNoLine{bibunits}{Label(s) may have changed.
        Rerun to get cross-references right}}%
  \fi
}     
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Bibunits by chapters or sections}
%
% |\chapter| or |\section| substituting definition.
%
% \begin{macro}{\old@bibunit}
% \begin{macro}{\@bibunit}
% \begin{macro}{\@endbibunit}
%    \begin{macrocode}
\let\old@bibunit\@gobble
\def\@bibunit{\endbibunit\bibunit\old@bibunit}
\def\@endbibunit{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\bibliographyunit}
%    \begin{macrocode}
\def\bibliographyunit{%
  \@endbibunit
  \@ifnextchar[{\@bibliographyunit}{%
    \global\let\old@bibunit\@gobble
    \global\let\bibliography\std@bibliography
    \global\let\bibliographystyle\std@bibliographystyle
    \endbibunit
    \gdef\@endbibunit{}}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@bibliographyunit}
%    \begin{macrocode}
\def\@bibliographyunit[#1]{%
  \global\let\bibliography\bu@bibliography
  \global\let\bibliographystyle\bu@bibliographystyle
  \global\let\old@bibunit#1
  \global\let#1\@bibunit
  \gdef\@endbibunit{\global\let#1\old@bibunit}%
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Insert a local bibliography}
% 
% \begin{macro}{\putbib}
%    If the \BibTeX\ data file is given as an optional argument, evoke
%    |\@putbib| which uses this argument, otherwise call |\@putbib|
%    with the default \BibTeX\ data file stored in |\bu@bibdata|.
%
%    \begin{macrocode}
\def\putbib{\@ifnextchar[{\@putbib}{\@putbib[\bu@bibdata]}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@putbib}
%    \changes{v2.1a}{1999/11/15}{Set \macrott{temp@auxout} and activate
%       \macrott{bu@addtocontents}.} 
%    \changes{v2.1c}{1999/11/24}{Change to respond to option
%        \option{labelstoglobalaux}.} 
%    \changes{v2.1g}{2000/07/31}{Add activation of
%       \macrott{bu@bibitem} and \macrott{bu@lbibitem}, remove activation of
%       \macrott{bu@addtocontents} and redefinition of \macrott{@auxout}.}
%    \changes{v2.1h}{2000/08/07}{Remove code for \option{labelstoglobalaux}
%       and activation of \macrott{bu@bibitem} and
%       \macrott{bu@lbibitem}.} 
%    \changes{v2.1i}{2000/09/30}{\macrott{@startbibunitorrelax} added.}
%    \changes{v2.1o}{2000/10/09}{Add local .bbl files to
%       \macrott{@filelist} by using \macrott{@input@} instead of 
%       \macrott{@input}.} 
%
%    Similar to |\bibliography|, |\@putbib| writes the \BibTeX\ data
%    and |\@input|s the typeset bibliography |\@bibunitname.bbl|. Macro
%    |\@startbibunitorrelax| triggers a new bibunit and allows
%    |\putbib| to be issued before the first cite command within a
%    bibunit.
%
%    \begin{macrocode}
\def\@putbib[#1]{%
  \@startbibunitorrelax
  \if@filesw
    \immediate\write\@bibunitaux{\string\bibdata{#1}}%
  \fi
  \@input@{\@bibunitname.bbl}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\bu@addtocontents}
%    \changes{v2.1a}{1999/11/15}{Add macro.}
%    \changes{v2.1g}{2000/07/31}{Remove macro.}
%
%    [Macro removed.]
% \end{macro}
%
%
% \noindent The package allows the same cited item to occur in more than one
% bibliography. Therefore the text which actually replaces the |\cite|
% has to be kept locally, too. This can be switched off using
% \option{labelstoglobalaux}. 
% 
% The text which replaces the |\cite| is created by the |\bibitem|
% commands in the |.bbl| file which is |\@input|ed in |\@putbib|. The
% |\bibitem| macro invokes macros |\@lbibitem| or |\@bibitem| which
% write to |\@auxout|. By default, i.e., without option
% \option{labelstoglobalaux} these commands are replaced by
% \package{bibunits} version to write to the local auxiliary file of the
% bibunit. The former meaning of |\@auxout| is saved in |\temp@auxout|
% to be restored afterwards.
% 
% Note that some packages, like \package{mparhack} or \komascript\ 
% classes like \class{scrartcl} with option \option{bibtotoc} also write
% material to |\@auxout| while inside |\thebibliography| within the
% |.bbl| file.  Therefore, redefining |\@auxout| in |\putbib| would
% result in erroneously writing this material to the local auxiliary
% file.  Therefore, only the commands which actually write, i.e.,
% |\@bibitem| and |\@lbibitem|, are redefined to write to the local
% auxiliary file.
% 
% Note that the redefined command |\bibliography| which generates a global
% bibliography uses the same mechanism to write the replacement text
% for the cite commands to |bu.aux|. 
%
%
% \begin{macro}{\std@bibitem}
% \begin{macro}{\std@lbibitem}
% \begin{macro}{\@bibitem}
% \begin{macro}{\@lbibitem}
%    \changes{v2.1g}{2000/07/31}{Add macros.}
%    \changes{v2.1h}{2000/08/07}{Direct redefinition of
%       \macrott{@bibitem} and \macrott{@lbibitem} instead of
%       definition of local \macrott{bu} versions, but only if
%       \macrott{labelstoglobalaux} is false. Setting of
%       \macrott{temp@auxout} added.}  
%
%    Define new versions of |\@bibitem| and |\@lbibitem| which write
%    to |\bibunitaux| instead of |\@auxout|.
%    Encapsulation with braces instead of the two |let|s results in 
%    |\@noitemerr| for the \textsf{thebibliography} environment.
%    \begin{macrocode}
\AtBeginDocument{%
  \iflabelstoglobalaux 
  \else 
    \let\std@bibitem\@bibitem
    \let\std@lbibitem\@lbibitem
    \def\@bibitem#1{%
      \let\temp@auxout\@auxout
      \let\@auxout\@bibunitaux
      \std@bibitem{#1}%
      \let\@auxout\temp@auxout 
    }
    \def\@lbibitem[#1]#2{%
      \let\temp@auxout\@auxout
      \let\@auxout\@bibunitaux
      \std@lbibitem[#1]{#2}%
      \let\@auxout\temp@auxout
    }
  \fi
}
%    \end{macrocode}
% \end{macro}                            
% \end{macro}                            
% \end{macro}                            
% \end{macro}                            
%
%
%
% \subsection{Allow commands to be used not only in preamble} 
%
% \changes{v1.0f}{1998/12/29}{Remove \macrott{bibcite} from 
%                             the list of preamble commands.}
%
% To handle citations of the same cited item in more than one
% bibliography, replacement text for the citation is written to the
% local auxiliary file. In subsequent runs of \LaTeX, this file is
% read at the beginning of the bibunit.
%
%
% \begin{macro}{\remequivalent}
% \begin{macro}{\plugh}
% \begin{macro}{\hgulp}
%    The local auxiliary file contains |\bibcite| commands, which can be
%    used only in the preamble in some older version of \LaTeX. For
%    compatibility with these versions, macros to remove an item from
%    a list are provided (see the \TeX book, p.\ 380; the only change
%    is the separator |\do| instead of |\\|).
%    \begin{macrocode}
\def\remequivalent#1\from#2{%
  \let\given=#1%
  \ifx#2\empty
  \else
    \edef#2{\expandafter\plugh#2\plugh}%
  \fi
}
\def\plugh\do#1#2{%
  \ifx#1\given
  \else
    \noexpand\do\noexpand#1%
  \fi
  \ifx#2\plugh
    \hgulp\fi\plugh#2%
}
\def\hgulp\fi\plugh\plugh{\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% Now we are ready to remove |\bibcite| from the |\@preamblecmds| list.
%    \begin{macrocode}
\remequivalent\bibcite\from\@preamblecmds
%    \end{macrocode}
%
%
% \medskip 
% The command |\bibcite| calls |\@newl@bel| to create new
% labels. This is by default only allowed as a preamble command and
% would cause a `Can be used only in preamble' warning. Instead of
% removing |\@newl@bel| from the list of preamble command (like
% |\bibcite|), we redefine |\bibcite| to directly call |\@namedef| and
% issue no warning. We decided not to give even an info, because
% multiple cited references should be perfectly normal to
% \package{bibunits}. Similarly, we modify \package{natbib}'s
% |\bibcite|.
%
%
% \begin{macro}{\bibcite}
%    \changes{v2.0a}{1999/06/02}{\package{natbib} compatibility.}
%    \changes{v2.0d}{1999/09/27}{Implementation streamlined.}
%    \changes{v2.1d}{2000/01/27}{\macrott{@namedef} not defined 
%                    \macrott{global}.}
%    \changes{v2.1h}{2000/08/06}{\macrott{@namedef} again defined 
%                    \macrott{global}.}
%
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{natbib}%
    {\renewcommand\bibcite[2]{\global\@namedef{b@#1\@extra@binfo}{#2}}}%
    {\renewcommand\bibcite[2]{\global\@namedef{b@#1}{#2}}}%
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Package \package{babel} compatibility}
%
% \begin{macro}{\@input}
% \begin{macro}{\std@@citex}
% \begin{macro}{\bu@@citex}
%    \changes{v2.1m}{2000/10/05}{Redefinition of \macrott{std@@citex},
%       \macrott{@input} and, if \package{natbib} is loaded, of
%       \macrott{bu@@citex}.}
%    \changes{v2.2e}{2003/08/18}{Package \package{jurabib} specific
%       redefinition of \macrott{std@@citex} and \macrott{bu@@citex}.}
%
%    If \package{babel} with options \option{frenchb} is loaded, some
%    characters like the colon `:' are made active. Since a colon
%    frequently appears within a cite key, active characters have to
%    be switched off for various commands. This can be done using
%    |\bbl@redefine|.  
%    
%    First, check if \package{babel} is loaded, by checking if |\bbl@redefine|
%    is defined. In this case, |\bbl@redefine| commands.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifundefined{bbl@redefine}%
  {}%
  {%
    \bbl@redefine\@input#1{%
      \@safe@activestrue\org@@input{#1}\@safe@activesfalse}%
    \@ifpackageloaded{natbib}%
    {%
      \bbl@redefine\std@@citex[#1][#2]#3{%
        \@safe@activestrue\org@std@@citex[#1][#2]{#3}\@safe@activesfalse}%
      \bbl@redefine\bu@@citex[#1][#2]#3{%
        \@safe@activestrue\org@bu@@citex[#1][#2]{#3}\@safe@activesfalse}%
    }%
    {
      \@ifpackageloaded{jurabib}%
      {%
        \bbl@redefine\std@@citex[#1][#2]#3{%
          \@safe@activestrue\org@std@@citex[#1][#2]{#3}\@safe@activesfalse}%
        \bbl@redefine\bu@@citex[#1][#2]#3{%
          \@safe@activestrue\org@bu@@citex[#1][#2]{#3}\@safe@activesfalse}%
      }%
      {% neither natbib nor jurabib loaded
        \bbl@redefine\std@@citex[#1]#2{%
          \@safe@activestrue\org@std@@citex[#1]{#2}\@safe@activesfalse}%
      }%
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \PrintIndex
% \PrintChanges
% \Finale
%
