summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/splitbib/splitbib.dtx
blob: da8c49d2612816f87b1bfafc3b0653b7db066cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
% \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