% chapterbib.sty   Version 1.6 (27-SEP-1996) Donald Arseneau
%
% Allow multiple bibliographies in a LaTeX document, including items
% \cite'd in more than one bibliography.  
%
% Instructions are below, after \endinput.
%
%====================== BEGIN MACROS ===========================

\newenvironment{cbunit}%
{\global\advance\c@inputfile\@ne \xdef\the@ipfilectr{@-\the\c@inputfile}%
  \immediate\write\@auxout{\string\gdef\string\@extra@binfo{\@extra@b@citeb}}}%
{\gdef\the@ipfilectr{}%
  \immediate\write\@auxout{\string\gdef\string\@extra@binfo{\@extra@b@citeb}}}

\let\cb@include\include
\def\include#1{\clearpage \cbunit \xdef\@currentipfile{#1}%
  \cb@include{#1}\global\let\@currentipfile\@savedjobname\endcbunit}

\newcommand\cbinput[1]{\cbunit \xdef\@currentipfile{#1}%
  \input{#1}\global\let\@currentipfile\@savedjobname\endcbunit}

\@ifundefined{@extra@binfo}{
  \def\bibcite#1#2{\global\@namedef{b@#1\@extra@binfo}{#2}}
}{}

\@ifundefined{@extra@b@citeb}{%
 \def\@citex[#1]#2{%  Add \@extra@b@citeb to \cite
   \let\@citea\@empty
   \@cite{\@for\@citeb:=#2\do
     {\@citea\let\@citea\citepunct
      \edef\@citeb{\expandafter\@firstofone\@citeb}%
      \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
      \@ifundefined{b@\@citeb \@extra@b@citeb}{\mbox{\reset@font\bf ?}%
         \@warning{Citation `\@citeb' in file `\@currentipfile'
             on page \thepage \space undefined}\G@refundefinedtrue}%
      {\hbox{\citeform{\csname b@\@citeb \@extra@b@citeb\endcsname}}}}}{#1}}
%
\def\nocite#1{\@bsphack % Add \@extra@b@citeb to \nocite
  \@for\@citeb:=#1\do{%
    \edef\@citeb{\expandafter\@firstofone\@citeb}%
    \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
    \@ifundefined{b@\@citeb\@extra@b@citeb}{\G@refundefinedtrue
        \@warning{Citation `\@citeb' in file `\@currentipfile' undefined}}{}}%
  \@esphack}
%
}{}% 2nd arg of \@ifundefined{@extra@b@citeb}

\def\@firstofone#1{#1}

\@ifundefined{citeform}{\let\citeform=\relax}{}
\@ifundefined{citepunct}{\def\citepunct{,\penalty\@m\ }}{}

\xdef\@savedjobname{\jobname}% save root file name as a macro
\let\@currentipfile\@savedjobname
\gdef\the@ipfilectr{}
\def\@extra@b@citeb{\the@ipfilectr}
\newcount\c@inputfile \global\c@inputfile=0

\gdef\@extra@binfo{}  % in case .aux files are left from old run.

\@ifundefined{reset@font}{\let\reset@font\relax}{}
\@ifundefined{G@refundefinedtrue}{\let\G@refundefinedtrue\relax}{}

\let\cb@bibliographystyle\bibliographystyle
\def\cb@roottest{2\ifx\the@ipfilectr\@empty 1\else2\fi}

\def\bibliographystyle#1{\if\cb@roottest\cb@bibliographystyle{#1}\fi}

\def\bibliography#1{\if\cb@roottest
   \if@filesw\immediate\write\@auxout{\string\bibdata{#1}}\fi
   \fi \@input{\@currentipfile.bbl}}

\@ifundefined{PackageWarning}{\let\cb@warning\@warning}%
 {\def\cb@warning{\PackageWarning{chapterbib}}}

\let\@test@defiii\@testdef

\def\@testdef#1#2#3{%
  \if#1b\relax\@test@defiii b{#2\@extra@binfo}{#3}%
  \else \@test@defiii #1{#2}{#3}\fi}

\def\sectionbib#1#2{% Macro to convert \thebibliography command
 \long\def\@tempa##1##{% to parse beginning of old def
  \long\def\@tempa####1\endthebibliography{}\def\@tempb{##1}\@tempa}%
 \expandafter\@tempa\thebibliography{}{}\endthebibliography
 \begingroup \toks@{#1}%
 \cb@warning {The sectionbib option changes \string"\expandafter
  \strip@prefix\meaning\@tempb\string" to \string"\the\toks@\string"
  in \string\thebibliography }\endgroup
 \let\cb@thebibliography\thebibliography 
 \def\thebibliography{\let\@currentlabel\@empty
   \ifx\the@ipfilectr\@empty \else
   \expandafter\expandafter\expandafter\cb@sectionbib\fi 
   \cb@thebibliography}%
 \expandafter\def\expandafter\cb@sectionbib\@tempb##1{#1{\bibname
 \ifx\@mkboth\@gobbletwo\else\markright{\uppercase{\bibname}}\fi% like \@mkright
 \ifx\@currentlabel\@empty \addcontentsline{toc}{#2}{\bibname}\fi}}}

\def\strip@prefix#1>{}
\@ifundefined{bibname}{\def\bibname{Bibliography}}{}

\@ifundefined{DeclareOption}{}{% LaTeX2e option
\ProvidesPackage{chapterbib}[1996/08/27 \space v 1.6]
\DeclareOption{sectionbib}{\AtBeginDocument{\sectionbib{\section*}{section}}}
\DeclareOption{draft}{\let\cbinput=\include}
\DeclareOption{rootbib}{\def\cb@roottest{2\ifx\the@ipfilectr\@empty2\else1\fi}}
\ProcessOptions 
\AtBeginDocument{\let\sectionbib\undefined}
}

\endinput

%====================== BEGIN INSTRUCTIONS ===========================


% chapterbib.sty   Version 1.6 (27-Sep-1996) Donald Arseneau (more sectionbib)
% ~~~~~~~~~~~~~~           1.5 (09-OCT-1995) Donald Arseneau (rootbib)
%                          1.4 (11-MAR-1995) Donald Arseneau (sectionbib)
%                          1.3 (04-JUL-1994) Donald Arseneau (2e, cbunit)
%                          1.2 (21-MAY-1993) Donald Arseneau (bug fix)
%                          1.1 (24-MAR-1993) Donald Arseneau
%                          1.0 (23-NOV-1988) Niel Kempson
%
% Allow multiple bibliographies in a LaTeX document, including items
% \cite'd in more than one bibliography.  Despite the name "chapterbib",
% the BIBLIOGRAPHIES ARE FOR EACH INCLUDED FILE, not necessarily for each
% chapter.  The main point is to allow you to use BibTeX: Each included 
% file should have its own \bibliography command, and you should run
% BibTeX on each included file separately rather than on the root file.
%
% There are also the commands \begin{cbunit}, \end{cbunit}, and \cbinput
% to allow multiple bibliographies without using \include (see item 4).
% There are two added hooks, \citeform and \citepunct, to customise the 
% formatting of each entry in a citation list.
%
% Usage, Restrictions, and Options
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% 1. Normal usage: Put a \bibliography command in each \include'd file; 
%    run LaTeX; run BibTeX on each included file; run LaTeX; run LaTeX.
% 
% 2. If you get errors like "! LaTeX Error: Command \xxx already defined."
%    then bibtex is foolishly putting "\newcommand" in each bbl file.  The
%    fix is to put the \bibliography command in braces: {\bibliography{x}}.
% 
% 3. The \bibliography and \bibliographystyle commands are not normally used 
%    in the root file, only in files that have been \include'd. To have a 
%    whole-document bibliography plus chapter-bibs, see item 5; for an 
%    independent partial bib in the root file, go on to item 4.
% 
% 4. If you want a completely independent bibliography in the root file, 
%    you must provide your own bibliography there using the thebibliography 
%    environment.  I don't suppose this will appeal to BibTeX users! 
%
% 5. To have a bibliography for the whole document, plus individual bibs,
%    put \bibliography commands in the included chapters plus in the root
%    file; use \usepackage[rootbib]{chapterbib}; run LaTeX; run BibTeX
%    on the root file; change to \usepackage{chapterbib}; run LaTeX; run 
%    BibTeX on each included file; run LaTeX; run LaTeX.
%    An `overall' bibliography only makes sense for various `named' 
%    bibliography styles; a style with numbering will give separate and 
%    unrelated numbers in each bibliography.
%
% 6. If you can't use \include because a new section must start below the
%    preceding bibliography on the same page [odd format!], then you can
%    use \begin{cbunit}...\end{cbunit} or \cbinput, with a {thebibliography}
%    environment in each unit or input file.  To use BibTeX: input separate 
%    files using \cbinput; at first use the package or global option [draft],
%    run LaTeX on the document, then BibTeX on each file that was \cbinput; 
%    finally, remove the [draft] option and run LaTeX again (maybe twice to 
%    get page references right).  The [draft] option only affects the treatment
%    of \cbinput, not \include or \begin{cbunit}.
%        With old LaTeX, do the preliminary run using \include commands, and
%    change these to \cbinput for the final run(s).
% 
% 7. Your preferred citation style (xxx.sty) may not work with chapterbib at
%    first, but it is easy to make it compatible:  In `xxx.sty' change every
%    "b@\@citeb" to "b@\@citeb\@extra@b@citeb", and insert the line
%        \@ifundefined{@extra@b@citeb}{\def\@extra@b@citeb{}}{}
%    somewhere (but not as a comment or as part of another definition!). 
%    If the style file also redefines \bibcite then you should change that
%    definition, replacing "b@#1" with "b@#1\@extra@binfo", and insert 
%        \gdef\@extra@binfo{}
%    somewhere in the file.
%
% 8. The report and book document classes usually treat the bibliography as 
%    an unnumbered chapter (\chapter*), which is not so good for bibliographies 
%    IN a chapter.  You can specify  
%        \usepackage[sectionbib]{chapterbib}  
%    and your bibliographies will be treated as sections (\section*) with an
%    entry in the table of contents and the page-header.  A bibliography in
%    the root file remains as a \chapter*.  The [sectionbib] option modifies 
%    the existing thebibliography environment, so the other formatting in the 
%    bibliography should remain unchanged.  On the other hand, if you already 
%    have a non-standard bibliography defined, or if you want them numbered,
%    it may be easier to redefine \thebibliography directly, without any 
%    trickery.
%        For more control, or in old LaTeX2.09, you can use the \sectionbib 
%    command directly in the document preamble.  It takes two parameters:
%    the sectioning command, and the name of the sectioning level.  For
%    instance, the [sectionbib] option does \sectionbib{\section*}{section}.
%
% \citeform and \citepunct:
% ~~~~~~~~~~~~~~~~~~~~~~~~~
% Normally, the citations are formatted as given, but you can define \citeform
% (with one parameter) to reformat every citation.  Some possibilities:
%  \renewcommand\citeform[1]{\romannumeral 0#1}% roman numerals:  [iv,x]
%  \renewcommand\citeform[1]{(#1)}             % parentheses:  [(3),(4),(7)]
%  \renewcommand\citeform[1]{\thechapter.#1}   % number by chapter:  [3.9,3.10]
% If you change \citeform, you should define \@biblabel to match.
% \citepunct gives the punctuation (comma-penalty-space) between items.
%
% -----------------Implementation notes---------------
%
% LaTeX normally uses command names in the form \b@TAG to associate a mnemonic
% TAG with the citation name or number.  Chapterbib changes this to a command
% like \b@TAG@-nn where nn is a number identifying which included file is in
% effect.
%
% Tags indicating the citations and/or the input files:
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% \c@inputfile    = counter counting included files
% \the@ipfilectr  = (empty) when typesetting from the root file,
%                 = @-\the\c@inputfile when typesetting from an included file
% \@extra@b@citeb = \the@ipfilectr (just an alias)
% \@extra@binfo   = the value of \@extra@b@citeb as saved in .aux files
%
% \b@FOO:
% ~~~~~~~
% In the root file, the citation number (or name) is given by \b@FOO
% just like regular LaTeX, but in an \include'd file it is given by
% \b@FOO@-number.  Any definition of \cite (\@citex) should refer
% to this using \csname b@\@citeb\@extra@b@citeb \endcsname.
%
% \include \cbinput and \cbunit:
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Redefine the \include macro so that when a file is \include'd...
% Increment the file number, (globally) update \the@ipfilectr to be 
% @-number, and write \gdef\@extra@binfo{@-number} in the (main) .aux 
% file, and do regular \include. When the \include'd file is finished, 
% write \gdef\@extra@binfo{} in the (main) .aux file. Globally reset 
% \the@ipfilectr to {}.  Provide similar \cbunit and \cbinput.
%
% \cite:
% ~~~~~~
% Redefine \cite (\@citex actually) and \bibcite to use the file number
% tag along with the specified tag.  \@citex also uses \citeform as a hook 
% to reformat each individual citation.
% Only do the redefinitions if no citation style that supports chapterbib 
% has been loaded--as indicated by existence of filename tags.
%
% \citepunct and \citeform:
% ~~~~~~~~~~~~~~~~~~~~~~~~~
% These customization hooks are present in cite.sty; others (\citemid, 
% \citeleft, \citeright) are not defined here because \@cite is not redefined.

% We redefine \@testdef so that it properly checks whether the 
% \cite labels have changed.

% Handle package options and redefining thebibliography.  [sectionbib]
% converts "\chapter*{...}" to "\section*{\bibname\markright{\bibname}%
%   \addcontentsline{toc}{section}{\bibname}}"

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

% Authors  Niel Kempson        (original)
% ~~~~~~~  Donald Arseneau  <asnd@reg.triumf.ca>   March 1993 - Sept 1996
%
% Changes with ver 1.6: change the \sectionbib command to take arguments.  
% Changes with ver 1.5: rootbib option; item 2 in instructions.
% Changes with ver 1.4: sectionbib and draft options
% Changes with ver 1.3: rewrite: 
% The filename tag is defined in each .aux file by \include, not as an
% extra parameter to \bibcite.  \@bibitem & \@lbibitem are left alone.  Add 
% \citeform and \citepunct. Add \cbunit and \cbinput for use without \include.
