summaryrefslogtreecommitdiff
path: root/macros/latex-dev/required/latex-lab/latex-lab-bib.dtx
blob: e8ee192a7b21b3b03b1f921d4de59fd2f0d41400 (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
% \iffalse meta-comment
%
%% File: latex-lab-bib.dtx (C) Copyright 2023 LaTeX Project
%
% 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
%
%    https://www.latex-project.org/lppl.txt
%
%
% The development version of the bundle can be found below
%
%    https://github.com/latex3/latex2e/required/latex-lab
%
% for those people who are interested or want to report an issue.
%
\def\ltlabbibdate{2024-02-12}
\def\ltlabbibversion{0.81b}
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-bib.dtx}
\end{document}
%</driver>
%
% \fi
%
% \title{The \textsf{latex-lab-bib} package\\
% Changes and additions to the kernel related to tagging and links in citations and 
% bibliography entries}
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
% \date{v\ltlabbibversion\ \ltlabbibdate}
%
% \maketitle
%
% \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
% \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
% \newcommand{\docclass}{document class \marginpar{\raggedright document class
% customizations}}
%
% \providecommand\hook[1]{\texttt{#1}}
%
% \begin{abstract}
% \end{abstract}
%
% \section{Introduction}
%
% The followings contains small changes to improve tagging of
% bibliography entries and citations. 
% 
% The tagging of the standard bibliography is actually quite straightforward:
% A bibliography is typically a list with a heading and the code which tags sectioning
% commands and lists handles that. 
% 
% There are here only two problems: 
% 
% \begin{itemize}
% \item The structure number of the \texttt{LI} element
% created by a \cs{bibitem} must be recorded somehow to allow to reference it in 
% a \cs{cite}. 
% \item \pkg{hyperref} redefines the item command and so breaks the list structure
% see \url{https://github.com/latex3/latex2e/discussions/1010#discussioncomment-5565418}
% \end{itemize}
% 
% Both problems are rather easy to resolve, but it must be checked if other packages 
% interfere again by redefining the commands.
% 
% More difficult is the tagging of citation commands. Citations should be inside 
% a Reference structure and contain a /Ref entry pointing to the relevant 
% item in the bibliography. For simple citations like 
% \enquote{[1]} or \enquote{Doody (2023)} this is easy, but it is not obvious how
% to handle combined citations like \enquote{Doody (2003,2018)} (or even compressed citations
% like \enquote{[1-3]}). The current implementation follows the links: whatever hyperref would
% link is set as the reference.
% 
% There exist various packages which over the years tried to improve and extend
% the bibliography commands. We discuss here three: natbib, chapterbib and biblatex.
% 
% \begin{description}
% \item[natbib] It is rather easy to support natbib: it has hooks for links and the tagging code
% can follow. Only a bit coordination with hyperref is needed to avoid that hyperref
% remove the tagging code again.
% 
% \item[chapterbib] In standard LaTeX every bib entry has an unique label which points to the 
% (mandatory and unique) bibliography and the target created by hyperref 
% has the simple form \texttt{cite.}\meta{key}.
% If a package that support multiple bibliographies is used (e.g. chapterbib)
% this is no longer works: a bib entry \texttt{doody} 
% can in one chapter get the label \enquote{[1]}
% and in the other \enquote{[5]} or even \enquote{Doo19} and naturally links
% should jump to the relevant chapter bibliography. chapterbib solves this
% by creating bib keys with a suffix: when reading the \texttt{.aux} files it will
% create the keys \texttt{doody@-1} and \texttt{doody@-2} where the number is related
% to the chapter/include, and in the document and in the document \verb+\cite{doody}+
% will look for \texttt{doody@-1} and \texttt{doody@-2} depending on the number of the
% current include. For some unknown reason chapterbib uses two commands
% to handle the suffix: the command \cs{@extra@binfo} is written to the aux-files 
% and used when processing the \cs{bibcite} commands, 
% but in the document \cs{@extra@b@citeb} is used. Supporting this is 
% straightforward: one only has to take care that the tagging code uses
% \cs{@extra@b@citeb} in the relevant places too.
% 
% \item[biblatex] biblatex supports multiple bibliographies out-of-the-box. 
% It numbers the link target by refsection and uses then the name
% \verb+\the\c@refsection @+\meta{key}.
% 
% Printing a bibliography is not required, in this case you get an engine warning
% and links jump to the begin of the document:
% \begin{verbatim}
% name{cite.0@doody} has been referenced but does not exist
% \end{verbatim}
% 
% Bibliographies can be printed more than once by refsection. To avoid duplicated
% target, biblatex stores the names of the targets in a list and if later
% it detects that a target name has already been used in a
% bibliography no new target is created for this item. This means a citation
% will normally jump to the first bibliography which shows the entry.
% 
% The tagging code has to mimic this code. This means that it can't label every item,
% but has to test if this anchor is already known.
% \end{description}
% 
% \section{Provided or redefined commands}
%
% \begin{function}{\@extra@binfo,\@extra@b@citeb}
% 
% These are taken from hyperref, they are for chapterbib compability (and also
% signal to chapterbib not to change the citation commands)
% \end{function}
% 
% \begin{function}{\@bibitem,\@lbibitem}
% The internal item commands. 
% \end{function}
% 
% \section{Implementation}
%    \begin{macrocode}
%<*package>
%<@@=tag>
%    \end{macrocode}
%    \begin{macrocode}
\ProvidesExplPackage {latex-lab-testphase-bib} {\ltlabbibdate} {\ltlabbibversion}
  {Code related to the tagging of bibliography and cite command}
%    \end{macrocode}
% We need at least the block tagging code.
%    \begin{macrocode}
\RequirePackage{latex-lab-testphase-block}  
%    \end{macrocode}
%
% At first we suppress the patches from hyperref. This will only work with the next
% hyperref!
%    \begin{macrocode}
\def\hyper@nopatch@bib{}
%    \end{macrocode}

% \begin{macro}{\@extra@binfo,\@extra@b@citeb}
% These are taken from hyperref, they are for chapterbib compability (and also
% signal to chapterbib not to change the citation commands)
%    \begin{macrocode}
\providecommand*\@extra@binfo{}%
\providecommand\@extra@b@citeb{}
%    \end{macrocode}
% \end{macro}
% 
% \begin{variable}{\l_@@_bib_citekey_tl}
%  We can't pass the cite key everywhere as argument so we store it:
%    \begin{macrocode}
\tl_new:N\l_@@_bib_citekey_tl
%    \end{macrocode}
% \end{variable}
% 
%\subsection{Handling the bibliography}
% \begin{macro}{\@lbibitem}
% The item command if an optional argument is used. 
% 
% We only prepend some code. 
%    \begin{macrocode}
\AddToHookWithArguments{cmd/@lbibitem/before}
 {
%    \end{macrocode} 
% we store the target name for the label.
%    \begin{macrocode} 
   \tl_set:Nn\l_@@_bib_citekey_tl{#2}
 }  
%    \end{macrocode}
% The target is added at the begin of the paragraph. We give that a label as
% it perhaps need to be removed by packages
%    \begin{macrocode}  
\AddToHookWithArguments{cmd/@lbibitem/before}[latex-lab-testphase-bib/target]
 { 
   \AddToHookNext{para/begin}
     {
       \makebox[0pt][r]{\MakeLinkTarget*{cite.#2\@extra@b@citeb}\hspace{\leftmargin}}
     }
 }
%    \end{macrocode} 
% we make a copy to be able to reinstate the definition. This is e.g.
% currently needed with hyperref.
%    \begin{macrocode}
\let\@kernel@copy@lbibitem\@lbibitem       
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@bibitem}
% Similar for \cs{@bibitem}. 
% TODO: If hyperref is loaded we will get a second target from the refstepcounter,
% but this is ignored for now. 
%    \begin{macrocode}
\AddToHookWithArguments{cmd/@bibitem/before}
 {
%    \end{macrocode}
% we store the target name for the label.
%    \begin{macrocode} 
   \tl_set:Nn\l_@@_bib_citekey_tl{#1}
 }  
%    \end{macrocode}
%
% The target is added at the begin of the paragraph. We give that a label as
% it perhaps need to be removed by packages
%    \begin{macrocode}   
\AddToHookWithArguments{cmd/@bibitem/before}[latex-lab-testphase-bib/target]
 {
   \AddToHookNext{para/begin}
     {
       \makebox[0pt][r]{\MakeLinkTarget*{cite.#1\@extra@b@citeb}\hspace{\leftmargin}}
     }
 }
%    \end{macrocode}  
%    \begin{macrocode}
\let\@kernel@copy@bibitem\@bibitem           
%    \end{macrocode}
% \end{macro}
% 
% TODO The LI-structure should set a label, we redefine the internal command locally for 
% now, but perhaps this need a recipe? 
% 
%    \begin{macrocode}
\AddToHook{env/thebibliography/begin}
  {
    \cs_set:Npn \__block_list_item_begin:
     { 
       \tag_struct_begin:n
         {
          tag=\LItag,
          label= cite.\l_@@_bib_citekey_tl\@extra@b@citeb
         }
     }
  }
%    \end{macrocode}
%
% \subsection{Handling citation commands}
% We redefine similar to hyperref the \cs{bibcite} command to inject link and
% structure. Even if it looks a bit odd it is now used for many years and so 
% hopefully compatible with various packages. But differently to hyperref we use
% the new hooks with arguments.
% TODO: consider hook name. 
%    \begin{macrocode}
\NewMirroredHookPairWithArguments{bibcite/before}{bibcite/after}{2}  
\def\bibcite#1#2{%
   \@newl@bel{b}{#1\@extra@binfo}{%
      \UseHookWithArguments{bibcite/before}{2}{#1}{#2}
       #2
      \UseHookWithArguments{bibcite/after}{2}{#1}{#2} 
       }%
     }% 
\let\@kernel@copy@bibcite\bibcite         
%    \end{macrocode}
% Now we add the tagging structure.
% TODO: with the next tagpdf version it should no longer be 
% needed to exand the ref key.
%    \begin{macrocode}
\AddToHookWithArguments{bibcite/before}
  {
    \tag_mc_end_push:
    \exp_args:Ne\tagstructbegin{tag=Reference,ref=cite.#1\@extra@b@citeb}
    \tagmcbegin{}
  }
\AddToHookWithArguments{bibcite/after}[tag]
  {
    \tag_mc_end:
    \tagstructend
    \tag_mc_begin_pop:n{}
  }
%    \end{macrocode}
% At last the code for hyperref, the link will be inside the reference, but
% this can be changed with a rule.
%    \begin{macrocode}
\AddToHook{package/hyperref/after}
  {
    \AddToHookWithArguments{bibcite/before}
     {
       \hyper@linkstart{cite}{cite.#1\@extra@b@citeb}
     }
    \AddToHookWithArguments{bibcite/after}{\hyper@linkend}
  }
%    \end{macrocode}
%
% \subsection{Natbib and biblatex support}
%  When hyperref is loaded, both natbib and biblatex use \cs{hyper@natlinkstart}
%  and \cs{hyper@natlinkend} to handle the links. We can use the generic hooks to 
%  add the tagging code (and the link code from hyperref).
%  We need in part different code for both systems: 
%  with biblatex we have to take care that only the first
%  structure sets a label, and if 
%  hyperref is not loaded (or deactived) we will need additional code
%  but this currently doesn't exist.
%  We assume that no document loads both package -- that will probably break.
%    \begin{macrocode}
\newcommand\hyper@natlinkstart[1]{}
\newcommand\hyper@natlinkend{}
%    \end{macrocode}
% With natbib we need to change the hooks to avoid duplicated target as it sets
% the anchor too. We can not simply empty \cs{hyper@natanchorstart} as that is 
% used by biblatex.
%    \begin{macrocode}
\AddToHook{package/natbib/after}
 {
   \RemoveFromHook{cmd/@bibitem/before} [latex-lab-testphase-bib/target]
   \RemoveFromHook{cmd/@lbibitem/before}[latex-lab-testphase-bib/target]
 }
%    \end{macrocode}
% This can be shared by both packages: it will work with natbib with and without hyperref.
% With biblatex it will work without hyperref as long as \cs{@extra@b@citeb} is empty
%    \begin{macrocode}
\AddToHookWithArguments{cmd/hyper@natlinkstart/before}
 {
   \leavevmode
   \tag_mc_end_push:
   \exp_args:Ne\tag_struct_begin:n{tag=Reference,ref=cite.#1\@extra@b@citeb}
   \tag_mc_begin:n{}
 }
\AddToHook{cmd/hyper@natlinkend/after}
 {
  \tag_mc_end:
  \tag_struct_end:
  \tag_mc_begin_pop:n{}
 }
      
%    \end{macrocode}
% if hyperref is loaded we have to revert its definition of the natbib hooks
% and add its code through the generic hooks. 
% TODO: allow to suppress the natbib code in hyperref. 
% 
%    \begin{macrocode}
\AddToHook{package/hyperref/after}
  {
    \renewcommand\hyper@natlinkstart[1]{}
    \renewcommand\hyper@natlinkend{}
    \AddToHookWithArguments{cmd/hyper@natlinkstart/before}
      {
        \Hy@backout{#1}%
%    \end{macrocode}
%  natbib passes the \cs{@extra@b@citeb} in the argument, and biblatex
%  the refsection, so we only need to add the prefix \texttt{cite.}.
%    \begin{macrocode}
          \hyper@linkstart{cite}{cite.#1}%
          \def\hyper@nat@current{#1}
      } 
    \AddToHook{cmd/hyper@natlinkend/after}
      {  
        \hyper@linkend
      }
  }
    
%    \end{macrocode}
% and now special biblatex code. The list item code has to test if the 
% anchor is already known:
%    \begin{macrocode}
\AddToHook{cmd/blx@bibinit/after}
  {
    \cs_set:Npn \__block_list_item_begin:
     { 
       \xifinlist{\the\c@refsection @\abx@field@entrykey}{\blx@anchors}
        {
          \tag_struct_begin:n
           {
            tag=\LItag,
           }
        }   
        {
          \tag_struct_begin:n
           {
            tag=\LItag,
            label= cite.\the\c@refsection @\abx@field@entrykey
           }
        }   
     }
  }
%    \end{macrocode}
% biblatex without hyperref is currently not supported but we at least avoid that it
% errors:
%    \begin{macrocode}
\AddToHook{package/biblatex/after}
 {
  \appto\blx@mknohyperref
   {
    \let\blx@anchors\@empty
    \protected\def\blx@anchor{%
       \xifinlist{\the\c@refsection @\abx@field@entrykey}{\blx@anchors}
         {}
         {\listxadd\blx@anchors{\the\c@refsection @\abx@field@entrykey}}}%
   }
 }  
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%    \begin{macrocode}
%<*latex-lab>
\ProvidesFile{bib-latex-lab-testphase.ltx}
        [\ltlabbibdate\space v\ltlabbibversion\space 
         latex-lab wrapper bib]

\RequirePackage{latex-lab-testphase-bib}

%</latex-lab>
%    \end{macrocode}