summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/notes2bib/xnotes2bib.dtx
blob: aae13e483c095c9828a36ccbde1179a8ea2885cd (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
% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
---------------------------------------------------------------
The notes2bib bundle --- Integrating notes into the
  bibliography
Maintained by Joseph Wright
E-mail: joseph.wright@morningstar2.co.uk
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
---------------------------------------------------------------

The notes2bib package defines a new type of note, bibnote,
which will always be added to the bibliography.  The package
allows footnotes and endnotes to be moved into the bibliography
in the same way.  The package can be used with natbib and
biblatex as well as plain LaTeX citations. Both sorted and
unsorted bibliography styles are supported.

The bundle includes both a current release version of the package
(notes2bib.sty) and an experimental re-implementation using
LaTeX3 syntax where possible (xnotes2bib.sty).
%</readme>
%<*internal>
\fi
\begingroup
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
---------------------------------------------------------------
The notes2bib bundle --- Integrating notes into the
  bibliography
Maintained by Joseph Wright
E-mail: joseph.wright@morningstar2.co.uk
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
---------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2007-2008 by
  Joseph Wright <joseph.wright@morningstar2.co.uk>

It may be distributed and/or modified under the conditions of
the LaTeX Project Public License (LPPL), either version 1.3c of
this license or (at your option) any later version.  The latest
version of this license is in the file:

   http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
  Joseph Wright.

This work consists of the file  notes2bib.dtx
                                xnotes2bib.dtx
          and the derived files notes2bib.pdf,
                                notes2bib.sty,
                                notes2bib.ins,
                                xnotes2bib.pdf,
                                xnotes2bib.sty and
                                xnotes2bib.ins.

\endpostamble
\usedir{tex/latex/notes2bib}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/notes2bib}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/notes2bib}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\def\destdir{}
\generate{
  \file{jawl3doc.sty}{\from{\jobname.dtx}{jawl3doc}}
}
\endgroup
\immediate
  \write18{makeindex -s gind.ist -o \jobname.ind \jobname.idx}
\immediate
  \write18{makeindex -s gglo.ist -o \jobname.gls \jobname.glo}
%</internal>
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\usepackage{jawl3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%\CheckSum{478}
%
%\GetFileInfo{\jobname.sty}
%
%\title{\texorpdfstring{\currpkg}{\jobname} ---
%  Integrating notes into the bibliography^^A
%  \thanks{This file describes version \fileversion, last
%    revised \filedate.}}
%\author{Joseph Wright^^A
%  \thanks{E-mail: joseph.wright@morningstar2.co.uk}}
%\date{Released \filedate}
%
%\maketitle
%
%\begin{abstract}
% The \currpkg\ package is an \emph{experimental}
% re-implementation of \pkg{notes2bib} using \LaTeX3 commands
% internally (taken from the \pkg{expl3} bundle).  The user
% interface is almost identical to \pkg{notes2bib}.
%
% The \currpkg package defines a new type of note,
% \cs{bibnote}, which will always be added to the bibliography.
% The package allows footnotes and endnotes to be moved into
% the bibliography in the same way.  The package can be used
% with \pkg{natbib} and \pkg{biblatex} as well as plain \LaTeX\
% citations. Both sorted and unsorted bibliography styles are
% supported.
%\end{abstract}
%
%\begin{multicols}{2}
%  \tableofcontents
%\end{multicols}
%
%\section{Introduction}
% The new code syntax introduced by \LaTeX3 is very different
% from plain \TeX\ or \LaTeXe.  This means that converting
% packages to \LaTeX3 requires working from the ground up if
% the new ideas are to be exploited fully.  This package
% re-implements \pkg{notes2bib} using the experimental syntax
% for internal commands.
%
% The internal logic of the package remains broadly similar to
% that in \pkg{notes2bib}.  However, some opportunities to
% simplify the code or make the flow clearer have been taken.
% There is therefore no one-to-one correspondence between the
% internals of the two packages.  In particular, this package
% does not guarantee backward-compatibility with
% \pkg{notes2bib} under all circumstances.  \LaTeX3 will
% require significant changes to documents, and so alterations
% to the logic of packages in order to make them more useful in
% the long term seem sensible.
%
%\section{Using the package}
%\subsection{Macros}
%\DescribeMacro{\bibnote}
%\DescribeMacro{\bibnotemark}
%\DescribeMacro{\bibnotetext}
%\DescribeMacro{\flushnotestack}
% The user macros of \currpkg\ are intended to
% work in the same way as those in \pkg{notes2bib}.  Please
% see \file{notes2bib.pdf} for details of:
%\begin{itemize}
%  \item \cs{bibnote}
%  \item \cs{bibnotemark}
%  \item \cs{bibnotetext}
%  \item \cs{flushnotestack}
%\end{itemize}
% The \cs{citenote} macro can be replaced by using
% \cs{bibnotemark} to cite the note for a second time.
%
%\subsection{Control values}
%\DescribeMacro{\niibsetup}
% The main user changes between \pkg{notes2bib} and
% \pkg{xnotes2bib} are in the user options.  These have been
% completely re-written here, to improve the logic of the
% interface and to remove unnecessary options. All package
% control is currently carried out using the \cs{niibsetup}
% macro, as the \LaTeXe\ kernel expands options and removes
% spaces. As in \pkg{notes2bib}, the \cs{niibsetup} macro
% accepts a key--value list as input.
%
% There are four types of control key.  Boolean keys take the
% values \opt{true} and \opt{false}.  If the key name alone is
% given, this is the same as giving the \opt{true} value.
% String keys save their value as a string for internal use:
% they can therefore take any input. Choice keys take one of a
% range of pre-determined values, in a similar way to Boolean
% options.  However, the list of possible values varies from
% key to key. Finally, macro keys require a macro as the value.
%
%\DescribeOption{convert~endnotes}
%\DescribeOption{convert~footnotes}
% To convert endnotes and footnotes into bibnotes, the options
% \opt{convert~endnotes} and \opt{convert~footnotes} are
% available.  Both taken Boolean values, and are set to
% \opt{false} when loading \currpkg.  If endnotes are not
% defined elsewhere, the \opt{convert~endnotes} only defines
% them when set to \opt{true}.
%
%\DescribeOption{placement}
% The package is able to position notes in three ways in the
% bibliography. The standard method is to create the notes
% interspersed with ``real'' citations. The order in the source
% then determines the position in the bibliography. The package
% can also create notes such that they appear either before or
% after normal citations. The behaviour here is controlled by
% the \opt{placement} option, which takes a choice from the
% list \opt{after}, \opt{before} and \opt{mixed}. The
% \opt{after} option places notes after normal citations,
% \opt{before} places notes first and \opt{mixed} mixes notes
% and citations based on the order in the source.
%
%\newcommand*{\field}[1]{\texttt{#1}}
%\DescribeOption{key~after}
%\DescribeOption{key~before}
%\DescribeOption{key~mixed}
% Most sorted bibliography styles provide methods for
% controlling the sort order of entries.  With standard
% \BibTeX\ styles, the \field{key} field is used for this
% purpose. The options \opt{key~after}, \opt{key~before} and
% \opt{key~mixed} are used to set this up.  They contain an
% item to be added before the note label when creating the
% \field{key} field.  The standard values are \opt{aaa} for
% \opt{key~before} and \opt{zzz} for \opt{key~after}:
% \opt{key~mixed} is empty.
%\DescribeOption{presort~after}
%\DescribeOption{presort~before}
%\DescribeOption{presort~mixed}
% When using \pkg{biblatex}, \field{presort} field is
% available. The values used by \currpkg\ for this field are
% stored as \opt{presort~after}, \opt{presort~before} and
% \opt{presort~mixed}, with default values \opt{mn}, \opt{ml}
% and \opt{mm}, respectively. The \opt{presort} system is
% described in the \pkg{biblatex} manual.
%
%\DescribeOption{cite}
% As \currpkg\ uses \BibTeX\ to create notes, it uses a
% citation command to mark the notes in the body of the text.
% The command used is controlled by the macro key \opt{cite}.
% The standard value is \cs{cite}.
%
%\DescribeOption{field}
%\DescribeOption{notename}
%\DescribeOption{prefix}
%\DescribeOption{record}
% Four control values are available to affect low-level aspects
% of the creation of the notes database.  The \opt{prefix}
% option sets the string used by \currpkg\ to name the
% database, and is set to \opt{niib-} initially.  The
% \opt{field} and \opt{record} options contain the name of the
% field used for the note, and the \BibTeX\ record type used.
% The standard values are \opt{note} and \opt{Misc},
% respectively.  Finally, the \opt{notename} option sets the
% text used in addition to a number when automatically
% labelling notes.  This is set to \opt{Bibnote}: there is no
% space between this text and the number as this will confuse
% \BibTeX.
%
%\DescribeOption{debug}
% The \opt{debug} Boolean option is available for understanding
% what the package is doing.  When set to \opt{true}, the
% package writes additional information to the log to aid
% problem solving.
%
%\section{Testing the package}
%
% This release of \currpkg\ has several aims, all related to
% testing. Firstly, it provides a method to examine new ideas
% for \pkg{notes2bib}: feedback on the altered options is
% welcome.  Second, it is a test-bed for using \pkg{expl3} to
% create packages.  People familiar with the current \LaTeX3
% ideas are encouraged to read the source and make suggestions.
% The new coding environment requires a more structured
% approach than \LaTeXe, and so it is possible ideas have been
% missed.
%
% The author wonders if this package could be implemented using
% the \pkg{template} system and \pkg{xfootnotes}.  Suggestions
% in this area are welcome!
%
% In order to use \currpkg, you will need an up-to-date \TeX\
% system and recent copies of \pkg{expl3} and \pkg{xparse}. The
% package is tested with the current SVN release of the \LaTeX3
% packages, but these may change on a day-to-day basis. So
% testers should be prepared for problems such as undefined
% macros from the new kernel! The \eTeX\ extensions to \TeX\
% are required by \currpkg.
%
% Due to the experimental nature of the package, it currently
% has no version number.  The package date should be used to
% check if versions are identical.  That said, the version of
% \pkg{notes2bib} will be stepped when new copies of \currpkg\
% are created.
%
% While the package is experimental, it should work properly as
% a replacement for \pkg{notes2bib}, with the proviso that the
% options are very different.  Any problems of function should
% be reported as normal.
%
%\section{Installation}
%
% The entire bundle is supplied with the TDS-ready \ext{zip}
% file \file{notes2bib.tds.zip}.  Simply unzip this into your
% local texmf tree and run your hash program (\texttt{texhash}
% for \TeX{}Live or \texttt{initextmf -u} for MiK\TeX)
%
% To extract the package \file{xnotes2bib.sty} from
% \file{xnotes2bib.dtx}, run (pdf)\LaTeX\ on the later.  This
% will also create the documentation and \file{README.txt}
% file.  Three (pdf)\LaTeX\ runs with \cs{write18} enabled will
% also build the index and table of contents in the \ext{pdf}.
%
%\StopEventually{%
%  \PrintChanges
%  \PrintIndex}
%
%\iffalse
%<*package>
%\fi
%
%\section{Implementation}
%\subsection{Preliminaries}
%\changes{v0.0}{2008/09/09}{Replaced use of \cs{def_new:NNn}
%  and \cs{def:NNn} with \cs{def_new:Npn} and \cs{def:Npn},
%  respectively}
%\changes{v0.0}{2008/09/09}{Replaced \cs{cs_use:c} by
%  \cs{use:c}}
% The package starts by loading the basics of \pkg{expl3}. To
% keep things clear, first \pkg{l3names} is loaded to do the
% basic identification, and then everything else is started up.
% This is based on Morten H{\o}gholm's \pkg{xfrac} package
% lead-off.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\begingroup
  \@ifundefined{eTeXversion}{%
    \PackageError{xnotes2bib}
      {Not running under e-TeX}
      {This package requires e-TeX: enable the extensions!}%
    \endgroup\endinput
  }{%
    \endgroup
  }
\RequirePackage{l3names}
\ProvidesExplPackage
  {xnotes2bib}{2008/09/09}{0.0}
  {Integrating notes into the bibliography (expl3 version)}
\RequirePackage{expl3,xparse}
%    \end{macrocode}
% As the \pkg{expl3} syntax is in use, there is no need to
% worry about white space in the code.  So there are very few
% |%| characters in the following. As far as possible,
% \pkg{expl3} or \pkg{xparse} macros are used here; however, a
% lot of the user-space \LaTeXe\ macros should still be valid
% in \LaTeX3.  There are also a few |@| macros where things are
% tidied closely to the \LaTeXe\ kernel at present (for example
% \cs{@filesw}).
%
% As the \LaTeXe\ kernel expands package options, for the
% moment \currpkg\ does not accept any load-time options.
%    \begin{macrocode}
\DeclareOption*{
  \PackageWarning{xnotes2bib}{
    All~package~control~should~be~given~using~the\MessageBreak
    \token_to_string:N\niibsetup\space macro
  }
}
\ProcessOptions\scan_stop:
%    \end{macrocode}
%
%\begin{macro}{\g_niib_note_int}
% A counter for the automatically-created notes is needed. This
% is a global value (life will get very complicated if not).
%    \begin{macrocode}
\int_new:N \g_niib_note_int
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\niib_thenote:}
% The counter needs to be expressed as a usable name. A check
% is made so that if there are more than ten notes then the
% number is padded.
%    \begin{macrocode}
\def_new:Npn \niib_thenote: {
  \l_niib_notename_tlp
  \num_compare:nNnT {\g_niib_totalnotes_int} > {\c_nine}{
    \num_compare:nNnF {\g_niib_note_int} > {\c_nine}{
      0
    }
  }
  \int_to_arabic:n \g_niib_note_int
}
%    \end{macrocode}
%\end{macro}
%
%\subsection{User options}
%\begin{macro}{\g_niib_KV_bool_clist}
%\begin{macro}{\niib_KV_bool:nT}
%\begin{macro}{\niib_KV_choice:nnT}
%\begin{macro}{\niib_KV_cs_n:n}
%\begin{macro}{\niib_KV_tlp:n}
%\changes{v0.0}{2008/09/09}{Renamed and recoded from
%  \cs{niib_KV_string:n}}
% To be going on with, the key-defining macros are created
% using \pkg{keyval}.
%    \begin{macrocode}
\RequirePackage{keyval}
\clist_new:N\g_niib_KV_bool_clist
\clist_put_left:Nn\g_niib_KV_bool_clist{true,false}
\def_new:Npn\niib_KV_bool:nT #1#2 {
  \define@key{niib}{#1}[true]{
    \clist_if_in:cnTF{g_niib_KV_bool_clist}{##1}{
      #2
    }{
      \PackageError{xnotes2bib}{
        Unknown~choice~`##1'
      }{
        The~Boolean~option~`#1'~accepts~only~the\MessageBreak
        values~true~and~false
      }
    }
  }
}
\def_new:Npn\niib_KV_choice:nnT #1#2#3 {
  \cs_if_free:cTF{g_niib_KV_#1_clist}{
    \clist_new:c{g_niib_KV_#1_clist}
  }{
    \clist_gclear:c{g_niib_KV_#1_clist}
  }
  \clist_put_left:cn{g_niib_KV_#1_clist}{#2}
  \define@key{niib}{#1}{
    \clist_if_in:cnTF{g_niib_KV_#1_clist}{##1}{
      #3
    }{
      \PackageError{xnotes2bib}{
        Unknown~choice~`##1'
      }{
        The~option~`#1'~accepts~only~the~values:\MessageBreak
        #2
      }
    }
  }
}
\def_new:Npn\niib_KV_cs_n:n #1 {
  \define@key{niib}{#1}{
    \def:cNn{niib_#1:n} 0 {##1}
  }
}
\def_new:Npn\niib_KV_tlp:n #1 {
  \tlp_new:c {l_niib_#1_tlp}
  \define@key{niib}{#1}{
    \tlp_set:cn {l_niib_#1_tlp}{##1}
  }
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\niib_cite:n}
% Options for storing macro names
%    \begin{macrocode}
\niib_KV_cs_n:n{cite}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\l_niib_field_tlp}
%\begin{macro}{\l_niib_key after_tlp}
%\begin{macro}{\l_niib_key before_tlp}
%\begin{macro}{\l_niib_key mixed_tlp}
%\begin{macro}{\l_niib_notename_tlp}
%\begin{macro}{\l_niib_prefix_tlp}
%\begin{macro}{\l_niib_presort after_tlp}
%\begin{macro}{\l_niib_presort before_tlp}
%\begin{macro}{\l_niib_presort mixed_tlp}
%\begin{macro}{\l_niib_record_tlp}
% A series of value-storing are created.
%    \begin{macrocode}
\niib_KV_tlp:n{field}
\niib_KV_tlp:n{key~after}
\niib_KV_tlp:n{key~before}
\niib_KV_tlp:n{key~mixed}
\niib_KV_tlp:n{notename}
\niib_KV_tlp:n{prefix}
\niib_KV_tlp:n{presort~after}
\niib_KV_tlp:n{presort~before}
\niib_KV_tlp:n{presort~mixed}
\niib_KV_tlp:n{record}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\niib_convert_endnotes:}
%\begin{macro}{\niib_convert_footnotes:}
% None of the Boolean keys actually work quite as simply as
% creating a switch.  For footnote and endnote manipulation,
% the keys created here have to be altered later in any case.
%    \begin{macrocode}
\let_new:NN\niib_convert_endnotes:\scan_stop:
\let_new:NN\niib_convert_footnotes:\scan_stop:
\niib_KV_bool:nT{convert~endnotes}{
  \let:Nc\niib_convert_endnotes:{niib_convert_endnotes_#1:}
}
\niib_KV_bool:nT{convert~footnotes}{
  \let:Nc\niib_convert_footnotes:{niib_convert_footnotes_#1:}
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\niib_debug:n}
%\begin{macro}{\niib_debug_true:n}
%\begin{macro}{\niib_debug_false:n}
% The \opt{debug} option is implemented as a choice, to save on
% some complexity.
%    \begin{macrocode}
\niib_KV_bool:nT{debug}{
  \let:Nc\niib_debug:n{niib_debug_#1:n}
}
\def_new:Npn\niib_debug_true:n #1 {
  \PackageInfo{xnotes2bib}{#1}
}
\let_new:NN\niib_debug_false:n\use_none:n
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\l_niib_after_cite_bool}
%\changes{v0.0}{2008/09/09}{Renamed from
%  \cs{l_niib_after_bool}}
%\begin{macro}{\l_niib_before_cite_bool}
%\changes{v0.0}{2008/09/09}{Renamed from
%  \cs{l_niib_before_bool}}
% The \opt{placement} option sets one or other flag if needed.
%    \begin{macrocode}
\bool_new:N\l_niib_after_cite_bool
\bool_new:N\l_niib_before_cite_bool
\niib_KV_choice:nnT{placement}{after,before,mixed}{
  \bool_set_false:N\l_niib_before_cite_bool
  \bool_set_false:N\l_niib_after_cite_bool
  \cs_if_really_exist:cT{l_niib_#1_cite_bool}{
    \bool_set_true:c{l_niib_#1_cite_bool}
  }
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
% All of the default values are set.
%    \begin{macrocode}
\setkeys{niib}{
  cite             = \cite,
  debug            = false,
  field            = note,
  key~after        = zzz,
  key~before       = aaa,
  key~mixed        = {},
  notename         = Bibnote,
  placement        = mixed,
  prefix           = niib-,
  presort~after    = mn,
  presort~before   = ml,
  presort~mixed    = mm,
  record           = Misc}
%    \end{macrocode}
%
%\subsection{User macros}
%\begin{macro}{\bibnote}
%\doarg{label}
%\darg{text}
% The main user macro has to check if the automatic note number
% is needed. If it is, the optional argument is note used to
% \cs{bibnotetext} as this automatically assumes the current
% value of \cs{niib_thenote:}.  The note text must be sorted
% out before the mark is made, as otherwise packages such as
% \pkg{cite} do not work correctly with punctuation. As the
% package is designed for \LaTeX3, it is assumed \eTeX\ is
% available and so \cs{scantokens} can be used on the mandatory
% argument. Hence everything is absorbed here without worrying
% about category codes.
%    \begin{macrocode}
\NewDocumentCommand{\bibnote}{o>{P}m}{
  \IfNoValueTF{#1}{
    \int_gincr:N \g_niib_note_int
    \bibnotetext{#2}
    \bibnotemark[\niib_thenote:]
  }{
    \bibnotetext[#1]{#2}
    \bibnotemark[#1]
  }
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\bibnotemark}
%\doarg{label}
% As with \cs{bibnote}, if no optional argument is given the
% global note tracking counter is incremented before carrying
% out the citation.
%    \begin{macrocode}
\NewDocumentCommand{\bibnotemark}{o}{
  \IfNoValueTF{#1}{
    \int_gincr:N \g_niib_note_int
    \niib_notemark:n{\niib_thenote:}
  }{
    \niib_notemark:n{#1}
  }
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\bibnotetext}
%\doarg{label}
%\darg{text}
% Writing the note text to the database should only occur if
% files are being written.  For the moment, this uses the
% \LaTeXe\ flag (and so the tradition syntax).
%    \begin{macrocode}
\NewDocumentCommand{\bibnotetext}{O{\niib_thenote:}>{P}m}{
  \if@filesw
%    \end{macrocode}
%\begin{macro}{\g_niib_out_stream}
% A new output stream may need to be created, if this is the
% first note to write. The \LaTeXe\ file switch is checked to
% see if this should occur.
%    \begin{macrocode}
    \cs_if_free:NT \g_niib_out_stream {
      \iow_new:N \g_niib_out_stream
      \iow_open:Nn \g_niib_out_stream
        {\l_niib_prefix_tlp\c_job_name_tlp.bib}
      \iow_expanded:Nn \g_niib_out_stream
        {This~is~an~auxiliary~file~used~by~the~`xnotes2bib'~
         package.^^J This~file~may~safely~be~deleted.^^J It~
         will~be~recreated~as~required.^^J}
    }
%    \end{macrocode}
% The writing macro works in expanded mode, as everything
% except the note text does need expansion. Notice that
% \cs{space} has to be used here after the macro names, as |~|
% does not work.
%    \begin{macrocode}
    \niib_debug:n{
      Writing~text:\MessageBreak #2\MessageBreak to~database~
      for~note~`#1'
    }
    \iow_expanded:Nn \g_niib_out_stream {
      @\l_niib_record_tlp{
        #1,^^J
        \space\space \l_niib_field_tlp\space
          =~{\exp_not:n{#2}},^^J
        \space\space \l_niib_keyname_tlp\space
          =~{\niib_key:#1},^^J
        \space\space presort~
          =~{\niib_presort:},^^J
      }^^J
    }
  \fi
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\flushnotestack}
% In order to delay citations to the end of the bibliography
% (and thus force others to the start), note citations are held
% in a clist until being added to the \ext{aux} file using
% \cs{flushnotestack}.  At that point, the stack is cleared so
% that collection can begin again.
%    \begin{macrocode}
\NewDocumentCommand{\flushnotestack}{}{
  \clist_if_empty:NF\g_niib_before_cite_clist{
    \if@filesw
      \iow_expanded:Nn\@auxout{
        \token_to_string:N\niibbeforecite{
          \g_niib_before_cite_clist
        }
      }
    \fi
  }
  \clist_if_empty:NF\g_niib_after_cite_clist{
    \nocite{\g_niib_after_cite_clist}
    \if@filesw
      \iow_expanded:Nn\@auxout{
        \token_to_string:N\niibaftercite{\g_niib_after_cite_clist}
      }
    \fi
  }
  \clist_gclear:N\g_niib_after_cite_clist
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\niibsetup}
%    \begin{macrocode}
\NewDocumentCommand{\niibsetup}{m}{\setkeys{niib}{#1}}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Macros for the \texorpdfstring{\ext{aux}}{.aux}
%  file}
% To allow tracking of the need to re-run \LaTeX, each run
% writes the current out-of-order citations to the \ext{aux}
% file.  This requires two user-space macros, although it is
% not intended the user ever applies them!
%
%\begin{macro}{\g_niib_before_cite_old_clist}
%\changes{v0.0}{2008/09/09}{Renamed from
%  \cs{g_niib_oldbeforecite_clist}}
%\begin{macro}{\g_niib_after_cite_old_clist}
%\changes{v0.0}{2008/09/09}{Renamed from
%  \cs{g_niib_oldaftercite_clist}}
% Two lists are created to track the out-of-order citations
% written in the previous run to the \ext{aux} file.
%    \begin{macrocode}
\clist_new:N\g_niib_before_cite_old_clist
\clist_new:N\g_niib_after_cite_old_clist
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\g_niib_document_tlp}
% A hook into the \cs{document} macro is needed by
% \cs{niibbeforecite}; this needs to exist before \cs{document}
% is executed, hence needing a tlp for this.
%    \begin{macrocode}
\tlp_new:N\g_niib_document_tlp
\tlp_put_right:Nn\document{\g_niib_document_tlp}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\niibbeforecite}
%\begin{macro}{\niibaftercite}
%\darg{citations}
% The macros themselves are quite simple.
% \cs{clist_gset_eq:Nc} is not used as the macros could appear
% more than once in the \ext{aux} file.
%    \begin{macrocode}
\NewDocumentCommand{\niibbeforecite}{m}{
  \clist_gput_right:Nn\g_niib_before_cite_old_clist{#1}
  \tlp_gput_right:Nn\g_niib_document_tlp{\nocite{#1}}
}
\NewDocumentCommand{\niibaftercite}{m}{
  \clist_gput_right:Nn\g_niib_after_cite_old_clist{#1}
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\g_niib_totalnotes_int}
% The total number of notes in the last run is needed
%    \begin{macrocode}
\int_new:N \g_niib_totalnotes_int
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\niibtotalnotes}
%\darg{number}
% The total number of notes created is stored for the next run.
%    \begin{macrocode}
\NewDocumentCommand{\niibtotalnotes}{m}{
  \int_gset:Nn \g_niib_totalnotes_int {#1}
}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Internal macros}
%\begin{macro}{\niib_notemark:n}
%\changes{v0.0}{2008/09/09}{Replaced \cs{clist_put_right:Nx}
%  by \cs{clist_gput_right:Nx}}
% The citation command itself is in an internal macro, as it is
% called from a few places.  Both the \opt{before} and
% \opt{after} modes require some tracking of the citations.
%    \begin{macrocode}
\def_new:Npn \niib_notemark:n #1 {
  \niib_debug:n{Creating~citation~for~note~`#1'}
  \bool_if:NT\l_niib_before_cite_bool{
    \niib_debug:n{Note~`#1'~before~real~citations}
    \clist_gput_right:Nx\g_niib_before_cite_clist{#1}
  }
  \bool_if:NTF\l_niib_after_cite_bool{
    \niib_debug:n{Note~`#1'~after~real~citations}
    \clist_gput_right:Nx\g_niib_after_cite_clist{#1}
    \niib_after_cite:n{#1}
  }{
    \niib_cite:n{#1}
  }
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\g_niib_before_cite_clist}
%\changes{v0.0}{2008/09/09}{Renamed from
%  \cs{g_niib_beforecite_clist}}
%\begin{macro}{\g_niib_after_cite_clist}
%\changes{v0.0}{2008/09/09}{Renamed from
%  \cs{g_niib_aftercite_clist}}
% Out-of-order citations in the current run are followed using
% two clists.
%    \begin{macrocode}
\clist_new:N\g_niib_before_cite_clist
\clist_new:N\g_niib_after_cite_clist
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\niib_key:}
%\begin{macro}{\niib_presort:}
% The key and presort information for the database needs to be
% created dynamically from the various keys.
%    \begin{macrocode}
\def_new:Npn\niib_key: {
  \bool_if:NTF\l_niib_before_cite_bool{
    \tlp_use:c{l_niib_key~before_tlp}
  }{
    \bool_if:NTF\l_niib_after_cite_bool{
      \tlp_use:c{l_niib_key~after_tlp}
    }{
      \tlp_use:c{l_niib_key~mixed_tlp}
    }
  }
}
\def_new:Npn\niib_presort: {
  \bool_if:NTF\l_niib_before_cite_bool{
    \tlp_use:c{l_niib_presort~before_tlp}
  }{
    \bool_if:NTF\l_niib_after_cite_bool{
      \tlp_use:c{l_niib_presort~after_tlp}
    }{
      \tlp_use:c{l_niib_presort~mixed_tlp}
    }
  }
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\niib_convert_endnotes_false:}
%\begin{macro}{\niib_convert_endnotes_true:}
%\begin{macro}{\niib_convert_footnotes_false:}
%\begin{macro}{\niib_convert_footnotes_true:}
% Conversion of footnotes and endnotes takes place using four
% dedicated macros, which can then be called by csname.
%    \begin{macrocode}
\def_new:Npn\niib_convert_endnotes_false: {
  \niib_debug:n{Restoring~normal~endnotes}
  \let:NN\endnote\niib_saved_endnote:
  \let:NN\endnotemark\niib_saved_endnotemark:
  \let:NN\endnotetext\niib_saved_endnotetext:
}
\def_new:Npn\niib_convert_endnotes_true: {
  \niib_debug:n{Converting~endnotes~to~bibnotes}
  \let:NN\endnote\bibnote
  \let:NN\endnotemark\bibnotemark
  \let:NN\endnotetext\bibnotetext
}
\def_new:Npn\niib_convert_footnotes_false: {
  \niib_debug:n{Restoring~normal~footnotes}
  \let:NN\footnote\niib_saved_footnote:
  \let:NN\footnotemark\niib_saved_footnotemark:
  \let:NN\footnotetext\niib_saved_footnotetext:
}
\def_new:Npn\niib_convert_footnotes_true: {
  \niib_debug:n{Converting~footnotes~to~bibnotes}
  \let:NN\footnote\bibnote
  \let:NN\footnotemark\bibnotemark
  \let:NN\footnotetext\bibnotetext
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\thanks}
% The \cs{thanks} macro has to be replaced by one that uses the
% original \cs{footnotemark} and \cs{footnotetext}.
%    \begin{macrocode}
\def:Npn\thanks #1 {
  \niib_saved_footnotemark:
  \protected@xdef\@thanks{\@thanks
  \protect\niib_saved_footnotetext:[\the\c@footnote]{#1}}
}
%    \end{macrocode}
%\end{macro}
%
%\subsection{Finalising}
%\begin{macro}{\l_niib_keyname_tlp}
%\changes{v0.0}{2008/09/09}{Renamed from \cs{niib_keyname:}}
% The name used for the \texttt{key} field is stored a little
% later.  The store is set up here.
%    \begin{macrocode}
\tlp_new:N \l_niib_keyname_tlp
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\niib_biliography:n}
%\begin{macro}{\printbibnotes}
%\begin{macro}{\niib_after_cite:n}
%\changes{v0.0}{2008/09/09}{Renamed from
%  \cs{niib_aftercite:n}}
% Various macro definitions depend on the loading of other
% packages.  When \pkg{biblatex} is in use, life is rather
% different to when the standard citation method is
% operational.  The \cs{niib_key:} macro is used to control
% sorting: \pkg{biblatex} uses a different macro name for this
% function than normal \LaTeX\ does.  The \cs{printbibnotes}
% user macro always prints the bibliography, using only the
% notes database if possible. The \cs{niib_after_cite:n} macro
% has to keep a track of the appropriate bibnotes and also
% fiddle with the \cs{@filesw} switch to prevent writing at the
% wrong time.
%    \begin{macrocode}
\AtBeginDocument{
  \PackageInfo{xnotes2bib}{
    Beginning~\token_to_string:N\AtBeginDocument\space tasks
  }
  \@ifpackageloaded{biblatex}{
    \tlp_set:Nn \l_niib_keyname_tlp {keysort}
    \gappto\blx@bibfiles{,\niib_prefix\c_job_name_tlp}
    \let_new:NN\printbibnotes\printbibliography
    \def_new:Npn\niib_after_cite:n #1 {
      \AtNextCite{\@fileswfalse}
      \niib_cite:n{#1}
    }
  }{
    \tlp_gset:Nn \l_niib_keyname_tlp {key}
    \let_new:NN\niib_biliography:n\bibliography
    \def:Npn\bibliography #1 {
      \int_compare:nNnTF{\g_niib_note_int}={\c_zero}{
        \niib_biliography:n{#1}
      }{
        \niib_biliography:n{
          #1,\l_niib_prefix_tlp\c_job_name_tlp
        }
      }
    }
    \NewDocumentCommand{\printbibnotes}{}{
      \niib_biliography:n{\l_niib_prefix_tlp\c_job_name_tlp}
    }
%    \end{macrocode}
%\begin{macro}{\ifniib@filesw}
% There is a need to back-up the \cs{@filesw} switch.  As this
% is a \LaTeXe\ switch, the name and creation method used here
% are not \LaTeX3-based.
%    \begin{macrocode}
    \newif\ifniib@filesw
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\@restore@auxhandle}
%\changes{v0.0}{2008/09/09}{Moved patch code to after
%  \cs{AtBeginDocument}}
%\begin{macro}{\niib_auxhandle:}
% If \pkg{cite} is loaded, there is further trouble.
%    \begin{macrocode}
    \@ifpackageloaded{cite}{
      \def_new:Npn\niib_after_cite:n #1 {
         \let:NN \ifniib@filesw \if@filesw
         \@fileswfalse
         \def:Npn \niib_auxhandle: {
           \let:NN \if@filesw \ifniib@filesw
           \let:NN \niib_auxhandle: \scan_stop:
         }
         \niib_cite:n{#1}
      }
      \tlp_gput_right:Nn \g_niib_document_tlp {
        \cs_if_exist:NTF\@restore@auxhandle{
          \tlp_put_right:Nn\@restore@auxhandle{\niib_auxhandle:}
        }{
          \def_new:Npn\@restore@auxhandle {\niib_auxhandle:}
        }
      }
      \let_new:NN\niib_auxhandle:\scan_stop:
%    \end{macrocode}
%\end{macro}
%\end{macro}
% Life is slightly easier without \pkg{cite}.
%    \begin{macrocode}
    }{
      \def_new:Npn\niib_after_cite:n #1 {
         \let\ifniib@filesw\if@filesw
         \@fileswfalse
         \niib_cite:n{#1}
         \let\if@filesw\ifniib@filesw
      }
    }
  }
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\niib_saved_footnote:}
%\begin{macro}{\niib_saved_footnotemark:}
%\begin{macro}{\niib_saved_footnotetext:}
%\begin{macro}{\niib_saved_endnote:}
%\begin{macro}{\niib_saved_endnotemark:}
%\begin{macro}{\niib_saved_endnotetext:}
% The second thing to worry about is footnotes and endnotes.
% The definitions are saved here so that any other packages can
% have made their changes. No argument specifiers are given as
% these are saving user space macros.
%    \begin{macrocode}
  \let_new:NN\niib_saved_footnote:\footnote
  \let_new:NN\niib_saved_footnotemark:\footnotemark
  \let_new:NN\niib_saved_footnotetext:\footnotetext
  \let_new:NN\niib_saved_endnote:\endnote
  \let_new:NN\niib_saved_endnotemark:\endnotemark
  \let_new:NN\niib_saved_endnotetext:\endnotetext
%    \end{macrocode}
% Any conversion can now be applied, before the \opt{convert
% footnotes} and \opt{convert endnotes} options are redefined
% to work immediately.
%    \begin{macrocode}
  \niib_convert_endnotes:
  \niib_convert_footnotes:
  \niib_KV_bool:nT{convert~endnotes}{
    \use:c{niib_convert_endnotes_#1:}
  }
  \niib_KV_bool:nT{convert~footnotes}{
    \use:c{niib_convert_footnotes_#1:}
  }
  \PackageInfo{xnotes2bib}{
    Finished~\token_to_string:N\AtBeginDocument\space tasks
  }
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\niib_rerun_check:n}
% If either of the out-of-order options are in use, then a
% comparison of the citations used with those used in the last
% run is needed. There are a couple of easy checks to make: if
% only one list is empty, there must be changes.  If both lists
% are occupied, a check of every item must be made.
%    \begin{macrocode}
\def_new:Npn\niib_rerun_check:n #1 {
  \clist_if_empty:cTF {g_niib_old#1cite_clist}{
    \clist_if_empty:cF {g_niib_#1cite_clist}{
      \niib_rerun_log:
    }
  }{
    \clist_if_empty:cTF {g_niib_#1cite_clist}{
      \niib_rerun_log:
%    \end{macrocode}
% If both lists are occupied by data, the total contents of
% each one have to be compared with the other.  This needs to
% be done both ways round.
%    \begin{macrocode}
    }{
      \bool_set_false:N\l_tmpa_bool
      \def:Npn\niib_rerun_test:n ##1 {
        \clist_if_in:cnTF{g_niib_old#1cite_clist}{##1}{
          \bool_set_true:N\l_tmpa_bool
          \clist_map_break:w
        }{
        }
      }
      \clist_map_function:cN{g_niib_#1cite_clist}
        \niib_rerun_test:n
      \def:Npn\niib_rerun_test:n ##1 {
        \clist_if_in:cnTF{g_niib_#1cite_clist}{##1}{
          \bool_set_true:N\l_tmpa_bool
          \clist_map_break:w
        }{
        }
      }
      \clist_map_function:cN{g_niib_old#1cite_clist}
        \niib_rerun_test:n
      \bool_if:NF\l_tmpa_bool{
        \niib_rerun_log:
      }
    }
  }
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\niib_rerun_test:n}
% This is redefined in the above, but is made available here.
%    \begin{macrocode}
\let_new:NN \niib_rerun_test:n \use_none_i:n
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\niib_rerun_log:}
%\changes{v0.0}{2008/09/09}{Maximum of one message per run}
% The information message needs to be available in a few
% different places.
%    \begin{macrocode}
\def_new:Npn\niib_rerun_log: {
  \PackageInfo{xnotes2bib}{
    To~get~notes~in~the~correct~order,~please\MessageBreak
    rerun~LaTeX,~(re)run~BibTeX~on~the~file~\c_job_name_tlp.aux
    \MessageBreak and~rerun~LaTeX~again~afterwards
  }
  \io_put_log:x {
    REQ:3:latex:REQ\iow_newline:
    REQ:2:bibtex:REQ\iow_newline:
    REQ:1:latex:REQ\iow_newline:
  }
  \let\niib_rerun_log:\scan_stop:
}
%    \end{macrocode}
%\end{macro}
%
% At the end of the document, the package should tidy up.
%    \begin{macrocode}
\AtEndDocument{
  \niib_rerun_check:n{before}
  \niib_rerun_check:n{after}
  \flushnotestack
  \iow_expanded:Nn\@auxout{
    \token_to_string:N\niibtotalnotes{
      \int_to_arabic:n \g_niib_note_int
    }
  }
  \num_compare:nNnF{\g_niib_totalnotes_int}={\g_niib_note_int}{
    \niib_rerun_log:
  }
  \cs_if_free:NF \g_niib_out_stream{
    \iow_close:N \g_niib_out_stream
  }
}
%    \end{macrocode}
%
%\iffalse
%</package>
%\fi
%
%\Finale
%
%\iffalse
%<*jawl3doc>
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{l3names}
\ProvidesExplPackage
  {jawl3doc}{2008/09/09}{0.0}
  {Additions to l3doc (JAW)}
\DeleteShortVerb\|
\DeleteShortVerb\"
\RequirePackage{expl3,xparse}
\MakeShortVerb\|
\IfFileExists{\c_job_name_tlp.sty}
  {\RequirePackage{\c_job_name_tlp}}{}
\RequirePackage{helvet,mathpazo}
\RequirePackage[final]{listings}

\NewDocumentCommand{\pkg}{m}{\textsf{#1}}
\NewDocumentCommand{\currpkg}{}{\pkg{\c_job_name_tlp}}
\NewDocumentCommand{\latin}{m}{\emph{#1}}
\NewDocumentCommand{\etc}{}{\latin{etc}}
\NewDocumentCommand{\ext}{m}{\file{.#1}}
\NewDocumentCommand{\opt}{m}{\texttt{#1}}

\int_new:N \g_jawldoc_arg_int
\def_new:Npn \jawldoc_thearg: {
  \int_to_arabic:n \g_jawldoc_arg_int
}
\NewDocumentCommand{\darg}{m}{
  \int_gincr:N \g_jawldoc_arg_int
  {\ttfamily\char`\#\jawldoc_thearg:~:~}#1
  \par\noindent\ignorespaces
}
\NewDocumentCommand{\doarg}{m}{
  \int_gincr:N \g_jawldoc_arg_int
  {\ttfamily\makebox[0pt][r]{[}
  \char`\#\jawldoc_thearg:]:~}#1
  \par\noindent\ignorespaces
}
\tlp_put_right:Nn\endmacro{\int_gzero:N\g_jawldoc_arg_int}
\NewDocumentCommand{\DescribeOption}{}{
  \leavevmode \@bsphack
  \group_begin:
  \MakePrivateLetters
  \Describe_Option:
}
\def_new:Npn \Describe_Option: #1 {
  \group_end:
  \marginpar{
    \raggedleft\PrintDescribeEnv{#1}
  }
  \SpecialOptionIndex{#1}
  \@esphack\ignorespaces
}
\NewDocumentCommand{\SpecialOptionIndex}{m}{
  \@bsphack
  \index{
    #1\actualchar{\protect\ttfamily#1}
    \space(option)\encapchar usage
  }
  \index{
    options:\levelchar#1\actualchar{\protect\ttfamily#1}
    \encapchar usage
  }
  \@esphack
}
%</jawl3doc>
%\fi