%% @texfile{
%%   author = "Karl Berry and Oren Patashnik",
%%   version = "0.99h",
%%   date = "24 Apr 1991",
%%   filename = "btxmac.tex",
%%   address = "Please use electronic mail",
%%   checksum = "812   4053  30557",
%%   email = "opbibtex@neon.stanford.edu",
%%   codetable = "ISO/ASCII",
%%   supported = "yes",
%%   docstring = "Defines macros that make BibTeX work with plain TeX",
%% }
% BibTeX-for-TeX macros, version 0.99h, for BibTeX 0.99c, TeX 3.0 or later.
% Copyright (C) 1990--91 by Karl Berry and Oren Patashnik; all rights reserved.
% You may copy this file provided: that it's accompanied by the
% "BibTeXing" document, whose text is contained in the file `btxdoc.tex';
% that any documentation you write for these macros also gives a
% reference for "BibTeXing"; and that either you make absolutely no
% changes to your copy, or if you do make changes, (1) you name the file
% something other than `btxmac.tex' and you remove all occurrences of
% `btxmac.tex' from the file, (2) you put, somewhere in the first twenty
% lines of the file, your name, along with an electronic address at which
% others who might use the file may reach you, and (3) you remove each
% occurrence of Oren's name and electronic address from this file.  These
% restrictions help ensure that all standard versions of these macros are
% identical, and that Oren doesn't get deluged with inappropriate e-mail.
%
% This file, btxmac.tex, 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@neon.stanford.edu).  These macros will become
% frozen shortly after BibTeX version 1.00 is released.
%
% 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.  Finally, as long as you follow the guidelines given
% by the LaTeX manual and "BibTeXing", you simply \input btxmac.
% [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 macro packages
% like Eplain.  If you want to try out these macros on 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.
%
%
%   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.
%
%
% The LaTeX-related commands defined in this file include (a) the four
% commands that a user types (\bibliography, \bibliographystyle, \cite,
% and \nocite); (b) the 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) a LaTeX
% command (\newcommand) that's written by one of the four standard
% bibliography styles (alpha).  The definitions here are not exactly the
% same as the corresponding LaTeX definitions (those eight LaTeX
% definitions depend on a significant fraction of LaTeX itself).  But
% the only substantial differences are with \newcommand, which here,
% without complaining, lets you redefine a preexisting control sequence
% (in LaTeX, \newcommand won't let you redefine a preexisting command),
% and which here doesn't make the control sequences it defines \long (in
% LaTeX, that happens automatically); there may also be other minor
% differences.  To summarize: Unless you know what you're doing, you
% shouldn't define any control sequences with these eight names:
%
% \bibdata
% \bibliography
% \bibliographystyle
% \bibstyle
% \citation
% \cite
% \newcommand
% \nocite
%
% There are three other commands written by one or more of the four
% standard (plain, abbrv, alpha, unsrt) or four semistandard (acm,
% apalike, ieeetr, siam) bibliography styles; they take effect only
% within the bibliography, and are redefinable, as explained later:
%
% \em
% \newblock
% \sc
%
% There's one control sequence you might want to use (but not redefine)
% in redefining \biblabelprint:
%
% \biblabelwidth
%
% There are twelve 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 five begin with `\bbl' and affect fonts, spacing,
% and perhaps other characteristics of the bibliography; the next three
% 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
% \bblhook
% \bblnewblock
% \bblrm
% \bblsc
% \biblabelcontents
% \biblabelextrahang
% \biblabelprint
% \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), with an `@'
% in its name, 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:
%
% \@undefinedmessage
%
% And here are still more control sequences that, should you already have
% them defined, might conflict with the way the btxmac.tex macros use them
% (but since each of these has an `@' in its name, it's unlikely that such
% conflicts will arise unless you intend them):
%
% \@auxfile               \@auxfiledonefalse      \@auxfiledonetrue
% \@bracketcheck          \@citation              \@cite
% \@citedef               \@citelabel             \@citewarningfalse
% \@citewarningtrue       \@commandname           \@continuenewcommand
% \@emptymarkA            \@emptymarkB            \@fileexistsfalse
% \@fileexiststrue        \@finishcitedef         \@finishtestfileexistence
% \@for                   \@forloop               \@fornoop
% \@fortmp                \@getoptionalarg        \@@getoptionalarg
% \@gobble                \@if@auxfiledone        \if@citewarning
% \@ifempty               \@@ifempty              \if@fileexists
% \if@notfirstcitation    \@iforloop              \@innerdef
% \@innernewcount         \@innernewdimen         \@innernewif
% \@innernewwrite         \@itemnum               \@letter
% \@linenumber            \@newcommandwithargs    \@newcommand
% \@next                  \@nextwhile             \@nil
% \@nnil                  \@notfirstcitationfalse \@notfirstcitationtrue
% \@numparams             \@oldatcatcode          \@onecitation
% \@optionalarg           \@other                 \@paramdef
% \@readauxfile           \@readbblfile           \@resetnumerals
% \@scfont                \@setletters            \@spacesub
% \@startdef              \@testfileexistence     \@tokstostring
% \@ttsA                  \@ttsB                  \@ttsmarkA
% \@ttsmarkB              \@undefined             \@writeaux
%
%
% 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.)  The use of `\cite' as a temporary control sequence is
% a kludge, but it's a reasonably simple way to accomplish what we need
% without 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
%
%
% \@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\temp = #1\futurelet\next\@bracketcheck}%
\def\@bracketcheck{\begingroup
   \if [\next
      \aftergroup\@@getoptionalarg
   \else
      \global\let\@optionalarg = \empty
      %
      % We can't do the \aftergroup\temp after the \fi, because then the
      % \temp gets in the way of reading the optional argument from the
      % input, if we do have one.
      \aftergroup\temp
   \fi
\endgroup}%
%
\def\@@getoptionalarg[#1]{%
   \def\@optionalarg{#1}%
   \temp
}%
%
%
% \@tokstostring converts its argument into a list of character tokens.
% It uses no commands that are executed in the stomach, only expansions
% handled in the gullet.  (This is necessary for use with \edef).
%
% Control sequences in the argument are not expanded.  Blanks become a
% \spacesub.  The argument shouldn't contain other special characters.
%
\def\@tokstostring#1{\@ttsA#1 \@ttsmarkA}%
%
% \@ttsA partitions a sequence of tokens into portions delimited by blanks.
%
\def\@ttsA#1 #2\@ttsmarkA{%
   \@ifempty{#1}\else
      \@ttsB #1\@ttsmarkB
      %
      \@ifempty{#2}\else
         \@spacesub\@ttsA#2\@ttsmarkA
      \fi
   \fi
}%
%
% \@ttsB processes a sequence of tokens with no contained blanks.
%
\def\@ttsB#1{%
   \ifx #1\@ttsmarkB\else
      \string #1%
      \expandafter\@ttsB
   \fi
}%
%
% This is just so \ifx won't complain about an undefined control sequence.
%
\def\@ttsmarkB{\@ttsmarkB}% should never be expanded
%
% The expansion of this macro replaces spaces.
%
\def\@spacesub{+}%
%
%
% \@ifempty tests if it is given an argument.
%
\def\@ifempty#1{\@@ifempty #1\@emptymarkA\@emptymarkB}%
\def\@@ifempty#1#2\@emptymarkB{\ifx #1\@emptymarkA}%
%
\def\@emptymarkA{\@emptymarkA}% Again, so \ifx won't complain.
%
%
% 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.
%
\def\bibliography#1{%
   \@readauxfile
   \@writeaux{\string\bibdata{#1}}%
   \@testfileexistence{bbl}%
   \if@fileexists
      \@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 before doing any writing, it
% doesn't need to call \@readauxfile.  We change the category code of
% `_' so that the dummy definition given to the citation label doesn't
% include a subcript character; that would cause TeX to complain.
%
\def\cite{\begingroup\catcode`_ = \@letter \@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{%
   \nocite{#1}%
   % Start printing the text, beginning with a left bracket by default.
   %
   \printcitestart
   \@notfirstcitationfalse
   \@for \@citation :=#1\do
   {%
      \expandafter\@onecitation\@citation\@@
   }%
   \ifx\empty\@optionalarg\else
      \printcitenote{\@optionalarg}%
   \fi
   \printcitefinish
   \endgroup
}%
%
\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 \nobreak\hskip0pt#1\nobreak\hskip0pt}}%
   \fi
   % Now produce the text, whether it was undefined or not.
   \csname\@citelabel{#1}\endcsname
   \@notfirstcitationtrue
}%
%
% Given a label `foo', the macro `\b@foo' is supposed to
% hold the text that should be produced.
%
\def\@citelabel#1{\@tokstostring{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.  But we have to be careful when reading the \@citedef, because
% the second argument might have accents or other commands with numbers
% as arguments. We have given the numerals category code 11 for reading
% the .aux file, and that prevents them from being recognized as TeX
% <number>s.  Similarly for the other characters that can be part of a
% <number>.  So we have to reset the category codes before reading the
% second argument.  We also have to reset them back to letters when we
% finish reading the \@citedef's, because there might be more labels
% with numerals in them defined afterwards in the .aux file, put there
% by Eplain or whatever.
%
\def\@citedef#1{%
   \begingroup
      \@resetnumerals
      \@finishcitedef{#1}%
}%
\def\@finishcitedef#1#2{%
      \expandafter\gdef\csname\@citelabel{#1}\endcsname{#2}%
   \endgroup
}%
\def\@resetnumerals{%
   \catcode`0 = \@other \catcode`1 = \@other \catcode`2 = \@other
   \catcode`3 = \@other \catcode`4 = \@other \catcode`5 = \@other
   \catcode`6 = \@other \catcode`7 = \@other \catcode`8 = \@other
   \catcode`9 = \@other \catcode`" = \@other \catcode`' = \@other
   \catcode`` = \@other \catcode`, = \@other \catcode`. = \@other
}%
%
%
% 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.
   \@innernewcount\@itemnum
   %
   \begingroup
      \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
      }%
      \def\end##1{}% ##1 is `thebibliography' again.
      %
      % 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{\futurelet\next\@bibitem}%
      \def\@bibitem{%
         \begingroup \if [\next
            \aftergroup\@alphabibitem
         \else
            \aftergroup\@numberedbibitem
         \fi \endgroup
      }%
      \def\@alphabibitem[##1]##2{%
         % Need \xdef here for various reasons.
         \expandafter \xdef\csname\@citelabel{##2}\endcsname {##1}%
         \@finishbibitem{##2}%
      }%
      %
      \def\@numberedbibitem##1{%
         \advance\@itemnum by 1
         \expandafter \xdef\csname\@citelabel{##1}\endcsname{\number\@itemnum}%
         \@finishbibitem{##1}%
      }%
      %
      \def\@finishbibitem##1{%
         \biblabelprint{\csname\@citelabel{##1}\endcsname}%
         \@writeaux{\string\@citedef{##1}{\csname\@citelabel{##1}\endcsname}}%
         \ignorespaces
      }%
      %
      % Do the printing (we're producing the bibliography, remember).
      %
      \let\em = \bblem
      \let\newblock = \bblnewblock
      \let\sc = \bblsc
      % 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 \biblabelextrahang}%
      \bblrm
      % the \parskip is a guess at what looks good;
      \parskip = 1.5ex plus .5ex minus .5ex
      % and the .5em matches the \enspace of \biblabelprint below.
      \biblabelextrahang = .5em
      \bblhook
      %
      \input \jobname.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 \biblabelextrahang,
% which is redefinable inside \bblhook, is added to \biblabelwidth to
% determine the amount of hanging indentation.
%
\@innernewdimen\biblabelwidth
\@innernewdimen\biblabelextrahang
%
% 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 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{\biblabelcontents{#1}\hss}\enspace}%
%
% 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}%
%
% 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
%
%
% 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
\def\printcitenote#1{, #1}%     comma, space, note (if it exists)
%
% That scheme is pretty flexible.  For example you could use
%     \def\printcitestart{$^\bgroup}
%     \def\printcitefinish{\egroup$}
%     \def\printbetweencitations{,}
%     \def\printcitenote#1{\hbox{\sevenrm\space (#1)}}
% to get superscripted in-text citations.  To get something radically
% different, however, you'll have to define your own \cite command.
%
% When we read this from the .aux file, \citation 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\@numparams
%
% \newcommand{\foo}[n]{text} defines the control sequence \foo to have
% n parameters, and replacement text `text'.
%
\def\newcommand#1{%
   \def\@commandname{#1}%
   \futurelet\@next\@continuenewcommand
}%
%
% Figure out if this definition has parameters.
%
\def\@continuenewcommand{\begingroup
   \if [\@next
      \aftergroup\@newcommandwithargs
   \else
      \global\@numparams = 0
      \aftergroup\@newcommand
   \fi
\endgroup}%
%
\def\@newcommandwithargs[#1]{%
   \global\@numparams = #1
   \@newcommand
}%
%
% \@numparams is how many arguments this command has.  The name of the
% command is \@commandname.  The replacement text for the new macro is #1.
%
\def\@newcommand#1{%
   \def\@startdef{\expandafter\edef\@commandname}%
   \ifnum\@numparams=0
      \let\@paramdef = \empty
   \else
      \ifnum\@numparams>9
         \errmessage{\the\@numparams\space is too many parameters}%
      \else
         \ifnum\@numparams<0
            \errmessage{\the\@numparams\space is too few parameters}%
         \else
            \edef\@paramdef{%
               % This is disgusting, but \loop doesn't work inside \edef,
               % because \body isn't defined.
               %
               \ifcase\@numparams
                  \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\@startdef\@paramdef{#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, there are never any
            % (non-pathological) cases where an end-of-line might
            % actually be desired.  If you really need to do something
            % strange, you can redefine \@setletters to do something
            % besides what it does now (this is intentionally left as
            % being somewhat painful to do).
            \endlinechar = -1
            \@setletters
            \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
%
% Since lots of characters are allowable inside reference names, and
% since a reference name can get converted into a control sequence,
% \@readauxfile must treat many characters as letters.  It does that by
% calling \@setletters, which changes the category code of all \@other
% characters to \@letter.  The 128/256 business arises because character
% codes go up to 256 starting with TeX 3.0, but up to only 128 with
% earlier versions.
%
\def\@setletters{%
   \count255 = 0
   \edef\temp{\ifx\inputlineno\@undefined 128\else 256\fi}%
   \loop
      \ifnum\catcode\count255 = \@other
         \catcode\count255 = \@letter
      \fi
      \advance\count255 by 1
      \ifnum\count255<\temp
   \repeat
   % The Eplain package uses underscores to construct the label names.
   \catcode`\_ = \@letter
}%
%
% 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