% \iffalse meta-comment
%
% ===================================================================
% splitbib.dtx
%
% Nicolas MARKEY   <markey@lsv.ens-cachan.fr>
% 2005/12/22
% v1.17
%
% ===================================================================
%
% \charactertable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
%    This file is built for \LaTeXe, so we make sure an error is
%    generated when it is used with another format
%    \begin{macrocode}
%<+package>\NeedsTeXFormat{LaTeX2e}
%    \end{macrocode}
%
%    Now announce the package name and version:
%    \begin{macrocode}
%<*dtx>
\ProvidesFile{splitbib.dtx}
%</dtx>
%<+package>\ProvidesPackage{splitbib}
     [2005/12/22 v1.17 Splits bibliography into categories]
%    \end{macrocode}
%
%
%    \begin{macrocode}
%<*driver>
\documentclass{ltxdoc}
\usepackage{boxedminipage}
\usepackage{splitbib}
\usepackage[dvips]{graphicx}
%    \end{macrocode}
%    We do want an index, using linenumbers
%    \begin{macrocode}
\CheckSum{1860}
\StopEventually{}
\EnableCrossrefs
\CodelineIndex
\DoNotIndex{\begin,\begingroup,\endgroup,\framebox,\fboxsep,\hbox,\long}
\DoNotIndex{\newif}
\DoNotIndex{\gdef,\@arabic,\@empty,\@firstofone,\@for,\@ifundefined,\@tempa,\@tempb}
\DoNotIndex{\@tempboxa,\addtocounter,\advance,\baselineskip,\bfseries,\bigskip}
\DoNotIndex{\chapter,\csname,\def,\do,\edef,\end,\endcsname,\endlist,\expandafter}
\DoNotIndex{\ifx,\else,\fi,\ifdim,\ifnum,\ifx,\fi,\fi,\fi}
\DoNotIndex{\gdef,\hfill,\hskip,\ignorespaces,\immediate,\Large,\let,\list,%
  \medskip,\message,\newcounter,\newdimen,\noindent,\null,\par,\relax,%
  \renewcommand,\setbox,\setcounter,\setlength,\settowidth,\smallskip,\stepcounter,%
  \string,\textwidth,\the,\unskip,\value,\vspace,\vskip,\vrule,\vbox,\wd,\xdef}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\package#1{\texttt{#1.sty}}
\def\cmpt#1{\texttt{#1}}
\def\ext#1{\texttt{.#1}}
\def\env#1{\texttt{#1}}
\def\news#1{\textsf{#1}}
\def\btcmd#1{\texttt{#1\$}}
\begin{document}
\DocInput{splitbib.dtx}
\end{document}
%</driver>
%    \end{macrocode}
% \fi
% \GetFileInfo{splitbib.dtx}
%
% \changes{0.99}{2004/06/18}{First release candidate}
% \changes{0.99b}{2004/06/29}{Added \cmd\SBalias}
% \changes{0.99c}{2004/06/30}{Added strict option + 
%			multiple category management}
% \changes{0.99d}{2004/07/04}{Added reorder option}
% \changes{1.00}{2004/07/21}{Added the "export" option +
%                       comment management +
%			published on CTAN}
% \changes{1.10}{2004/08/14}{Protect arguments against 
%			expansion when exporting}
% \changes{1.11}{2005/02/22}{\string\NMSB@currprefix was also 
%                       not expanded in v1.10. This led to 
%                       wrong references when extra prefixes
%                       were used.}
% \changes{1.12}{2005/02/25}{}
% \changes{1.13}{2005/08/16}{}
% \changes{1.14}{2005/11/26}{[export] doesn't export the number
%			if it will be computed correctly.}
% \changes{1.15}{2005/11/27}{fix a bug in option reorder}
% \changes{1.16}{2005/12/12}{fix a stupid bug in previous version}
% \changes{1.17}{2005/12/22}{fix another bug related to reorder, 
%			fix a bug with label prefixes}
%
%
% \title{Split your bibliography into categories
%         \thanks{This file has version number \fileversion,
%                 last revised \filedate.}}
%
% \author{ Nicolas Markey \\
%    \texttt{markey@lsv.ens-cachan.fr}}
%
% \date{\today}
%
% \maketitle
%
% \begin{abstract}
% This package allows for sorting a bibliography into categories and 
% subcategories. This is interesting for lists of publications, for
% grouping references by subject, by year,~... An option allows to export
% the resulting bibliography as a \ext{bbl}~like file.
% \end{abstract}
%
% \section{Introduction}
%
% Up to now, there exists several ways for sorting bibliographic references 
% into categories:
% \begin{itemize}
% \item Using a bibliographic style allowing it.
%   There exists a few such styles, but then you'll need to add a 
%   "category" field in each entry of your \ext{bib}~file. 
%   Moreover, grouping criteria might change from one document to the
%   other, and modifying categories requires that you modify your 
%   \ext{bib}~file, which is a long and tedious task;
% \item \package{multibib}, by Thorsten Hansen, allows to create several
%   bibliographies, by defining several \cmd\cite{} commands. This however
%   has several limitations: Duplicate labels, risk of citing one reference
%   in two different categories,~...
% \item Typesetting the bibliography by hand, which should eventually give
%   you exactly what you want, if you're patient enough...
% \end{itemize}
%
% \package{splitbib} offers a new, original way of doing this with \LaTeX. 
% It uses a \env{category} environment for defining categories. Up to two 
% such environments can be nested. Those environments take one mandatory 
% argument defining the "title" of the category. Within a category, command
% \cmd\SBentries{} defines the entries that should appear in that category.
% With the \texttt{reorder} option, this will also define the order in 
% which references should appear (the default is to keep the order of the
% original \ext{bbl}~file within each (sub)category).
%
% Then, when a reference appears in a \env{thebibliography} environment,
% it is moved in its category. A warning may be echoed in case a reference
% appears in two categories. On the contrary, if a reference has not been
% assigned a category, it is put in a ``miscellaneous'' category.
%
% \section{How does it work?}
%
% \subsection{Once upon a time...}
%
% The starting point of this package is a selection sorting algorithm 
% initially proposed 
% by Josselin Noirel on \news{fr.comp.text.tex}, and that I made more 
% performant 
% by using a quicksort algorithm. A discussion followed about the 
% usefulness of such a command... 
%
% \subsection{The link between sorting and categories}
%
% As mentionned previously, some bibliographic styles handle categories. This 
% is achieved by adding a prefix to the \btcmd{sort.key} string used by
% Bib\TeX{} for sorting bibliographic items. We use the same idea here, using
% a numeric value for each category. Another numeric value is appended to the
% category entry, in order to keep the initial order within each category. 
%
% \subsection{In practice}
%
% Defining categories is achieved with a \env{category} environment. 
% The argument of this environment defines the title of that new category.
% An optional argument allows to define a prefix for the labels of each entry 
% in that category.
%
% Within a category, the \cmd\SBentries~command defines which entries should 
% appear in that category. It is a comma-separated list of internal keys.
%
% Everything else should be transparent for the user. Several important 
% remarks though:
% \begin{itemize}
% \item \package{splitbib} redefines \cmd\bibitem{} and the
% \env{thebibliography} environment. More precisely,
% \cmd\begin\marg{thebibliography} does almost nothing, \cmd\bibitem~just
% stores the references (but echoes nothing), and
% \cmd\end\marg{thebibliography} sorts and outputs the bibliography.
% \item Since this package deeply redefines \cmd\bibitem{} and the
% \env{thebibliography} environment, it must be loaded \emph{after} 
% packages that redefine those commands. In particular \package{apalike}, 
% \package{natbib} or \package{jurabib};
% \item \package{natbib} and \package{jurabib} use very special formats 
% for the optional argument 
% of \cmd\bibitem. In order to fully use their features, you should load
% \package{splitbib} with the \texttt{export} argument, so that their original
% definitions of \cmd\bibitem{} will be used when processing the bibliography.
% Globally speaking, the \texttt{export} option should make \package{splitbib}
% compatible with many type of bibliography;
% \item  Since
% it is possible to add a prefix to reference labels, the argument of the 
% \env{thebibliography} environment is irrelevant. Therefore, the longest
% label will be re-computed, or can be forced; 
% \item Last, several styles for
% category titles are predefined, but you can define your own style 
% by redefining \cmd\SBtitle~and \cmd\SBsubtitle.
% \end{itemize}
%
% \subsection{An example}
%
% An example is shown on figure~\ref{example}. It contains the basic commands
% for a simple use of \package{splitbib}. 
%
% \begin{figure}[!htp]
% \noindent\begin{boxedminipage}{\textwidth}
% \def\MacroFont{\fontsize{7pt}{8pt}\selectfont\ttfamily}
% \begin{minipage}[t]{.45\linewidth}
% \begin{verbatim}
% \documentclass{article}
% \usepackage{splitbib}
%
% \begin{category}[A]{First category}
%    \SBentries{entry1,entry4}
% \end{category}
% \begin{category}[B]{Second category}
%  \begin{category}{First sub-category}
%    \SBentries{entry2,entry6}
%  \end{category}
%  \begin{category}{Second sub-category}
%    \SBentries{entry5,entry3}
%  \end{category}
% \end{category}
%
% \begin{document}
% We cite~\cite{entry1,entry3,entry4,%
% entry5}. Note that we cite neither 
% \verb+entry2+ nor \verb+entry6+, 
% even though they have been assigned
% a category.
% \end{verbatim}
% \end{minipage}
% \hfil\vrule\hfil
% \begin{minipage}[t]{.45\linewidth}
% \begin{verbatim}
% defined in the last category. The first
% sub-category will then not appear in the 
% bibliography.
%
% % \def\SBlongestlabel{A1}
% \SBtitlestyle{bar}
% \SBsubtitlestyle{none}
% \begin{thebibliography}{1}
% \bibitem{entry1} This is the first entry.
%
% \bibitem{entry3} This is the third entry.
%
% \bibitem{entry4} This is the fourth one.
%
% \bibitem{entry5} This is the last one.
%
% \end{thebibliography}
% \end{document}
% \end{verbatim}
% \end{minipage}
% \end{boxedminipage}
% \vskip5mm
%
% \noindent\begin{boxedminipage}{\linewidth}
% \vskip5mm
% \centering\begin{minipage}{.7\linewidth}
% \begin{category}[A]{First category}
%    \SBentries{entry1,entry4}
% \end{category}
% \begin{category}[B]{Second category}
%  \begin{category}{First sub-category}
%    \SBentries{entry2,entry6}
%  \end{category}
%  \begin{category}{Second sub-category}
%    \SBentries{entry5,entry3}
%  \end{category}
% \end{category}
%
% \fontsize{7pt}{8pt}\selectfont
% We cite~\cite{entry1,entry3,entry4,entry5}. 
% Note that we cite neither \texttt{entry2} nor
% \texttt{entry6}, even though they have been 
% defined in the last category. The first
% sub-category will then not appear in the 
% bibliography. 
%
% % \def\SBlongestlabel{A1}
% \SBtitlestyle{bar}
% \SBsubtitlestyle{none}
% \begin{thebibliography}{1}
% \bibitem{entry1} This is the first entry.
%
% \bibitem{entry3} This is the third entry.
%
% \bibitem{entry4} This is the fourth one.
%
% \bibitem{entry5} This is the last one.
%
% \end{thebibliography}
% \end{minipage}
% \vskip5mm\par\vbox{}
% \end{boxedminipage}
% \caption{An example using \package{splitbib}}\label{example}
% \end{figure}
%
%
%
% \newpage
% \section{The code}
% \begin{macro}{\ifNMSB@strict}
% \begin{macro}{strict}
% \begin{macro}{nonstrict}
% \begin{macro}{\ifNMSB@ownorder}
% \begin{macro}{reorder}
% \begin{macro}{keeporder}
% \begin{macro}{\ifNMSB@export}
% \begin{macro}{export}
% \begin{macro}{noexport}
% \begin{macro}{\ifNMSB@newchap}
% \begin{macro}{newchap}
% \begin{macro}{newsec}
% \begin{macro}{nonewchap}
% \begin{macro}{nonewsec}
% \package{splitbib} understands four options: \begin{itemize}
% \item \texttt{nonstrict} is to disallow multiple categories for one entry. 
% Default is to
% allow it: In that case, \package{splitbib} won't complain, but \LaTeX{} 
% will find multiply defined labels. In the other case, if an entry is 
% declared in several categories, only the first one will be used, and
% \package{splitbib} will warn you about the problem.
% \item \texttt{reorder} tells \package{splitbib} to use the order the entries 
% appear in the \cmd\SBentries\ as the output order. The default is to 
% keep the order the references appear in the \ext{bib}~file within each
% (sub)categories. With \texttt{reorder}, entries that have no category 
% will be omitted.
% \item\texttt{export} will export the new \env{thebibliography} environment
% into an \ext{sbb}~file, similar to the \ext{bbl}~file, but with 
% categories. The default is not to create that file, but you should consider 
% adding this option if you encounter compilation problems.
% \item\texttt{nonewchap} and \texttt{nonewsec} prevent \env{thebibliography} 
% to start a new chapter or section. 
% 					       \end{itemize}
% \begin{macrocode}
\newif\ifNMSB@strict\NMSB@strictfalse
\DeclareOption{strict}{\NMSB@stricttrue}
\DeclareOption{nonstrict}{\NMSB@strictfalse}
\newif\ifNMSB@ownorder\NMSB@ownorderfalse
\DeclareOption{reorder}{\NMSB@ownordertrue}
\DeclareOption{keeporder}{\NMSB@ownorderfalse}
\newif\ifNMSB@export\NMSB@exportfalse
\DeclareOption{export}{\NMSB@exporttrue}
\DeclareOption{noexport}{\NMSB@exportfalse}
\newif\ifNMSB@newchap\NMSB@newchaptrue
\DeclareOption{newchap}{\NMSB@newchaptrue}
\DeclareOption{newsec}{\NMSB@newchaptrue}
\DeclareOption{nonewchap}{\NMSB@newchapfalse}
\DeclareOption{nonewsec}{\NMSB@newchapfalse}
\ProcessOptions*
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{NMSB@catlevelone}
% \begin{macro}{NMSB@catleveltwo}
% These two counters are used for numbering categories. Their initial value is
% set to~$10$ here. We require that the category number always has two digits, 
% because it is important to ensure that the sorting number we will generate 
% all have the same number of digits (because the~11th entry in the first
% category should be numberred differently from the 1st entry in the~11th
% category). The definition below allows up to 89~categories (in fact, 90, but
% one is reserved as the "misc" category), and $89\times
% 89$ subcategories, which should be
% sufficient. It this is not, be aware that modifying the values below is not
% sufficient, and that several other values has to be updated.
%    \begin{macrocode}
\newcounter{NMSB@catlevelone}
\newcounter{NMSB@catleveltwo}
\setcounter{NMSB@catlevelone}{10}
\setcounter{NMSB@catleveltwo}{10}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{NMSB@catlevel}
% This counter counts the nesting depth of categories. This depth is
% limited to~$2$, and nesting $3$~categories raises an error.
% \begin{macrocode}
\newcounter{NMSB@catlevel}
\setcounter{NMSB@catlevel}{0}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{SBresetdepth}
% When using numerical labels, this defines when the label counter 
% has to be reset. $0$ means "never", $1$ means "at each new level-$1$
% category", and $2$ means "at each new category".
% \begin{macrocode}
\newcounter{SBresetdepth}
\setcounter{SBresetdepth}{0} 
%    \end{macrocode}
% \end{macro}
% \begin{macro}{NMSB@ent}
% \begin{macro}{NMSB@maxent}
% \begin{macro}{\NMSB@initent}
% \begin{macro}{NMSB@tok}
% \cmpt{NMSB@ent} will be the number of the current entry. \cmpt{NMSB@maxent}
% is the  
% max value, computed from the initial value {\makeatletter\cmd\NMSB@initent}
% in order to 
% always have the same number of digits. Here, we allow up to 900 entries per
% category or subcategory. If you \emph{really} need more, you'll probably
% also have to modify the memory limits of \LaTeX{} anyway. However, as regards
% \package{splitbib}, this can be achieved by simply replacing "100" below by,
% say, "1000".
% {\makeatletter\cmd\NMSB@tok}~is a token that will be used to protect commands against expansion.
% \begin{macrocode}
\newcounter{NMSB@ent}
\newcounter{NMSB@maxent}
\def\NMSB@initent{100}
\newtoks\NMSB@tok
\setcounter{NMSB@maxent}{\NMSB@initent0}
\addtocounter{NMSB@maxent}{-1}
\setcounter{NMSB@ent}{\NMSB@initent}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@longest}
% \begin{macro}{\NMSB@reallylongest}
% \begin{macro}{\NMSB@reallylongestlabel}
% Those variables are used when computing the longest label. This is done in
% two phases: first when reading \cmd\bibitem{}s. It is just an approximation,
% since we can't know numeric labels at that time. Exact computation is done
% at a second time,
% when writing bibliographic references. This second computation uses 
% {\makeatletter\cmd\NMSB@reallylongest and \cmd\NMSB@reallylongestlabel}.
% \begin{macrocode}
\newdimen\NMSB@longest
\newdimen\NMSB@reallylongest
\setlength{\NMSB@longest}{0pt}
\setlength{\NMSB@reallylongest}{0pt}
\def\NMSB@reallylongestlabel{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSBtitle@99}
% \begin{macro}{\NMSBprefix@9999}
% \begin{macro}{\NMSB@currprefixtok}
% \begin{macro}{\NMSB@currprefixlevelonetok}
% \begin{macro}{\SBmisctitle}
% \begin{macro}{\SBmiscprefix}
% Some macros for handling titles and prefix. "Misc" commands will be used
% for bibliographic entries whose category has not been defined.
% \begin{macrocode}
\expandafter\def\csname NMSBtitle@99\endcsname{\SBmisctitle}
\expandafter\def\csname NMSBprefix@9999\endcsname{\SBmiscprefix}
\def\SBmisctitle{Miscellaneous}
\def\SBmiscprefix{}
\newtoks\NMSB@currprefixtok
\newtoks\NMSB@currprefixlevelonetok
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@prevcat}
% \begin{macro}{\NMSB@prevcatlevelone}
% Those commands will be used for detecting category changes.
% \begin{macrocode}
\def\NMSB@prevcat{0}
\def\NMSB@prevcatlevelone{0}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@missingcat}
% \begin{macro}{\NMSB@doublecat}
% Those two commands will contain entries that are \cmd\cite{}d but have not 
% category, or that have two categories, resp.
% \begin{macrocode}
\def\NMSB@missingcat{}
\def\NMSB@doublecat{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@valuelist}
% The list of entries that appear in \cmd\SBentries, and that will be sorted.
% \begin{macrocode}
\let\NMSB@valuelist\relax
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SBabovesepwidth}
% \begin{macro}{\SBbelowsepwidth}
% Width of lines for the ``rules'' style.
% \begin{macrocode}
\newdimen\SBabovesepwidth
\newdimen\SBbelowsepwidth
\setlength{\SBabovesepwidth}{.4pt}
\setlength{\SBbelowsepwidth}{.4pt}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@penalty}
% \begin{macro}{\NMSB@halfpenalty}
% Penalties inserted after category titles, in order to avoid lonely titles at
% bottom of pages.
% \begin{macrocode}
\def\NMSB@penalty{5000}
\def\NMSB@halfpenalty{500}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@warnnocateg}
% \begin{macro}{\NMSB@warndblcateg}
% \begin{macro}{\NMSB@warnwronglongest}
% \begin{macro}{\NMSB@errtoomanycat}
% \begin{macro}{\NMSB@errtoomanyent}
% \begin{macro}{\NMSB@errcattoodepp}
% \begin{macro}{\NMSB@errentriesoutsidecat}
% \begin{macro}{\NMSB@errentrieswithoptinsidecat}
% \begin{macro}{\NMSB@erraliasoutsidecat}
% \begin{macro}{\NMSB@erraliasalreadydef}
% \begin{macro}{\NMSB@erraliasundefined}
% Errors or warning... Names should be explicit.
% \begin{macrocode}
\def\NMSB@warnnocateg#1,\end{%
  \message{---- Splitbib warning ----^^J%
    -- The following bib entries have no category: #1^^J}}
\def\NMSB@warndblcateg#1,\end{%
  \message{---- Splitbib warning ----^^J%
    -- The following bib entries have several categories: #1^^J%
    -- The first one will be used.^^J}}
\def\NMSB@warnwronglongest{%
  \expandafter\NMSB@tok\expandafter{\NMSB@reallylongestlabel}
  \message{---- Splitbib warning ----^^J%
    -- The longest label appears to be [\the\NMSB@tok]
    instead of}
  \@ifundefined{SBlongestlabel}{}{\message{(forced)}}
  \expandafter\NMSB@tok\expandafter{\NMSB@longestlabel}
  \message{[\the\NMSB@tok]^^J}}
\def\NMSB@errtoomanycat#1{%
  \message{---- Splitbib error ----^^J%
    -- You defined too many level-#1 categories (max = 89).^^J}}
\def\NMSB@errtoomanyent{%
  \setcounter{NMSB@maxent}{\NMSB@initent0}
  \addtocounter{NMSB@maxent}{-\NMSB@initent}
  \message{---- Splitbib error ----^^J%
    -- You defined too many entries in one category % 
    (max = \theNMSB@maxent)^^J}}
\def\NMSB@errcattoodeep{%
  \message{---- Splitbib error ----^^J 
    -- Only two category depth allowed.^^J}}
\def\NMSB@errentriesoutsidecat{%
  \message{---- Splitbib error ----^^J
    -- \string\SBentries outside category environment.^^J}}
\def\NMSB@errentrieswithoptinsidecat{%
  \message{----Splitbib error ----^^J
    -- \string\SBentries with optional argument inside category env.^^J}}
\def\NMSB@erraliasoutsidecat{%
  \message{----Splitbib error ----^^J
    -- \string\SBalias used outside category environment.^^J}}
\def\NMSB@erraliasalreadydef#1{%
  \message{----Splitbib error ----^^J
    -- Alias #1 multiply defined.^^J}}
\def\NMSB@erraliasundefined#1{%
  \message{----Splitbib error ----^^J
    -- Alias #1 undefined.^^J}}
\def\NMSB@errcommentoutsidecat{%
  \message{----Splitbib error ----^^J
    -- \string\SBcomment used outside category environment.^^J}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\SBtitle}
% \begin{macro}{\SBsubtitle}
% \begin{macro}{\SBtitlestyle}
% \begin{macro}{\SBsubtitlestyle}
% \begin{macro}{\NMSB@titlestyle}
% \begin{macro}{\NMSB@subtitlestyle}
% \begin{macro}{\SBtitlefont}
% \begin{macro}{\SBsubtitlefont}
% \begin{macro}{\NMSB@stylebox}
% \begin{macro}{\NMSB@stylebar}
% \begin{macro}{\NMSB@styledash}
% \begin{macro}{\NMSB@stylenone}
% \begin{macro}{\NMSB@stylesimple}
% Macros for (sub)title styles. The arguments are the numbers of the category
% and subcategory. Of course, it is also possible to add titles in the
% headers, in the table of contents,~...
% \begin{macrocode}
\def\SBtitlestyle#1{\gdef\NMSB@titlestyle{#1}}
\def\SBsubtitlestyle#1{\gdef\NMSB@subtitlestyle{#1}}
\def\NMSB@titlestyle{bar}
\def\NMSB@subtitlestyle{dash}
\def\SBtitle#1{\def\NMSB@level{title}%
  \csname NMSB@style\NMSB@titlestyle\endcsname{}{#1}}
\def\SBsubtitle#1#2{\def\NMSB@level{subtitle}%
  \csname NMSB@style\NMSB@subtitlestyle\endcsname{}{#2}}
\def\SBtitlefont#1{{\bfseries\Large #1}}
\def\SBsubtitlefont#1{{\bfseries #1}}
\def\NMSB@stylebox#1#2{\hskip-\leftmargin%
  \vbox{%
    \medskip\par
    {\null\hfill
      \setlength\fboxsep{\baselineskip}%
      \framebox[\textwidth]{%
        \csname SB\NMSB@level font\endcsname{#1#2}}%
      \hfill\null}}%
  \bigskip}
\def\NMSB@stylebar#1#2{\hskip-\leftmargin%
  \vbox{%
    \medskip\par
    \vrule height \SBabovesepwidth depth 0pt width \textwidth
    \vskip.3\baselineskip\par\noindent
    {\null\hfill
      \csname SB\NMSB@level font\endcsname{#1#2}%
      \hfill\null}%
    \vskip-.4\baselineskip\par\noindent
    \vrule height \SBbelowsepwidth depth 0pt width \textwidth}}
\def\NMSB@styledash#1#2{\unskip\hskip-\leftmargin%
  \vbox{%
    \smallskip\noindent
    {\null\hfill
      \csname SB\NMSB@level font\endcsname{---~#1#2~---}}
    \hfill\null}
  \par}
\def\NMSB@stylenone#1#2{%
  \vspace{-2\itemsep}\vspace{-\baselineskip}}
\def\NMSB@stylesimple#1#2{\hskip-\leftmargin%
  \csname SB\NMSB@level font\endcsname{#1#2}
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\category}
% \begin{macro}{\endcategory}
% \begin{macro}{\NMSB@category}
% \begin{macro}{\lNMSB@category}
% Definition of the \env{category} environment. While not too deep, we 
% increase the 
% category number and define the corresponding title and prefix.
% \begin{macrocode}
\def\category{\@ifnextchar[{\@lNMSBcategory}{\@lNMSBcategory[]}}
\def\@lNMSBcategory[#1]#2{%
  \stepcounter{NMSB@catlevel}%
  \ifnum\theNMSB@catlevel>2\relax
    \NMSB@errcattoodeep
    \addtocounter{NMSB@catlevel}{-1}%
  \fi
  \ifnum\theNMSB@catlevel=1\relax
    \ifnum\theNMSB@catlevelone=98\relax
      \NMSB@errtoomanycat{one}%
    \else
      \stepcounter{NMSB@catlevelone}%      
    \fi
    \setcounter{NMSB@catleveltwo}{10}%
    \expandafter\gdef\csname NMSBprefix@\theNMSB@catlevelone
     \endcsname{#1}%
    \expandafter\gdef\csname NMSBtitle@\theNMSB@catlevelone
     \endcsname{#2}%
  \else
    \ifnum\theNMSB@catleveltwo=98\relax
      \NMSB@errtoomanycat{two}%
    \else
      \stepcounter{NMSB@catleveltwo}%
    \fi
    \expandafter\let\expandafter\NMSB@tempentry\csname
      NMSBprefix@\theNMSB@catlevelone\endcsname
    \expandafter\NMSB@tok\expandafter{\NMSB@tempentry}
    \expandafter\xdef\csname NMSBprefix@\theNMSB@catlevelone
      \theNMSB@catleveltwo\endcsname{\the\NMSB@tok #1}%
    \expandafter\gdef\csname NMSBtitle@\theNMSB@catlevelone
      \theNMSB@catleveltwo\endcsname{#2}%
  \fi
}
\def\@NMSBcategory#1{%
  \stepcounter{NMSB@catlevel}
  \ifnum\theNMSB@catlevel>2\relax
    \NMSB@errcattoodeep
    \addtocounter{NMSB@catlevel}{-1}%
  \fi
  \ifnum\theNMSB@catlevel=1\relax
    \ifnum\theNMSB@catlevelone=98\relax
      \NMSB@errtoomanycat{one}%
    \else
      \stepcounter{NMSB@catlevelone}%
    \fi
    \setcounter{NMSB@catleveltwo}{10}
    \expandafter\gdef\csname NMSBtitle@\theNMSB@catlevelone
     \endcsname{#1}%
  \else
    \ifnum\theNMSB@catleveltwo=98\relax
      \NMSB@errtoomanycat{two}%
    \else
      \stepcounter{NMSB@catleveltwo}%
    \fi
    \expandafter\ifx\csname NMSBprefix@\theNMSB@catlevelone\endcsname
      \relax
    \else
      \expandafter\let\expandafter\NMSB@tempentry\csname
        NMSBprefix@\theNMSB@catlevelone\endcsname
      \expandafter\NMSB@tok\expandafter{\NMSB@tempentry}%
      \expandafter\xdef\csname NMSBprefix@\theNMSB@catlevelone
       \theNMSB@catleveltwo\endcsname{\the\NMSB@tok}%
    \fi
    \expandafter\gdef\csname NMSBtitle@\theNMSB@catlevelone
     \theNMSB@catleveltwo\endcsname{#1}%
  \fi
}
\def\endcategory{\addtocounter{NMSB@catlevel}{-1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\SBalias}
% This defines an alias for the category, so that you can add new 
% items in that category afterwards, by using the optional argument of
% \cmd\SBentries.
% \begin{macrocode}
\def\SBalias#1{%
  \ifnum\theNMSB@catlevel<1\relax
    \NMSB@erraliasoutsidecat
  \else
    \expandafter\ifx\csname NMSBalias@#1\endcsname\relax
      \ifnum\theNMSB@catlevel=1\relax
        \expandafter\xdef\csname NMSBalias@#1\endcsname{%
          \theNMSB@catlevelone 10}%
      \else
        \expandafter\xdef\csname NMSBalias@#1\endcsname{%
          \theNMSB@catlevelone\theNMSB@catleveltwo}%
      \fi
    \else
      \NMSB@erraliasalreadydef{#1}%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SBcomment}
% Command \cmd\SBcomment~allows you to put a comment at the beginning of each
% category. That comment will be put into a \env{minipage} for the moment, but
% that behavior should depend on the style of titles and subtitles. I'll do
% that shortly.
% \begin{macrocode}
\long\def\SBcomment#1{%
  \ifnum\theNMSB@catlevel<1\relax
    \NMSB@errcommentoutsidecat
  \else
    \ifnum\theNMSB@catlevel=1\relax
      \expandafter\gdef\csname NMSBcomment@\theNMSB@catlevelone
       \endcsname{#1}%
    \else
      \expandafter\gdef\csname NMSBcomment@\theNMSB@catlevelone
       \theNMSB@catleveltwo\endcsname{#1}%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SBentries}
% \begin{macro}{\NMSB@entries@withoptarg}
% \begin{macro}{\NMSB@entries@incatenv}
% Command \cmd\SBentries~for defining entries that should appear 
% in that category. It should be either used with an optional argument
% outside a \env{category}~environment, or without its optional
% argument inside a \env{category}~environment.
% \begin{macrocode}
\def\SBentries{\@ifnextchar[
  {\NMSB@entries@withoptarg}%
  {\NMSB@entries@incatenv}}
\def\NMSB@entries@withoptarg[#1]#2{%
  \ifnum\theNMSB@catlevel>0\relax
    \NMSB@errentrieswithoptinsidecat
  \else
    \@for\@citeb:=#2\do{%
      \expandafter\ifx\csname NMSBcateg@\@citeb\endcsname\relax
        \expandafter\ifx\csname NMSBalias@#1\endcsname\relax
          \NMSB@erraliasundefined{#1}%
        \else
          \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
            \csname NMSBalias@#1\endcsname}%
          \ifNMSB@ownorder
            \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
              \csname NMSBcateg@\@citeb\endcsname\theNMSB@ent}%
	    \stepcounter{NMSB@ent}%
            \ifnum\theNMSB@ent=\theNMSB@maxent\relax
	      \NMSB@errtoomanyent
            \fi
          \fi
        \fi
      \else
        \ifNMSB@strict
          \xdef\NMSB@doublecat{\NMSB@doublecat \@citeb,}%
        \else
          \expandafter\ifx\csname NMSBalias@#1\endcsname\relax
            \NMSB@erraliasundefined{#1}%
          \else
            \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
              \csname NMSBcateg@\@citeb\endcsname,%
              \csname NMSBalias@#1\endcsname}%
            \ifNMSB@ownorder
              \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
                \csname NMSBcateg@\@citeb\endcsname\theNMSB@ent}%
              \stepcounter{NMSB@ent}%
              \ifnum\theNMSB@ent=\theNMSB@maxent\relax
                \NMSB@errtoomanyent
              \fi
            \fi
          \fi
        \fi
      \fi}%
  \fi
}
\def\NMSB@entries@incatenv#1{%
  \ifnum\theNMSB@catlevel<1\relax
    \NMSB@errentriesoutsidecat
  \else
    \@for\@citeb:=#1\do{%
      \expandafter\ifx\csname NMSBcateg@\@citeb\endcsname\relax
        \ifnum\theNMSB@catlevel=2\relax
          \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
            \theNMSB@catlevelone\theNMSB@catleveltwo}%
        \else
          \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
             \theNMSB@catlevelone10}%
        \fi%
        \ifNMSB@ownorder
          \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
            \csname NMSBcateg@\@citeb\endcsname\theNMSB@ent}%
          \stepcounter{NMSB@ent}%
          \ifnum\theNMSB@ent=\theNMSB@maxent\relax\NMSB@errtoomanyent\fi
        \fi
      \else
        \ifNMSB@strict
          \xdef\NMSB@doublecat{\NMSB@doublecat \@citeb,}%
        \else
          \ifnum\theNMSB@catlevel=2\relax
            \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
              \csname NMSBcateg@\@citeb\endcsname,%
              \theNMSB@catlevelone\theNMSB@catleveltwo}%
          \else
            \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
              \csname NMSBcateg@\@citeb\endcsname,%
              \theNMSB@catlevelone10}%
          \fi
        \fi
        \ifNMSB@ownorder
          \expandafter\xdef\csname NMSBcateg@\@citeb\endcsname{%
            \csname NMSBcateg@\@citeb\endcsname\theNMSB@ent}%
          \stepcounter{NMSB@ent}%
          \ifnum\theNMSB@ent=\theNMSB@maxent\relax\NMSB@errtoomanyent\fi
        \fi
      \fi}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSBorig@@lbibitem}
% \begin{macro}{\NMSBorig@@bibitem}
% \begin{macro}{\NMSBrealorig@@bibitem}
% Keep track of the original \cmd\bibitem~commands.
% \begin{macrocode}
\let\@NMSBrealorig@@bibitem\@bibitem
\let\@NMSBorig@@lbibitem\@lbibitem
\def\@NMSBorig@@bibitem#1{\item\if@filesw \immediate\write\@auxout
  {\string\bibcite{#1}{\the\NMSB@currprefixtok\the\value{\@listctr}}}%
  \fi\ignorespaces}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@getcateg}
% \begin{macro}{\NMSB@getent}
% Those macros retrive the category and entry number from their concatenation.
% This is used when reordering.
% \begin{macrocode}
\def\NMSB@getcateg#1#2#3#4#5-{#1#2#3#4}
\def\NMSB@getent#1#2#3#4#5-{#5}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\@lbibitem}
% \begin{macro}{\@bibitem}
% These are the new \cmd\bibitem~commands. They won't print
% anything. Instead, they write their arguments in different commands
% named after the number of the current 
% entry. Longest label is also evaluated.
% \begin{macrocode}
\def\@lbibitem[#1]#2#3\par{%
  \expandafter\ifx\csname NMSBcateg@#2\endcsname\relax
    \expandafter\gdef\csname NMSBcateg@#2\endcsname{9999}%
    \edef\NMSB@missingcat{\NMSB@missingcat #2,}%
%    \ifNMSB@ownorder
%      \expandafter\xdef\csname NMSBcateg@#2\endcsname{%
%        \csname NMSBcateg@#2\endcsname\theNMSB@maxent}%
%    \fi
  \fi
  \let\@tempa\relax
  \ifNMSB@ownorder
    \edef\NMSB@temp{\csname NMSBcateg@#2\endcsname}%
    \@for\NMSB@local:=\NMSB@temp\do{%
      \ifx\relax\@tempa\relax
        \edef\@tempa{\expandafter\NMSB@getcateg\NMSB@local-}%
      \else
        \edef\@tempa{\@tempa,\expandafter\NMSB@getcateg\NMSB@local-}%
      \fi
    }%
  \else
    \edef\@tempa{\csname NMSBcateg@#2\endcsname}%
  \fi
  \expandafter\ifx\csname NMSBprefix@\@tempa\endcsname\relax
    \def\@tempb{#1}%
  \else
    \expandafter\let\expandafter\NMSB@tempentry\csname
      NMSBprefix@\@tempa\endcsname
    \expandafter\NMSB@tok\expandafter{\NMSB@tempentry #1}%
    \edef\@tempb{\the\NMSB@tok}%
  \fi
  \@ifundefined{SBlongestlabel}{%
    \setbox\@tempboxa=\hbox{\@tempb}%
    \ifdim\NMSB@longest<\wd\@tempboxa 
      \setlength\NMSB@longest{\wd\@tempboxa}%
      \global\let\NMSB@longestlabel\@tempb
    \fi}{}%
  \ifNMSB@ownorder
    \edef\NMSB@temp{\csname NMSBcateg@#2\endcsname}%
    \@for\NMSB@local:=\NMSB@temp\do{%
      \edef\NMSB@tempb{\expandafter\NMSB@getent\NMSB@local-}
      \expandafter\xdef\csname NMSBkey@\NMSB@tempb\endcsname{#2}%
      \global\expandafter\let\csname NMSBlabel@\NMSB@tempb\endcsname
        \@tempb
      \expandafter\gdef\csname NMSBentry@\NMSB@tempb\endcsname{#3}%
      \ifx\relax\NMSB@valuelist\relax
        \xdef\NMSB@valuelist{\NMSB@local}%
      \else
        \xdef\NMSB@valuelist{\NMSB@valuelist,\NMSB@local}%
      \fi}%
  \else
    \expandafter\xdef\csname NMSBkey@\theNMSB@ent\endcsname{#2}%
    \global\expandafter\let\csname NMSBlabel@\theNMSB@ent\endcsname
      \@tempb
    \expandafter\gdef\csname NMSBentry@\theNMSB@ent\endcsname{#3}%
    \@for\NMSB@item:=\@tempa\do{%
      \ifx\relax\NMSB@valuelist\relax
        \xdef\NMSB@valuelist{\NMSB@item\theNMSB@ent}%
      \else
        \xdef\NMSB@valuelist{\NMSB@valuelist,\NMSB@item\theNMSB@ent}%
      \fi}%
    \stepcounter{NMSB@ent}%
    \ifnum\theNMSB@ent=\theNMSB@maxent\relax\NMSB@errtoomanyent\fi
  \fi
}
\def\@bibitem#1#2\par{%
  \expandafter\ifx\csname NMSBcateg@#1\endcsname\relax
    \expandafter\gdef\csname NMSBcateg@#1\endcsname{9999}%
%    \ifNMSB@ownorder
%      \expandafter\xdef\csname NMSBcateg@#1\endcsname{%
%        \csname NMSBcateg@#1\endcsname\theNMSB@maxent}%
%    \fi
    \edef\NMSB@missingcat{\NMSB@missingcat #1,}%
  \fi
  \def\@tempa{}%
  \ifNMSB@ownorder
    \edef\NMSB@temp{\csname NMSBcateg@#1\endcsname}%
    \@for\NMSB@local:=\NMSB@temp\do{%
      \ifx\relax\@tempa\relax
        \edef\@tempa{\expandafter\NMSB@getcateg\NMSB@local-}%
      \else
        \edef\@tempa{\@tempa,\expandafter\NMSB@getcateg\NMSB@local-}%
      \fi}%
  \else
    \edef\@tempa{\csname NMSBcateg@#1\endcsname}%
  \fi
  \@ifundefined{SBlongestlabel}{%
    \expandafter\ifx\csname NMSBprefix@\@tempa\endcsname\relax
      \let\@tempb\NMSB@initiallongestlabel
    \else
      \expandafter\let\expandafter\NMSB@tempentry\csname
        NMSBprefix@\@tempa\endcsname
      \expandafter\expandafter\expandafter\NMSB@tok
      \expandafter\expandafter\expandafter{%
        \expandafter\NMSB@tempentry
        \NMSB@initiallongestlabel}%
      \edef\@tempb{\the\NMSB@tok}%
    \fi
    \setbox\@tempboxa=\hbox{\@tempb}%
    \ifdim\NMSB@longest<\wd\@tempboxa 
      \setlength\NMSB@longest{\wd\@tempboxa}%
      \global\let\NMSB@longestlabel\@tempb
    \fi}{}%
  \ifNMSB@ownorder
    \edef\NMSB@temp{\csname NMSBcateg@#1\endcsname}%
    \@for\NMSB@local:=\NMSB@temp\do{%
      \edef\NMSB@tempb{\expandafter\NMSB@getent\NMSB@local-}
      \expandafter\xdef\csname NMSBkey@\NMSB@tempb\endcsname{#1}%
      \expandafter\gdef\csname NMSBentry@\NMSB@tempb\endcsname{#2}%
      \ifx\relax\NMSB@valuelist\relax
        \xdef\NMSB@valuelist{\NMSB@local}%
      \else
        \xdef\NMSB@valuelist{\NMSB@valuelist,\NMSB@local}%
      \fi}%
  \else
    \expandafter\xdef\csname NMSBkey@\theNMSB@ent\endcsname{#1}%
    \expandafter\gdef\csname NMSBentry@\theNMSB@ent\endcsname{#2}%
    \@for\NMSB@item:=\@tempa\do{%
      \ifx\relax\NMSB@valuelist\relax
        \xdef\NMSB@valuelist{\NMSB@item\theNMSB@ent}%
      \else
        \xdef\NMSB@valuelist{\NMSB@valuelist,\NMSB@item\theNMSB@ent}%
      \fi}%
    \stepcounter{NMSB@ent}%
    \ifnum\theNMSB@ent=\theNMSB@maxent\relax\NMSB@errtoomanyent\fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@afterfi}
% \begin{macro}{\NMSB@afterelse}
% \begin{macro}{\NMSB@empty}
% \begin{macro}{\NMSB@pivot}
% \begin{macro}{\NMSB@qsort}
% \begin{macro}{\NMSB@resort}
% \begin{macro}{\NMSB@sort}
% \begin{macro}{\NMSB@sortlt}
% The sorting commands. This is an implementation of the quicksort
% algorithm. 
% \begin{macrocode}
\def\NMSB@afterfi#1\fi{\fi#1}
\def\NMSB@afterelse#1\else#2\fi{\fi#1}
\def\NMSB@empty{}
\def\NMSB@pivot#1#2#3#4#5,{%
  \ifx\relax#5\NMSB@empty%
    \ifx\relax#3\relax\else\NMSB@resort{#1}{#3}, \fi%
    #2%
    \ifx\relax#4\relax\else,\NMSB@resort{#1}{#4}\fi%
  \else%
    \ifnum#5#1#2 \NMSB@afterelse{\NMSB@afterfi{%
        \NMSB@pivot{#1}{#2}{#3#5,}{#4}}}%
    \else%
      \NMSB@afterfi{\NMSB@afterfi{%
          \NMSB@pivot{#1}{#2}{#3}{#4#5,}}}%
    \fi
  \fi
}%
\def\NMSB@qsort#1#2,{%
  \ifx\relax#2\relax\else
    \NMSB@afterfi{\NMSB@pivot{#1}{#2}{}{}}\fi}
\def\NMSB@resort#1#2{\NMSB@qsort{#1}#2\relax,}
\def\NMSBsort#1#2{\NMSB@qsort{#1}#2,\relax,}
\def\NMSBsortlt#1{\NMSBsort{<}{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\NMSB@writecatbib}
% Macros for writing unexpanded commands into the \ext{sbb}~file.
% \begin{macrocode}
\long\def\NMSB@writecatbib#1{%
  \NMSB@tok{#1}%
  \immediate\write\NMSB@catbib{\the\NMSB@tok}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\NMSB@writeentry}
% \begin{macro}{\NMSB@writelist}
% Those macros are to write one entry, and the list of entries, respectively.
% When writing an entry, we have to detect category-changes, for writing 
% the (sub)title. 
% {\makeatletter\cmd\NMSB@writeentry}~is becoming more and more complex, 
% but it mainly detects category changes, writes category titles if needed, 
% and writes a \cmd\bibitem.
% \begin{macrocode}
\def\NMSB@writeentry#1#2#3#4#5,{\ifx\relax #5\relax 
  \else
    \def\NMSB@currcat{#1#2#3#4}%
    \def\NMSB@currcatlevelone{#1#2}%
    \ifx\NMSB@currcatlevelone\NMSB@prevcatlevelone\else
      \expandafter\ifx\csname NMSBtitle@\NMSB@currcatlevelone
       \endcsname\relax
      \else
        \ifNMSB@export
          \if@filesw
            \immediate\write\NMSB@catbib{%
              \string\par\string\addpenalty{-\NMSB@penalty}%
              \string\relax^^J%
              \string\item[]%
              \string\SBtitle}%
            \expandafter\let\expandafter\NMSB@tempentry
              \csname NMSBtitle@\NMSB@currcatlevelone\endcsname
            \expandafter\NMSB@writecatbib\expandafter{%
              \expandafter{\NMSB@tempentry}}%
            \immediate\write\NMSB@catbib{\string\relax^^J%
              \string\par\string\addpenalty{\NMSB@penalty}%
              \string\relax}%
            \expandafter\ifx\csname 
             NMSBcomment@\NMSB@currcatlevelone\endcsname\relax
            \else
              \immediate\write\NMSB@catbib{\string\vskip2ex^^J%
                \string\hspace{-\leftmargin}\string\relax^^J%
                \string\begin{minipage}{\textwidth}^^J%
                \string\addtolength\string\parindent{20pt}^^J%
                \string\noindent}
              \expandafter\let\expandafter\NMSB@tempentry
                \csname NMSBcomment@\NMSB@currcatlevelone\endcsname
              \expandafter\NMSB@writecatbib\expandafter{%
                \NMSB@tempentry^^J}%
              \immediate\write\NMSB@catbib{\string\end{minipage}^^J%
                \string\par\string\addpenalty{\NMSB@penalty}
                \string\vskip2ex}
            \fi
          \fi
        \else
          \par\addpenalty{-\NMSB@penalty}%
          \item[]%
            \SBtitle{\csname NMSBtitle@\NMSB@currcatlevelone\endcsname}
          \par\addpenalty{\NMSB@penalty}%
          \expandafter\ifx\csname 
           NMSBcomment@\NMSB@currcatlevelone\endcsname\relax
          \else
            \vskip2ex\hspace{-\leftmargin}\begin{minipage}{\textwidth}%
            \addtolength\parindent{20pt}\noindent%
            \csname NMSBcomment@\NMSB@currcatlevelone\endcsname
            \end{minipage}%
            \par\addpenalty{\NMSB@penalty}\vskip2ex
          \fi
        \fi
      \fi
      \xdef\NMSB@prevcatlevelone{#1#2}%
      \ifnum\theSBresetdepth>0\relax
        \setcounter{\@listctr}{0}%
      \fi
      \expandafter\ifx\csname NMSBprefix@\NMSB@prevcatlevelone
       \endcsname\relax
        \NMSB@currprefixlevelonetok{\relax}%
      \else
        \expandafter\expandafter\expandafter\NMSB@currprefixlevelonetok
        \expandafter\expandafter\expandafter{%
           \csname NMSBprefix@\NMSB@prevcatlevelone\endcsname}%
      \fi
    \fi
    \ifx\NMSB@currcat\NMSB@prevcat\else
      \ifnum\NMSB@currcat=9999\else
        \expandafter\ifx\csname NMSBtitle@\NMSB@currcat\endcsname
         \relax
        \else
          \ifNMSB@export
            \if@filesw
              \immediate\write\NMSB@catbib{%
                \string\par\string\addpenalty{-\NMSB@halfpenalty}%
                \string\relax^^J%
                \string\item[]%
                \string\SBsubtitle}%
              \expandafter\let\expandafter\NMSB@tempentrya
                \csname NMSBtitle@\NMSB@currcatlevelone\endcsname
              \expandafter\let\expandafter\NMSB@tempentryb
                \csname NMSBtitle@\NMSB@currcat\endcsname
              \expandafter\NMSB@writecatbib\expandafter{%
                \expandafter{\NMSB@tempentrya}}
              \expandafter\NMSB@writecatbib\expandafter{%
                \expandafter{\NMSB@tempentryb}}
              \immediate\write\NMSB@catbib{\string\relax^^J%
                \string\par\string\addpenalty{\NMSB@penalty}%
                \string\relax}%
              \expandafter\ifx\csname 
               NMSBcomment@\NMSB@currcat\endcsname\relax
              \else
                \immediate\write\NMSB@catbib{\string\vskip2ex^^J%
                  \string\hspace{-\leftmargin}\string\relax^^J%
                  \string\begin{minipage}{\textwidth}^^J%
                  \string\addtolength\string\parindent{20pt}^^J%
                  \string\noindent}
                \expandafter\let\expandafter\NMSB@tempentry
                  \csname NMSBcomment@\NMSB@currcat\endcsname
                \expandafter\NMSB@writecatbib\expandafter{%
                  \NMSB@tempentry^^J}%
                \immediate\write\NMSB@catbib{\string\end{minipage}^^J%
                  \string\par\string\addpenalty{\NMSB@penalty}%
                  \string\vskip2ex}
              \fi
            \fi
          \else
            \par\addpenalty{-\NMSB@halfpenalty}%
            \item[]
            \SBsubtitle{\csname NMSBtitle@\NMSB@currcatlevelone\endcsname}%
                       {\csname NMSBtitle@\NMSB@currcat\endcsname}%
            \par\addpenalty{\NMSB@penalty}%
            \expandafter\ifx\csname 
             NMSBcomment@\NMSB@currcat\endcsname\relax
            \else
              \vskip2ex\hspace{-\leftmargin}\begin{minipage}{\textwidth}%
              \addtolength\parindent{20pt}\noindent%
              \csname NMSBcomment@\NMSB@currcat\endcsname
              \end{minipage}%
              \par\addpenalty{\NMSB@penalty}\vskip2ex
            \fi
          \fi
        \fi
      \fi
      \xdef\NMSB@prevcat{#1#2#3#4}%
      \ifnum\theSBresetdepth>1\relax
        \setcounter{\@listctr}{0}%
      \fi
      \expandafter\ifx\csname NMSBprefix@\NMSB@currcat\endcsname\relax
        \expandafter\NMSB@currprefixtok\expandafter{%
           \the\NMSB@currprefixlevelonetok}%
      \else
        \expandafter\expandafter\expandafter\NMSB@currprefixtok
        \expandafter\expandafter\expandafter{%
          \csname NMSBprefix@\NMSB@currcat\endcsname}
      \fi
    \fi
    \expandafter\ifx\csname NMSBlabel@#5\endcsname\relax
      \ifNMSB@export
        \if@filesw
          \stepcounter\@listctr
          \immediate\write\NMSB@catbib{%
            \string\bibitem}%
	  \expandafter\ifx\expandafter\relax\the\NMSB@currprefixtok
          \else
	    \expandafter\expandafter\expandafter\def
	    \expandafter\expandafter\expandafter\NMSB@tempentry
	    \expandafter\expandafter\expandafter{%
	      \expandafter\the\expandafter\NMSB@currprefixtok
              \the\value{\@listctr}}%
            \expandafter\NMSB@writecatbib\expandafter{%
              \expandafter[\NMSB@tempentry]}%
	  \fi
          \immediate\write\NMSB@catbib{%
            {\csname NMSBkey@#5\endcsname}}
          \expandafter\let\expandafter\NMSB@tempentry
          \csname NMSBentry@#5\endcsname%
          \expandafter\NMSB@writecatbib\expandafter{%
            \NMSB@tempentry^^J^^J}
        \fi
      \else
        \@NMSBorig@@bibitem{\csname NMSBkey@#5\endcsname}%
        \csname NMSBentry@#5\endcsname
      \fi
      \setbox\@tempboxa=\hbox{\the\NMSB@currprefixtok\the\value{\@listctr}}%
      \ifdim\NMSB@reallylongest<\wd\@tempboxa 
        \setlength{\NMSB@reallylongest}{\wd\@tempboxa}%
        \xdef\NMSB@reallylongestlabel{%
          \expandafter\ifx\expandafter\relax\the\NMSB@currprefixtok
	  \else\the\NMSB@currprefixtok\fi\the\value{\@listctr}}%
      \fi
    \else
      \ifNMSB@export
        \if@filesw
          \immediate\write\NMSB@catbib{%
            \string\bibitem}
          \expandafter\let\expandafter\NMSB@tempentry
            \csname NMSBlabel@#5\endcsname
          \expandafter\NMSB@writecatbib\expandafter{%
            \expandafter[\NMSB@tempentry]}%
          \immediate\write\NMSB@catbib{%
            {\csname NMSBkey@#5\endcsname}}%
          \expandafter\let\expandafter\NMSB@tempentry
            \csname NMSBentry@#5\endcsname%
          \expandafter\NMSB@writecatbib\expandafter{%
            \NMSB@tempentry^^J^^J}
        \fi
      \else
        \@NMSBorig@@lbibitem[\csname NMSBlabel@#5\endcsname]%
                            {\csname NMSBkey@#5\endcsname}%
                            \csname NMSBentry@#5\endcsname
      \fi
      \setbox\@tempboxa=\hbox{\csname NMSBlabel@#5\endcsname}%
      \ifdim\NMSB@reallylongest<\wd\@tempboxa 
        \setlength{\NMSB@reallylongest}{\wd\@tempboxa}%
        \expandafter\let\expandafter\NMSB@reallylongestlabel\csname 
	  NMSBlabel@#5\endcsname
      \fi
    \fi
  \fi
}
\def\NMSB@writelist#1{%
  \@for\NMSB@curritem:=#1\do{%
    \edef\NMSB@curritem{\expandafter
      \@firstofone\NMSB@curritem\@empty}%
    \expandafter\NMSB@writeentry\NMSB@curritem ,}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\@NMSBorig@thebibliography}
% \begin{macro}{\@NMSBorig@endthebibliography}
% \begin{macro}{\thebibliography}
% \begin{macro}{\endthebibliography}
% \cmd\thebibliography~does not really use its argument. It's simply used for 
% approximating the longest label. Note that
% \cmd\endthebibliography~writes the entries, since sorting has to be
% done after \cmd\bibitem{}s have treated all the entries. 
% \begin{macrocode}
\let\@NMSBorig@thebibliography\thebibliography
\let\@NMSBorig@endthebibliography\endthebibliography
\def\thebibliography#1{%
  \setcounter{NMSB@ent}{\NMSB@initent}%
  \@ifundefined{SBlongestlabel}{%
    \gdef\NMSB@initiallongestlabel{#1}}{%
    \global\let\NMSB@longestlabel\SBlongestlabel
    \setbox\@tempboxa=\hbox{\SBlongestlabel}%
    \setlength{\NMSB@longest}{\wd\@tempboxa}}%
  \ifNMSB@export
    \if@filesw
      \newwrite\NMSB@catbib
      \immediate\openout\NMSB@catbib \jobname.sbb\relax
      \expandafter\NMSB@tok\expandafter{\SBlongestlabel}
      \immediate\write\NMSB@catbib{%
        \string\begin{thebibliography}{%
        \@ifundefined{SBlongestlabel}{#1}{\the\NMSB@tok}}}
    \fi
  \fi
}
\def\endthebibliography{%
  \ifNMSB@export
    \usecounter{enumiv}%
  \else
    \ifNMSB@newchap
      \@ifundefined{chapter}{\section*{\refname}}{\chapter*{\bibname}}%
    \fi   
    \list{\@biblabel{\the\NMSB@currprefixtok\@arabic\c@enumiv}\hfill}{%
      \settowidth\labelwidth{\@biblabel{\NMSB@longestlabel}}%
      \leftmargin\labelwidth
      \advance\leftmargin\labelsep
      \@openbib@code
      \usecounter{enumiv}%
      \let\p@enumiv\@empty
      \renewcommand\theenumiv{\@arabic\c@enumiv}}%
  \fi
  \edef\NMSB@sortedvaluelist{%
    \expandafter\NMSBsortlt\expandafter{\NMSB@valuelist}}%
  \expandafter\NMSB@writelist\expandafter{\NMSB@sortedvaluelist}%
  \ifNMSB@export
    \if@filesw
      \immediate\write\NMSB@catbib{%
        \string\end{thebibliography}}
      \immediate\closeout\NMSB@catbib
    \fi
    \begingroup
    \ifNMSB@newchap\else
      \@ifundefined{chapter}{\def\section##1##2{}}{\def\chapter##1##2{}}%
    \fi
    \let\thebibliography\@NMSBorig@thebibliography
    \let\endthebibliography\@NMSBorig@endthebibliography
    \let\@lbibitem\@NMSBorig@@lbibitem
    \let\@bibitem\@NMSBrealorig@@bibitem
    \@input@{\jobname.sbb}
    \endgroup
  \else
    \endlist
  \fi
  \ifx\NMSB@missingcat\NMSB@empty\else
    \expandafter\NMSB@warnnocateg\NMSB@missingcat\end
  \fi
  \ifNMSB@strict
    \ifx\NMSB@doublecat\NMSB@empty\else
      \expandafter\NMSB@warndblcateg\NMSB@doublecat\end
    \fi
  \fi
  \ifdim\NMSB@reallylongest=\NMSB@longest\relax\else
    \NMSB@warnwronglongest
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \setcounter{IndexColumns}{2}
% \IfFileExists{splitbib.ind}{}{%
%    \message{Please run "makeindex -s gind.ist splitbib.idx" for the index.}}
% \PrintIndex
% \Finale
\endinput