%% @texfile{
%%   author = "Karl Berry and Oren Patashnik",
%%   version = "0.99p",
%%   date = "30 May 2019",
%%   filename = "btxmac.tex",
%%   address = "tex-eplain@tug.org",
%%   supported = "yes",
%%   docstring = "Defines macros that make BibTeX work with plain TeX",
%% }
% BibTeX-for-TeX macros for BibTeX 0.99c, TeX 3.0 or later.
% Copyright 1990, 1991, 1992, 1995, 2005, 2007, 2013, 2016, 2019 Karl Berry &
% Oren Patashnik.
% 
% Unlimited copying and redistribution of this file are permitted as
% long as this file is not modified. Modifications (and redistribution
% thereof) are permitted, but only if the resulting file is not named
% btxmac.tex.
% 
% This file contains TeX macros that allow BibTeX, a bibliography
% program that was originally designed for use with LaTeX, to work with
% plain TeX.  Please report any bugs (outright goofs, improvable macros,
% misfeatures, or unclear documentation) to Oren Patashnik
% (opbibtex@cs.stanford.edu).  These macros will become frozen shortly
% after BibTeX version 1.00 is released.
%
% AMS-TEX WARNING: We tried very hard, as of version .99i of these macros,
% to make them compatible with AmS-TeX.  We succeeded to the extent
% that, if you use one of the standard bibliography styles, you probably
% won't notice any problems with version 0.99i of btxmac.tex.  But
% ultimately we failed, in that the inherent incompatibilities between
% plain TeX and AmS-TeX kept making these macros break, for certain
% inputs or certain styles.  Examples:  (1) AmS-TeX treats at-signs as
% special, in ways that plain TeX and LaTeX don't, so that, for example,
% you can't have any `@' characters in an argument to the \cite command,
% the way you can in TeX or LaTeX; (2) AmS-TeX decided that plain TeX's
% and LaTeX's macron-accent control sequence `\=' should be undefined;
% so you'll need to define `\=' to be `\B' to get the xampl.bib example
% suggested below to work with AmS-TeX; (3) AmS-TeX redefines the tie
% character `~' of plain TeX, and AmS-TeX's `amsppt' style redefines
% plain TeX's `\nobreak' macro, so that if you use an author-date style
% like `apalike' and you have a multiple-author reference for which the
% author-date style automatically produces a citation in the text like
% `(Jones et~al., 1992)' you will throw AmS-TeX's `amsppt' style into
% an infinite loop, exceeding its input stack size.
% 
% In practice, such incompatibilities surface infrequently; but it is
% now clear to us that it's not worth the effort (perhaps it's not even
% possible) to make the btxmac.tex macros robust when used both with
% plain TeX and Ams-TeX.  If the BibTeX/AmS-TeX results attainable with
% the current btxmac.tex macros are sufficient, fine.  But if there's a
% demand for more robust BibTeX/AmS-TeX behavior, then someone who's
% very familiar with the AmS-TeX package should probably make an
% amsbtxmc.tex version of the macros.  Until then, if you're an AmS-TeX
% user, or a LaTeX or plain TeX user sharing files with an AmS-TeX user,
% beware.
% END OF AMS-TEX WARNING.
%
% To use these macros you should be familiar with how BibTeX interacts
% with LaTeX, since BibTeX's interaction with TeX is very similar; that
% interaction is explained in the LaTeX manual.  It also helps to
% have read "BibTeXing", the documentation that accompanies BibTeX.
% 
% Then, if you want, you should redefine any of the macros that begin
% with `\bbl' or `\biblabel' or `\print' that you need to get formatting
% different from the default (the default settings are designed to
% accompany a bibliography style like BibTeX's standard style `plain').
% The macros you might want to change are described briefly a few
% paragraphs hence.
% 
% To get started without reading any documentation, try running the
% short .tex file below through TeX and BibTeX.
% 
% Remember the general scheme: Running (La)TeX writes information on the
% .aux (auxiliary) file; then running BibTeX reads information from the
% .aux, .bst (style), and .bib (database) files, and writes information
% (the bibliography) on a .bbl file; then running (La)TeX incorporates
% the bibliography; then running (La)TeX once more fixes the remaining
% forward references into the bibliography.  Thus, to get everything
% incorporated into your output, you'll have to run (La)TeX, BibTeX,
% (La)TeX, (La)TeX.  (Standup, sitdown, fight, fight, fight.)
%
% These macros can stand alone or they can be \input into a macro
% package, like Eplain, that is sufficiently compatible with plain TeX.
% To use these macros to format the 0.99 version of the xampl.bib file
% that's distributed with BibTeX (that version of the file has no
% self-identification), you'll need to define \mbox, which is a LaTeX
% command, to be \hbox, as in the example below.
%
% Here's an eight-line plain TeX file for trying out btxmac.tex; of course
% you'll have to remove the comment characters at the beginning of each
% line, and, depending on your system, you might have to take steps so
% that BibTeX can "see" the files xampl.bib and plain.bst (BibTeX will
% give you two empty-field warning messages that you should ignore).
%
%     \input btxmac
%     \noindent This cites Aamport's gnominious article~\cite{article-full}.
%     \medskip
%     \leftline{\bf References}
%     \nocite{*}   % put all database entries into the reference list
%     \bibliographystyle{plain}   % specify plain.bst as the style file
%     \bibliography{xampl}   % specify the database files; here, just xampl.bib
%     \bye
%
%
%   HISTORY
%
% Karl Berry wrote the original version of these macros in 1989 and
% 1990, for use in his `Eplain' package.  Oren Patashnik modified them
% slightly in July 1990, as part of the official BibTeX distribution.
%
%    1-Aug-90  Version 0.99a, not released to the general public.
%   14-Aug-90  0.99b, first general release.
%   26-Aug-90  0.99c, made \@undefinedmessage work with other macro packages.
%    6-Sep-90  0.99d, allowed for general formatting of bibliography labels,
%                     for general formatting of (in-text) citations, and for
%                     changing certain catcodes while reading the .aux file.
%   14-Nov-90  0.99e, changed the way \@setletters works, made some \new...'s
%                     non-outer, and changed the way Eplain reads this file.
%   12-Dec-90  0.99f, made \@resetnumerals change the `,' and `.' catcodes; and
%                     added \biblabelextrahang, \@getoptionalarg, and \bblsc.
%   11-Mar-91  0.99g, made a few minor changes required by the way Eplain reads
%                     this file, but no functional changes.
%   24-Apr-91  0.99h, inhibited the reading and writing of the .aux file if it
%                     isn't used or if the \noauxfile macro is defined, and
%                     removed some .aux-file-opening detritus; printed the
%                     cite-key of undefined citations in \tt font; changed the
%                     catcode of `_' inside \cite; and called \@resetnumerals
%                     from inside a group.
%   29-Feb-92  0.99i, made these macros semi-compatible with AmS-TeX; removed
%                     \@resetnumerals, \@setletters, \@tokstostring, and
%                     friends; changed the way \cite handles catcodes; changed
%                     \@getoptionalarg, and had \bibitem and \newcommand use
%                     it; added \@futurenonspacelet and (to facilitate the use
%                     of multiple reference lists) \bblfilebasename; changed
%                     \biblabelprint to use the new macros \biblabelprecontents
%                     and \biblabelpostcontents, and to, by default, right-
%                     justify numeric labels; and renamed \biblabelextrahang to
%                     the more descriptive \biblabelextraspace.
%   14-Mar-92  0.99j, made 0.99i's use of `\\' local to btxmac.tex.
%   13-Nov-95  0.99k, defined \begin and \end (while reading bbl file) only if
%                     it wasn't already defined.
%   11-Aug-03  0.99l, defined \renewcommand and \providecommand, since some AMS
%                     style files use them.  Also defined \emph, \textbf, and
%                     \mbox, since many .bib files use them.  This version was
%                     never actually released, but these changes appear
%                     starting with 0.99k-18aug05.
%   18-Aug-05  0.99k-18aug05, made the \em, \emph, etc., definitions effective
%                             only if not already defined.  Provided
%                             \printcite{pre,post}item, \bblitemhook.
%   09-Sep-07  0.99k-09sep07, executed \@citedef in a group to avoid potential
%                             save-stack overflow.
%   07-Feb-13  0.99m, made \newcommand and \providecommand be \long, and not
%                     use \edef.  Defined \bbltextbf.
%   30-Sep-13  0.99n, made \@futurenonspacelet be \long,
%                     renamed \@optionaltemp to \@optionalusercs,
%                     ignored spaces after the optional argument.
%   12-May-16  0.99o, renamed \@newcommand and others to \@btx..., to avoid
%                     conflict with Babel's file plain.def.
%   30-May-19  0.99p, one missed rename of \@continuenewcommand to \@btx...,
%                     that made \providecommand fail.
%                     
% The LaTeX-related commands defined in this file include
% (a) the four commands that a user types (\bibliography,
%     \bibliographystyle, \cite, and \nocite); 
% (b) three commands that BibTeX looks for in the .aux
%     file (\bibdata, \bibstyle, and \citation---there is a fourth command
%     that BibTeX looks for, but that command is related to LaTeX's \include
%     facility, so these macros ignore that command); and 
% (c) the LaTeX commands (\newcommand, \renewcommand, \providecommand)
%     that are written by a standard (alpha) or AMS bibliography style.
% (d) a few more LaTeX commands (\emph, \mbox, \textbf) which many .bib
%     files use.
%     
% The definitions here are not the same as the corresponding LaTeX
% definitions (those LaTeX definitions depend on a significant fraction
% of LaTeX itself).  But the only significant differences are with the
% \*command macros, which in LaTeX (a) give warnings in various
% circumstances; (b) allow a second optional argument; and (c) make the
% defined control sequences \long.  The versions here don't do any of
% those things.
% 
% To summarize: Unless you know what you're doing, you shouldn't define
% any control sequences with these ten names:
%
% \bibdata
% \bibliography
% \bibliographystyle
% \bibstyle
% \citation
% \cite
% \newcommand
% \nocite
% \providecommand
% \renewcommand
% 
% There are other commands written by one or more of the four standard
% (plain, abbrv, alpha, unsrt) or four semistandard (acm, apalike,
% ieeetr, siam) bibliography styles, or commonly used in .bib files.
% These take effect only within the bibliography, and are redefinable,
% as explained later:
%
% \em
% \emph
% \mbox
% \newblock
% \sc
% \textbf
%
% There's one control sequence you might want to use (but not redefine)
% in redefining \biblabelprint:
%
% \biblabelwidth
%
% There are fifteen other control sequences (explained later in more detail)
% that the macros of this file will use if you define them---you should
% define them after the \input btxmac command but before the \bibliography
% command.  The first six begin with `\bbl' and affect fonts, spacing,
% perhaps other characteristics of the bibliography, and which .bbl files
% get read; the next five begin with `\biblabel' and determine how labels
% are formatted in the bibliography; and the last four begin with `\print'
% and determine how the in-text citations are formatted:
%
% \bblem
% \bblfilebasename
% \bblhook
% \bblnewblock
% \bblrm
% \bblsc
% \biblabelcontents
% \biblabelprecontents
% \biblabelprint
% \biblabelpostcontents
% \biblabelextraspace
% \printbetweencitations
% \printcitefinish
% \printcitenote
% \printcitestart
%
% If it's defined before the \input btxmac command, the control sequence
% below inhibits the reading and writing of the .aux file(s), and the
% issuing of related warning messages.  Any definition will do.  This
% feature might help when you're working on draft stages of a document:
%
% \noauxfile
%
%
% Here's another control sequence (it's described later) that you
% probably won't want to redefine unless you are writing another macro
% package; if you do redefine it, however, do it before the \input btxmac
% command (and notice that it has an `@' in its name):
%
% \@undefinedmessage
%
% Any other control sequence in this file that might conflict with
% something you've defined will have an `@' in its name, so such conflicts
% are unlikely; but if you're worried about a specific control sequence
% name, do a text search of this file to look for it.
%
%
% So to start things off we turn `@' into a letter (category code 11),
% keeping track of the old category code for future restoration.
% (Simply resetting it to 12 when we leave these macros is
% insufficient.)  We use `\cite' as a temporary control sequence to
% save the current catcode of @ to avoid possibly overwriting something
% (without an `@' in its name) that might already be defined.
%
\edef\cite{\the\catcode`@}%
\catcode`@ = 11
\let\@oldatcatcode = \cite
\chardef\@letter = 11
\chardef\@other = 12
%
%
% Next come some things that will be useful later.
%
% Make an outer definition into an inner one (due to Chris Thompson).
% The arguments should be the control sequence to be defined, and the
% new of the \outer control sequence, as characters; the control
% sequence #1 is defined to be just the same as \csname#2\endcsname, but
% not \outer.  For example, \@innerdef\innernewcount{newcount} would
% define \innernewcount to be a non-outer version of \newcount.
%
\def\@innerdef#1#2{\edef#1{\expandafter\noexpand\csname #2\endcsname}}%
%
% We use \@innerdef to make some of our allocations local, because
% Eplain includes our code inside a conditional.  We put @'s in the
% names to minimize the (already small) chance of conflicts.
%
\@innerdef\@innernewcount{newcount}%
\@innerdef\@innernewdimen{newdimen}%
\@innerdef\@innernewif{newif}%
\@innerdef\@innernewwrite{newwrite}%
%
%
% Swallow one parameter.
%
\def\@gobble#1{}%
%
%
% Use TeX 3.0's \inputlineno to get the line number, for better error
% messages, but if we're using an old version of TeX, don't do anything.
%
\ifx\inputlineno\@undefined
   \let\@linenumber = \empty % Pre-3.0.
\else
   \def\@linenumber{\the\inputlineno:\space}%
\fi
%
%
% The following macro \@futurenonspacelet (from the TeXbook) behaves
% essentially like \futurelet except that it discards any implicit or
% explicit space tokens that intervene before a nonspace is scanned:
%
\long\def\@futurenonspacelet#1{\def\cs{#1}%
   \afterassignment\@stepone\let\@nexttoken=
}%
\begingroup % The grouping here avoids stepping on an outside use of `\\'.
\def\\{\global\let\@stoken= }%
\\ % now \@stoken is a space token (\\ is a control symbol, so that
   % space after it is seen).
\endgroup
\def\@stepone{\expandafter\futurelet\cs\@steptwo}%
\def\@steptwo{\expandafter\ifx\cs\@stoken\let\@@next=\@stepthree
   \else\let\@@next=\@nexttoken\fi \@@next}%
\def\@stepthree{\afterassignment\@stepone\let\@@next= }%
%
%
% \@getoptionalarg\CS gets an optional argument from the input, enclosed
% in brackets, then expands \CS.  We set \@optionalarg to \empty if we
% don't find one, otherwise to the text of the argument.  This assumes
% the brackets don't have a funny category code.
%
\def\@getoptionalarg#1{%
   \let\@optionalusercs = #1%
   \let\@optionalnext = \relax
   \@futurenonspacelet\@optionalnext\@bracketcheck
}%
%
% The \expandafter's in this macro let us avoid the use of \aftergroup,
% which is somewhat more expensive.
%
\def\@bracketcheck{%
   \ifx [\@optionalnext
      \expandafter\@@getoptionalarg % we have an optional arg
   \else
      \let\@optionalarg = \empty    % no optional arg
      \expandafter\@optionalusercs
   \fi
}%
%
% Get the next bracketed text.
\def\@@getoptionalarg[#1]{%
   \def\@optionalarg{#1}%
   % Ignore spaces after the optional arg, too.
   % (We don't actually need to peek ahead, but this is the easiest way
   % to ignore the spaces.)
   \let\@optdummy=\relax % just in case it has become \outer somehow
   \@futurenonspacelet\@optdummy\@optionalusercs
}%
%
%
% From LaTeX.
%
\def\@nnil{\@nil}%
\def\@fornoop#1\@@#2#3{}%
%
\def\@for#1:=#2\do#3{%
   \edef\@fortmp{#2}%
   \ifx\@fortmp\empty \else
      \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}%
   \fi
}%
%
\def\@forloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@iforloop #3\@@#4{#5}\fi\fi
}%
%
\def\@iforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
       \let\@nextwhile=\@fornoop \else
      #4\relax\let\@nextwhile=\@iforloop\fi\@nextwhile#2\@@#3{#4}%
}%
%
%
% This macro tests if a file \jobname.#1 exists, and sets \if@fileexists
% appropriately.  If an optional argument is given, it is used as the
% root part of the filename instead of \jobname.
%
\@innernewif\if@fileexists
%
\def\@testfileexistence{\@getoptionalarg\@finishtestfileexistence}%
\def\@finishtestfileexistence#1{%
   \begingroup
      \def\extension{#1}%
      \immediate\openin0 =
         \ifx\@optionalarg\empty\jobname\else\@optionalarg\fi
         \ifx\extension\empty \else .#1\fi
         \space
      \ifeof 0
         \global\@fileexistsfalse
      \else
         \global\@fileexiststrue
      \fi
      \immediate\closein0
   \endgroup
}%
%
%
%% [[[start of BibTeX-specific stuff]]]
%
% Now come the four main LaTeX commands and their associated .aux
% commands.  Just as in LaTeX, \bibliographystyle defines the BibTeX
% style name (.bst file, that is), and \bibliography defines the
% database (.bib) file(s).  The corresponding .aux-file commands are
% \bibstyle and \bibdata, which are there only for BibTeX's (but not
% LaTeX's) use.
%
\def\bibliographystyle#1{%
   \@readauxfile
   \@writeaux{\string\bibstyle{#1}}%
}%
\let\bibstyle = \@gobble
%
% As well as writing the \bibdata command to tell BibTeX which .bib
% files to read, we read the .bbl file that BibTeX (or a person,
% conceivably) has produced.  We use \bblfilebasename as the root of the
% filename to read; this defaults to \jobname.
%
\let\bblfilebasename = \jobname
\def\bibliography#1{%
   \@readauxfile
   \@writeaux{\string\bibdata{#1}}%
   \@testfileexistence[\bblfilebasename]{bbl}%
   \if@fileexists
      % We just output a non-discardable item (the `whatsit' with the
      % \bibdata command).  This means that the glue that will be
      % inserted next (\parskip or \baselineskip, most likely) will be a
      % legal breakpoint.  Most likely, this is after some kind of
      % heading, where we don't want to allow a page break.  So:
      \nobreak
      \@readbblfile
   \fi
}%
\let\bibdata = \@gobble
%
% The \nocite{label,label,...} command writes its argument to \@auxfile,
% unless instructed not to, but produces no text in the document.  Both
% the \nocite and \cite commands produce \citation commands in the .aux file.
%
\def\nocite#1{%
   \@readauxfile
   \@writeaux{\string\citation{#1}}%
}%
%
\@innernewif\if@notfirstcitation
%
% \cite[note]{label,label,...} produces the citations for the labels as
% well.  If the optional argument `note' is present, it's added after
% the labels.  Since \cite calls \nocite to do its .aux-file writing,
% \cite doesn't need to call \@readauxfile (\nocite does).
%
\def\cite{\@getoptionalarg\@cite}%
%
% Typeset the citations for the labels in #1, followed by the note, if
% it exists.  To change the citation's format in the text, redefine one
% or more `\print...' macros, whose defaults appear later in this file.
%
\def\@cite#1{%
   % Remember the optional argument, in case one of the macros we call
   % below ends up looking for an optional argument itself.  For
   % example, if a \cite[note] triggers reading the .aux file, then the
   % [note] would be clobbered, since \@testfileexistence looks for an
   % optional arg.
   \let\@citenotetext = \@optionalarg
   % Start printing the text, beginning with a left bracket by default.
   \printcitestart
   % It's complicated, but because \nocite puts a `whatsit' onto the list,
   % \nocite should follow \printcitestart.  It's conceivable, but very
   % unlikely, that this `whatsit' will cause a problem (glue that doesn't
   % disappear when you want it to is the most likely symptom), requiring
   % a change either to \printcitestart or to the label that the .bst file
   % produces.
   \nocite{#1}%
   \@notfirstcitationfalse
   \@for \@citation :=#1\do
   {%
      \expandafter\@onecitation\@citation\@@
   }%
   \ifx\empty\@citenotetext\else
      \printcitenote{\@citenotetext}%
   \fi
   \printcitefinish
}%
%
\def\@onecitation#1\@@{%
   \if@notfirstcitation
      \printbetweencitations
   \fi
   %
   \expandafter \ifx \csname\@citelabel{#1}\endcsname \relax
      \if@citewarning
         \message{\@linenumber Undefined citation `#1'.}%
      \fi
      % Give it a dummy definition:
      \expandafter\gdef\csname\@citelabel{#1}\endcsname{%
         {\tt
            \escapechar = -1
            \nobreak\hskip0pt
            \expandafter\string\csname#1\endcsname
            \nobreak\hskip0pt
         }%
      }%
   \fi
   % Now produce the text, whether it was undefined or not.
   \printcitepreitem{#1}%
   \csname\@citelabel{#1}\endcsname
   \printcitepostitem
   \@notfirstcitationtrue
}%
%
% Given a label `foo', the macro `\b@foo' is supposed to
% hold the text that should be produced.
%
\def\@citelabel#1{b@#1}%
%
% So, how does a citation label get defined?  When we read the .bbl file
% (below), a \bibitem writes out a \@citedef command.  And when we read
% the \@citedef, we define \@citelabel{#1}, where #1 is the user's
% label.
%
% The group around the def will keep the save stack from overflowing:
% We are reading the .aux file inside a group.  For undefined
% commands, \csname...\endcsname makes them equivalent to \relax
% locally in that group; when we globally redefine them, TeX enters
% the definitions on the save stack, once for each label.  When done
% in the topmost group, this can overflow a stack of any size, given
% sufficient number of label definitions.  So, we put these
% \csname...\endcsname's inside another group, to pop the stack for
% every label.
\def\@citedef#1#2{{\expandafter\gdef\csname\@citelabel{#1}\endcsname{#2}}}%
%
%
% Reading the .bbl file also produces the typeset bibliography.  Please
% notice, however, that we do not produce the title for the references
% (e.g., `References'), as LaTeX does.  The formatting and spacing of
% that title, whether it should go into the headline, and so on, are all
% things determined by your format.  We cannot know those things in
% advance.  If you wish, you can define \bblhook to produce the title.
% Or just do it before the \bibliography command.
%
\def\@readbblfile{%
   % Define a counter to tell us which item number we are on, unless
   % we've already defined it (because the document has more than one
   % bibliography).
   \ifx\@itemnum\@undefined
      \@innernewcount\@itemnum
   \fi
   %
   \begingroup
      % If another package has already defined \begin, don't define our
      % own simplistic \begin and \end; assume they want to take care of
      % it themselves.  (That way, their \begin's and \end's for other
      % things can be used in the bib files.)
      \ifx\begin\@undefined
         \def\begin##1##2{%
            % ##1 is just `thebibliography'.
            % ##2 is the widest label.
            % We set (new dimen) \biblabelwidth based on the widest label
            \setbox0 = \hbox{\biblabelcontents{##2}}%
            \biblabelwidth = \wd0
         }%
         \let\end = \@gobble % The arg is `thebibliography' again.
      \fi
      %
      % Here we have two possibilities:
      % \bibitem[typesetlabel]{citationlabel}
      % \bibitem{citationlabel}
      % If we have the second of these, the citations are numbered, starting
      % from one; we use our own count register \@itemnum for this.
      %
      \@itemnum = 0
      \def\bibitem{\@getoptionalarg\@bibitem}%
      \def\@bibitem{%
         \ifx\@optionalarg\empty
            \expandafter\@numberedbibitem
         \else
            \expandafter\@alphabibitem
         \fi
      }%
      \def\@alphabibitem##1{%
         % Need \xdef here for various reasons.
         \expandafter \xdef\csname\@citelabel{##1}\endcsname {\@optionalarg}%
         % Left-justify alpha labels, unless \biblabel{pre,post}contents
         % are already defined.
         \ifx\biblabelprecontents\@undefined
            \let\biblabelprecontents = \relax
         \fi
         \ifx\biblabelpostcontents\@undefined
            \let\biblabelpostcontents = \hss
         \fi
         \@finishbibitem{##1}%
      }%
      %
      \def\@numberedbibitem##1{%
         \advance\@itemnum by 1
         \expandafter \xdef\csname\@citelabel{##1}\endcsname{\number\@itemnum}%
         % Right-justify numeric labels, unless \biblabel{pre,post}contents
         % are already defined.
         \ifx\biblabelprecontents\@undefined
            \let\biblabelprecontents = \hss
         \fi
         \ifx\biblabelpostcontents\@undefined
            \let\biblabelpostcontents = \relax
         \fi
         \@finishbibitem{##1}%
      }%
      %
      \def\@finishbibitem##1{%
         \bblitemhook{##1}%
         \biblabelprint{\csname\@citelabel{##1}\endcsname}%
         \@writeaux{\string\@citedef{##1}{\csname\@citelabel{##1}\endcsname}}%
         \ignorespaces
      }%
      %
      % Do the printing (we're producing the bibliography, remember).
      %
      \ifx\undefined\em \let\em=\bblem \fi
      \ifx\undefined\emph \let\emph=\bblemph \fi
      \ifx\undefined\mbox \let\mbox=\bblmbox \fi
      \ifx\undefined\newblock \let\newblock=\bblnewblock \fi
      \ifx\undefined\sc \let\sc=\bblsc \fi
      \ifx\undefined\textbf \let\textbf=\bbltextbf \fi
      %
      % Punctuation won't affect spacing;
      \frenchspacing
      % the penalties below are from LaTeX's [article,book,report].sty;
      \clubpenalty = 4000 \widowpenalty = 4000
      % the next two values come from LaTeX's \sloppy command;
      \tolerance = 10000 \hfuzz = .5pt
      \everypar = {\hangindent = \biblabelwidth
                      \advance\hangindent by \biblabelextraspace}%
      \bblrm
      % the \parskip is a guess at what looks good;
      \parskip = 1.5ex plus .5ex minus .5ex
      % and the space between label and text comes from LaTeX's \labelsep.
      \biblabelextraspace = .5em
      \bblhook
      %
      \input \bblfilebasename.bbl
   \endgroup
}%
%
% The widest label's width is useful for redefining \biblabelprint;
% you redefine \biblabelwidth, in effect, by redefining the
% \biblabelcontents macro that appears below.  And \biblabelextraspace,
% which is redefinable inside \bblhook, is added to \biblabelwidth to
% determine the amount of hanging indentation.
%
\@innernewdimen\biblabelwidth
\@innernewdimen\biblabelextraspace
%
% Now come the main macros that are related to the printing of the
% bibliography.  Since you might want to redefine them, they are given
% default definitions outside of \@readbblfile.
%
% The first one controls the printing of a bibliography entry's label.
% If you change it, make sure that it starts with something like
% \noindent or \indent or \leavevmode that puts TeX into horizontal mode
% (even if the label itself is empty); otherwise, the hanging
% indentation will get messed up in certain circumstances.
%
\def\biblabelprint#1{%
   \noindent
   \hbox to \biblabelwidth{%
      \biblabelprecontents
      \biblabelcontents{#1}%
      \biblabelpostcontents
   }%
   \kern\biblabelextraspace
}%
%
% If you are using numeric labels, and you want them left-justified
% (numeric labels by default are right-justified), do something like:
%     \def\biblabelprecontents{\relax}
%     \def\biblabelpostcontents{\hss}
%
% By default the labels are typeset in \bblrm, and enclosed in brackets.
\def\biblabelcontents#1{{\bblrm [#1]}}%
%
% The main text, too, is typeset using \bblrm, which is \rm by default.
\def\bblrm{\rm}%
%
% Emphasis for producing, e.g., titles, is done with \it by default.
\def\bblem{\it}%
%
% Another way to emphasize, with built-in italic correction.  (Doesn't
% look for a following comma or period, though.)
\def\bblemph#1{{\bblem #1\/}}
%
% And bold.
\def\bbltextbf#1{{\bf #1}}
%
% \mbox is \hbox forcing horizontal mode.
\def\bblmbox{\leavevmode\hbox}
%
% Some styles use a caps-and-small-caps font for author names.  LaTeX
% defines an \sc command but plain TeX doesn't, so we need one here.
% The definition below doesn't load the font unless it's needed, but it
% tries to load only the 10pt version, because it might not exist at
% other point sizes.
%
\def\bblsc{\ifx\@scfont\@undefined
              \font\@scfont = cmcsc10
           \fi
           \@scfont
}%
%
% The major parts of an entry are separated with \bblnewblock.  The
% numbers below are taken from LaTeX's `article' style.
%
\def\bblnewblock{\hskip .11em plus .33em minus .07em }%
%
% Here's where you stick any other bibliography-formatting goodies, or
% redefine the values above.
%
\let\bblhook = \empty
%
% And here you can add code to be executed before each item (before
% \biblabelprint is called).  The parameter is citation label for this
% bibliography entry.
%
\let\bblitemhook = \@gobble
%
%
% Here are the four default definitions for formatting the in-text
% citations.  These are what you redefine (after your \input btxmac but
% before your \bibliography) to get parens instead of brackets, or
% superscripts, or footnotes, or whatever.
%
\def\printcitestart{[}%         left bracket
\def\printcitefinish{]}%        right bracket
\def\printbetweencitations{, }% comma, space
\let\printcitepreitem\@gobble % takes label
\let\printcitepostitem\empty
\def\printcitenote#1{, #1}%     comma, space, note (if it exists)
%
% That scheme is pretty flexible.  For example you could use
%     \def\printcitestart{\unskip $^\bgroup}
%     \def\printcitefinish{\egroup$}
%     \def\printbetweencitations{,}
%     \def\printcitenote#1{\hbox{\sevenrm\space (#1)}}
%     \font\eighttt = cmtt8
%     \scriptfont\ttfam = \eighttt
% to get superscripted in-text citations.  (The scriptfont stuff
% exists only to print an undefined citation; it's in cmtt8 because
% there is no cmtt7.)  To get something radically different, however,
% you'll have to define your own \cite command.
%
% When we read `\citation' from the .aux file, it means nothing.
%
\let\citation = \@gobble
%
%
% Now comes the stuff for dealing with LaTeX's \newcommand.  As
% mentioned earlier, this \newcommand will redefine a preexisting
% command; that's different from how LaTeX's \newcommand behaves.
%
\@innernewcount\@btxnumparams
%
% \newcommand{\foo}[n]{text} defines the control sequence \foo to have n
% parameters, and replacement text `text'.  In LaTeX, this command gives
% a warning if \foo is already defined, and allows a second
% optional argument (which is a default value for the first optional
% arg) but we don't bother with either of these.
%
\ifx\newcommand\undefined
\long\def\newcommand#1{%
   \def\@btxcommandname{#1}%
   \@getoptionalarg\@btxcontinuenewcommand
}%
\fi
%
%
% \renewcommand{\foo}[n]{text}, for our purposes, is the same as
% \newcommand.  In LaTeX, it gives a warning if \foo is not already defined.
%
\ifx\renewcommand\undefined
  \let\renewcommand = \newcommand
\fi
% 
% 
% \providecommand{\foo}[n]{text} defines \foo as in \newcommand only if
% \foo is not already defined.
% 
\ifx\providecommand\undefined
\long\def\providecommand#1{%
   \def\@btxcommandname{#1}%
   %
   \expandafter\ifx\@btxcommandname \@undefined
     \let\cs=\@btxcontinuenewcommand  % undefined, so we'll define it
   \else
     \let\cs=\@gobble                 % already defined, so ignore it
   \fi
   \@getoptionalarg\cs
}%
\fi
% 
% 
% Figure out if this definition has parameters.
%
\def\@btxcontinuenewcommand{%
   % If no optional argument, we have zero parameters.  Otherwise, we
   % have that many.
   \@btxnumparams = \ifx\@optionalarg\empty 0\else\@optionalarg \fi \relax
   \@btxnewcommand
}%
%
% \@btxnumparams is how many arguments this command has.  The name of the
% command is \@btxcommandname.  The replacement text for the new macro is #1.
%
\def\@btxnewcommand#1{%
   \def\@btxstartdef{\expandafter\def\@btxcommandname}%
   \ifnum\@btxnumparams=0
      \let\@btxparamdef = \empty
   \else
      \ifnum\@btxnumparams>9
         \errmessage{\the\@btxnumparams\space is too many parameters}%
      \else
         \ifnum\@btxnumparams<0
            \errmessage{\the\@btxnumparams\space is too few parameters}%
         \else
            \edef\@btxparamdef{%
               % This is sad, but \loop doesn't work inside \edef,
               % because \body isn't defined.
               \ifcase\@btxnumparams
                  \empty  No arguments.
               \or ####1%
               \or ####1####2%
               \or ####1####2####3%
               \or ####1####2####3####4%
               \or ####1####2####3####4####5%
               \or ####1####2####3####4####5####6%
               \or ####1####2####3####4####5####6####7%
               \or ####1####2####3####4####5####6####7####8%
               \or ####1####2####3####4####5####6####7####8####9%
               \fi
            }%
         \fi
      \fi
   \fi
   \expandafter\@btxstartdef\@btxparamdef{#1}%
}%
%
%% [[[end of BibTeX-specific stuff]]]
%
%
% Names of references (arguments given in the \cite and \nocite
% commands) and file names (arguments given in the \bibliography and
% \bibliographystyle commands) are recorded in \jobname.aux, called the
% \@auxfile in these macros.  Here's how they get read in.
%
\def\@readauxfile{%
   \if@auxfiledone \else % remember: \@auxfiledonetrue if \noauxfile is defined
      \global\@auxfiledonetrue
      \@testfileexistence{aux}%
      \if@fileexists
         \begingroup
            % Because we might be in horizontal mode when \@readauxfile
            % is called (if it's in response to a \cite or \nocite), we
            % want to ignore all the would-be spaces at the ends of
            % lines in the aux file.  Fortunately, it's highly unlikely
            % an end-of-line might actually be desired.
            % And because we don't change the category code of anything
            % but @, primitives like \gdef can't be used to define labels
            % in the aux file.  The solution adopted by btxmac.tex is to
            % write `\@citedef{LABEL}{DEFINITION}' to the aux file, and
            % use \csname on LABEL.
            \endlinechar = -1
            \catcode`@ = 11
            \input \jobname.aux
         \endgroup
      \else
         \message{\@undefinedmessage}%
         \global\@citewarningfalse
      \fi
      \immediate\openout\@auxfile = \jobname.aux
   \fi
}%
%
% The \@readauxfile macro does all that work the first time it's called.
% Since it's called once for every \cite, \nocite, \bibliography, and
% \bibliographystyle command that the user issues, we need to remember
% whether the work's been done.  It's considered done if we're not to do
% it---that is, if \noauxfile is defined.
%
\newif\if@auxfiledone
\ifx\noauxfile\@undefined \else \@auxfiledonetrue\fi
%
% It's conceivable you'd want to change how other characters are read;
% to do that, change their category code before doing \input btxmac.
%
%
% After reading the .aux file, \@readauxfile opens it for writing.
% The \@writeaux macro does the actual writing (as long as
% \noauxfile is undefined).
%
\@innernewwrite\@auxfile
\def\@writeaux#1{\ifx\noauxfile\@undefined \write\@auxfile{#1}\fi}%
%
%
% A macro package that uses btxmac.tex might define
% \@undefinedmessage (before doing an \input btxmac).
%
\ifx\@undefinedmessage\@undefined
   \def\@undefinedmessage{No .aux file; I won't give you warnings about
                          undefined citations.}%
\fi
%
% Even if citations are undefined, we want to complain only if
% \@citewarningtrue.  The default is to set \@citewarningtrue unless
% \noauxfile is defined.  Again, a macro package that uses
% btxmac.tex might want to redefine this.
%
\@innernewif\if@citewarning
\ifx\noauxfile\@undefined \@citewarningtrue\fi
%
%
% Finally, before leaving we restore @'s old category code.
%
\catcode`@ = \@oldatcatcode
