%% @texfile{
%%   author = "Karl Berry and Oren Patashnik",
%%   version = "0.99b",
%%   date = "14 Aug 1990",
%%   filename = "btxmac.tex",
%%   address = "Please use e-mail",
%%   checksum = "624    2566   19244",
%%   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.99b, for BibTeX 0.99c, TeX 3.0 or later.
% Copyright (C) 1990 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 (1) you make
% absolutely no changes to your copy, or (2) if you do make changes, (a)
% you name the file something other than `btxmac.tex' and you remove all
% occurrences of `btxmac.tex' from the file, (b) 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 (c) 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 the four macros \bblrm, \bblem,
% \bblnewblock, and \bblhook as described later (and, depending on your
% citation style, perhaps redefine the \@@cite macro, as suggested
% later---the current definition is meant to accompany a bibliography
% style like BibTeX's standard style `plain').  Finally, as long as you
% follow the guidelines given by the LaTeX manual and "BibTeXing", you
% simply \input btxmac.  [Be sure that any redefinitions mentioned in
% the previous sentence appear after the \input btxmac.  Also, remember
% the general scheme: running (La)TeX writes information on the .aux
% file; then running BibTeX reads information from the .aux, .bst, and
% .bib files and writes information on a .bbl file; then running (La)TeX
% incorporates the bibliography; then running (La)TeX one more time
% 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.]
%
%
%%%%   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
%
%
% The LaTeX-related commands defined in this file include the four
% commands that a user types (\bibliography, \bibliographystyle, \cite,
% and \nocite); the three commands that BibTeX looks for in the .aux
% file (\bibdata, \bibstyle, and \citation---there is a fourth command
% that BibTeX looks for, when used with LaTeX, but that command is
% related to LaTeX's \include facility, so these macros ignore that
% command); and the only LaTeX command (\newcommand) that's written by
% any of the four standard bibliography styles.  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 difference is with \newcommand,
% which here lets you redefine a preexisting control sequence without
% complaining (in LaTeX, \newcommand won't let you redefine a
% preexisting command; also, in LaTeX the new command isn't global, the
% way it is here).  So you shouldn't have defined any control sequences
% with these eight names:
%
% \bibdata
% \bibliography
% \bibliographystyle
% \bibstyle
% \citation
% \cite
% \newcommand
% \nocite
%
% In addition, there are four other control sequences that the macros
% of this file will use if you define them (these are explained later):
%
% \bblem
% \bblhook
% \bblnewblock
% \bblrm
%
% And here are other control sequences that, should you already have
% them defined, might conflict with the btxmac.tex macros (but since each
% of these has an `@' in its name, it's unlikely you will already have
% any of them defined).
%
% \@auxfile
% \@auxfileopenedtrue
% \@auxfileopenedfalse
% \@citation
% \@cite
% \@@cite
% \@citedef
% \@citelabel
% \@citenote
% \@citewarningtrue
% \@citewarningfalse
% \@citewithnote
% \@commandname
% \@continuenewcommand
% \@emptymarkA
% \@emptymarkB
% \@fileexiststrue
% \@fileexistsfalse
% \@finishcitedef
% \@for
% \@forloop
% \@fornoop
% \@fortmp
% \@gobble
% \if@auxfileopened
% \if@citewarning
% \@ifempty
% \@@ifempty
% \if@fileexists
% \if@notfirstcitation
% \@iforloop
% \@itemnum
% \@letter
% \@newcommandwithargs
% \@newcommand
% \@next
% \@nextwhile
% \@nil
% \@nnil
% \@notfirstcitationtrue
% \@notfirstcitationfalse
% \@numparams
% \@oldatcatcode
% \@onecitation
% \@openauxfile
% \@other
% \@paramdef
% \@readauxfile
% \@readbblfile
% \@resetnumerals
% \@setletters
% \@spacesub
% \@startdef
% \@testfileexistence
% \@tokstostring
% \@ttsA
% \@ttsB
% \@ttsmarkA
% \@ttsmarkB
% \@undefinedmessage
% \@widelabel
%
% So to start things off, we turn `@' into a letter (catcode 11),
% keeping track of the old catcode 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.
%
% Swallow one parameter.
%
\def\@gobble#1{}%
%
%
% \@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
%
% 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 routine tests if a file \jobname.#1 exists, and sets
% \if@fileexists appropriately.
%
\newif\if@fileexists
%
\def\@testfileexistence#1{%
   \begingroup
      \immediate\openin0 = \jobname.#1 % That space is swallowed.
      \ifeof 0
         \global\@fileexistsfalse
      \else
         \global\@fileexiststrue
      \fi
      \immediate\closein0
   \endgroup
}%
%
%
% 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{\write\@auxfile{\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{%
   \write\@auxfile{\string\bibdata{#1}}%
   \@testfileexistence{bbl}%
   \if@fileexists
      \@readbblfile
   \fi
}%
\let\bibdata = \@gobble
%
%
% The \nocite{label,label,...} command writes its argument to \@auxfile,
% but produces no text in the document.  Both \nocite and \cite
% commands produce \citation commands in the .aux file.
%
\def\nocite#1{\write\@auxfile{\string\citation{#1}}}%
%
%
\newif\if@notfirstcitation
%
% \cite[note]{label,label,...} produces the citations for the labels as
% well.  If `[note]' is present, it's added after the labels.
%
\def\cite{\futurelet\next\@cite}%
\def\@cite{%
   \begingroup \if [\next
      \aftergroup\@citewithnote
   \else
      \global\let\@citenote = \empty
      \aftergroup\@@cite
   \fi \endgroup
}%
%
% We get here when we have the optional note.
%
\def\@citewithnote[#1]{%
   \def\@citenote{#1}%
   \@@cite
}%
%
% Typeset the citations for the labels in #1, followed by \@citenote, if
% non\empty.  [If you need to change the way a citation appears in the
% text (for example if you need parens or superscripts instead of
% brackets), it's the definition of \@@cite that you need to change.]
%
\def\@@cite#1{%
   \nocite{#1}%
   % Print the left bracket to start the citation text.
   %
   [%
   \@notfirstcitationfalse
   \@for \@citation :=#1\do
   {%
      \expandafter\@onecitation\@citation\@@
   }%
   \ifx\empty\@citenote\else
     , \@citenote
   \fi
   ]%
}%
%
\def\@onecitation#1\@@{%
   \if@notfirstcitation
      ,\space
   \fi
   %
   \expandafter \ifx \csname\@citelabel{#1}\endcsname \relax
      \if@citewarning
    \message{Undefined citation `#1'.}%
      \fi
      % Give it a dummy definition:
      \expandafter\gdef\csname\@citelabel{#1}\endcsname{%}}
        \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.
%
\def\@citedef#1{\@resetnumerals\@finishcitedef{#1}}%
\def\@finishcitedef#1#2{\expandafter\gdef\csname\@citelabel{#1}\endcsname{#2}}%
\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
}%
%
%
% 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 these things in
% advance.  If you wish, you can define \bblhook to produce the title.
% Or just do it before the \bibliography command.
%
\def\@readbblfile{%
   \begingroup
      \def\begin##1##2{%
         % ##1 is just `thebibliography'.
         % ##2 is the widest label.
         \setbox\@widelabel = \hbox{\bblrm [##2]}%
      }%
      \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 will use (new count) \@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{%
         \@bibitemprint{\csname\@citelabel{##1}\endcsname}%
         \write\@auxfile
            {\string\@citedef{##1}{\csname\@citelabel{##1}\endcsname}}%
         \ignorespaces
      }%
      %
      % Do the printing (we're producing the bibliography, remember).
      % \box0 has the widest label.
      %
      \def\@bibitemprint##1{%
         \noindent\hbox to \wd\@widelabel{[\bblrm ##1]\hfil}\enspace
      }%
      \let\em = \bblem
      \let\newblock = \bblnewblock
      \frenchspacing % Punctuation doesn't affect spacing.
      % The penalties below are from LaTeX's [article,book,report].sty
      \clubpenalty4000\widowpenalty4000
      \tolerance = 10000 \hfuzz = .5pt
      % The \parskip is a guess at what looks good.
      \parskip = 1.5ex plus .5ex minus .5ex
      % The .5em below matches the \enspace ten-or-so lines above.
      \everypar = {\hangindent = \wd\@widelabel \advance\hangindent by .5em }%
      \bblhook
      %
      \input \jobname.bbl
   \endgroup
}%
%
% These are defined outside of \@readbblfile because new boxes and counts
% must be outer.
%
\newcount\@itemnum
\newbox\@widelabel
%
% These are defined outside of \@readbblfile only so that
% you can redefine them if you wish.
%
% Used for typesetting the labels, as well as the main text.
%
\def\bblrm{\rm}%
%
% Emphasis, e.g., titles:
%
\def\bblem{\it}%
%
% Parts of an entry.  These numbers are taken from LaTeX's article.doc.
%
\def\bblnewblock{\hskip .11em plus .33em minus .07em}%
%
\let\bblhook = \empty
%
% When we read this from the .aux file, it means nothing.
%
\let\citation = \@gobble
%
%
% Definitions of references are recorded in \jobname.aux, called
% the \@auxfile in these macros, which we open (for writing)
% right after we read it.
%
\newwrite\@auxfile
\newif\if@auxfileopened
%
\def\@openauxfile{%
   \if@auxfileopened\else
      \@auxfileopenedtrue
      \immediate\openout\@auxfile = \jobname.aux
   \fi
}%
%
% Those definitions are read in when we call \@readauxfile,
% which happens at the end of this (btxmac.tex) file.
%
% \@readauxfile treats many characters as letters, since it
% makes a control sequence name out of the argument, and yet
% we want to allow general names for labels.
%
\def\@readauxfile{%
   \@testfileexistence{aux}%
   \if@fileexists
      \begingroup
         \@setletters
         \input \jobname.aux
      \endgroup
   \else
      \message{\@undefinedmessage}%
      \@citewarningfalse
   \fi
   \@openauxfile
}%
%
% A macro package that uses btxmac.tex might redefine \@undefinedmessage.
%
\def\@undefinedmessage{No .aux file; I won't give you warnings about
               undefined citations.}%
\def\@setletters{%
   \catcode`_ = \@letter
   \catcode`+ = \@letter \catcode`- = \@letter
   \catcode`@ = \@letter
   \catcode`0 = \@letter \catcode`1 = \@letter \catcode`2 = \@letter
   \catcode`3 = \@letter \catcode`4 = \@letter \catcode`5 = \@letter
   \catcode`6 = \@letter \catcode`7 = \@letter \catcode`8 = \@letter
   \catcode`9 = \@letter
   \catcode`( = \@letter \catcode`) = \@letter
}%
%
% Even if citations are undefined, we only want to complain if
% \@citewarningtrue.
%
\newif\if@citewarning  \@citewarningtrue
%
%
% Now comes the stuff for dealing with LaTeX's \newcommand.  As
% mentioned earlier, this \newcommand will redefine a preexisting
% command, and its definition is global; both of these are
% different from how LaTeX's \newcommand behaves.
%
\newcount\@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}%
}%
%
%
% Finally, before leaving we read in the .aux file
%
\@readauxfile
%
% and restore @'s old catcode.
%
\catcode`@ = \@oldatcatcode
