| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: latex-lab-bib.dtx (C) Copyright 2023 LaTeX Project |
| 4 |
% |
| 5 |
% It may be distributed and/or modified under the conditions of the |
| 6 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 7 |
% license or (at your option) any later version. The latest version |
| 8 |
% of this license is in the file |
| 9 |
% |
| 10 |
% https://www.latex-project.org/lppl.txt |
| 11 |
% |
| 12 |
% |
| 13 |
% The development version of the bundle can be found below |
| 14 |
% |
| 15 |
% https://github.com/latex3/latex2e/required/latex-lab |
| 16 |
% |
| 17 |
% for those people who are interested or want to report an issue. |
| 18 |
% |
| 19 |
\def\ltlabbibdate{2025-05-09} |
| 20 |
\def\ltlabbibversion{0.81c} |
| 21 |
%<*driver> |
| 22 |
\DocumentMetadata{tagging=on,pdfstandard=ua-2} |
| 23 |
\documentclass{l3doc} |
| 24 |
\usepackage{latex-lab-testphase-l3doc} |
| 25 |
\EnableCrossrefs |
| 26 |
\CodelineIndex |
| 27 |
\begin{document} |
| 28 |
\DocInput{latex-lab-bib.dtx} |
| 29 |
\end{document} |
| 30 |
%</driver> |
| 31 |
% |
| 32 |
% \fi |
| 33 |
% |
| 34 |
% \title{The \textsf{latex-lab-bib} package\\ |
| 35 |
% Changes and additions to the kernel related to tagging and links in citations and |
| 36 |
% bibliography entries} |
| 37 |
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}} |
| 38 |
% \date{v\ltlabbibversion\ \ltlabbibdate} |
| 39 |
% |
| 40 |
% \maketitle |
| 41 |
% |
| 42 |
% \newcommand{\xt}[1]{\textsl{\textsf{#1}}} |
| 43 |
% \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}} |
| 44 |
% \newcommand{\docclass}{document class \marginpar{\raggedright document class |
| 45 |
% customizations}} |
| 46 |
% |
| 47 |
% \providecommand\hook[1]{\texttt{#1}} |
| 48 |
% |
| 49 |
% \begin{abstract} |
| 50 |
% \end{abstract} |
| 51 |
% |
| 52 |
% \section{Introduction} |
| 53 |
% |
| 54 |
% The followings contains small changes to improve tagging of |
| 55 |
% bibliography entries and citations. |
| 56 |
% |
| 57 |
% The tagging of the standard bibliography is actually quite straightforward: |
| 58 |
% A bibliography is typically a list with a heading and the code which tags sectioning |
| 59 |
% commands and lists handles that. |
| 60 |
% |
| 61 |
% There are here only two problems: |
| 62 |
% |
| 63 |
% \begin{itemize} |
| 64 |
% \item The structure number of the \texttt{LI} element |
| 65 |
% created by a \cs{bibitem} must be recorded somehow to allow to reference it in |
| 66 |
% a \cs{cite}. |
| 67 |
% \item \pkg{hyperref} redefines the item command and so breaks the list structure |
| 68 |
% see \url{https://github.com/latex3/latex2e/discussions/1010#discussioncomment-5565418} |
| 69 |
% \end{itemize} |
| 70 |
% |
| 71 |
% Both problems are rather easy to resolve, but it must be checked if other packages |
| 72 |
% interfere again by redefining the commands. |
| 73 |
% |
| 74 |
% More difficult is the tagging of citation commands. Citations should be inside |
| 75 |
% a Reference structure and contain a /Ref entry pointing to the relevant |
| 76 |
% item in the bibliography. For simple citations like |
| 77 |
% \enquote{[1]} or \enquote{Doody (2023)} this is easy, but it is not obvious how |
| 78 |
% to handle combined citations like \enquote{Doody (2003,2018)} (or even compressed citations |
| 79 |
% like \enquote{[1-3]}). The current implementation follows the links: whatever hyperref would |
| 80 |
% link is set as the reference. |
| 81 |
% |
| 82 |
% There exist various packages which over the years tried to improve and extend |
| 83 |
% the bibliography commands. We discuss here three: natbib, chapterbib and biblatex. |
| 84 |
% |
| 85 |
% \begin{description} |
| 86 |
% \item[natbib] It is rather easy to support natbib: it has hooks for links and the tagging code |
| 87 |
% can follow. Only a bit coordination with hyperref is needed to avoid that hyperref |
| 88 |
% remove the tagging code again. |
| 89 |
% |
| 90 |
% \item[chapterbib] In standard LaTeX every bib entry has an unique label which points to the |
| 91 |
% (mandatory and unique) bibliography and the target created by hyperref |
| 92 |
% has the simple form \texttt{cite.}\meta{key}. |
| 93 |
% If a package that support multiple bibliographies is used (e.g. chapterbib) |
| 94 |
% this is no longer works: a bib entry \texttt{doody} |
| 95 |
% can in one chapter get the label \enquote{[1]} |
| 96 |
% and in the other \enquote{[5]} or even \enquote{Doo19} and naturally links |
| 97 |
% should jump to the relevant chapter bibliography. chapterbib solves this |
| 98 |
% by creating bib keys with a suffix: when reading the \texttt{.aux} files it will |
| 99 |
% create the keys \texttt{doody@-1} and \texttt{doody@-2} where the number is related |
| 100 |
% to the chapter/include, and in the document and in the document \verb+\cite{doody}+ |
| 101 |
% will look for \texttt{doody@-1} and \texttt{doody@-2} depending on the number of the |
| 102 |
% current include. For some unknown reason chapterbib uses two commands |
| 103 |
% to handle the suffix: the command \cs{@extra@binfo} is written to the aux-files |
| 104 |
% and used when processing the \cs{bibcite} commands, |
| 105 |
% but in the document \cs{@extra@b@citeb} is used. Supporting this is |
| 106 |
% straightforward: one only has to take care that the tagging code uses |
| 107 |
% \cs{@extra@b@citeb} in the relevant places too. |
| 108 |
% |
| 109 |
% \item[biblatex] biblatex supports multiple bibliographies out-of-the-box. |
| 110 |
% It numbers the link target by refsection and uses then the name |
| 111 |
% \verb+\the\c@refsection @+\meta{key}. |
| 112 |
% |
| 113 |
% Printing a bibliography is not required, in this case you get an engine warning |
| 114 |
% and links jump to the begin of the document: |
| 115 |
% \begin{verbatim} |
| 116 |
% name{cite.0@doody} has been referenced but does not exist |
| 117 |
% \end{verbatim} |
| 118 |
% |
| 119 |
% Bibliographies can be printed more than once by refsection. To avoid duplicated |
| 120 |
% target, biblatex stores the names of the targets in a list and if later |
| 121 |
% it detects that a target name has already been used in a |
| 122 |
% bibliography no new target is created for this item. This means a citation |
| 123 |
% will normally jump to the first bibliography which shows the entry. |
| 124 |
% |
| 125 |
% The tagging code has to mimic this code. This means that it can't label every item, |
| 126 |
% but has to test if this anchor is already known. |
| 127 |
% \end{description} |
| 128 |
% |
| 129 |
% \section{Provided or redefined commands} |
| 130 |
% |
| 131 |
% \begin{function}{\@extra@binfo,\@extra@b@citeb} |
| 132 |
% |
| 133 |
% These are taken from hyperref, they are for chapterbib compatibility (and also |
| 134 |
% signal to chapterbib not to change the citation commands) |
| 135 |
% \end{function} |
| 136 |
% |
| 137 |
% \begin{function}{\@bibitem,\@lbibitem} |
| 138 |
% The internal item commands. |
| 139 |
% \end{function} |
| 140 |
% |
| 141 |
% \section{Implementation} |
| 142 |
% \begin{macrocode} |
| 143 |
%<*package> |
| 144 |
%<@@=tag> |
| 145 |
% \end{macrocode} |
| 146 |
% \begin{macrocode} |
| 147 |
\ProvidesExplPackage {latex-lab-testphase-bib} {\ltlabbibdate} {\ltlabbibversion} |
| 148 |
{Code related to the tagging of bibliography and cite command} |
| 149 |
% \end{macrocode} |
| 150 |
% We need at least the block tagging code. |
| 151 |
% \begin{macrocode} |
| 152 |
\RequirePackage{latex-lab-testphase-block} |
| 153 |
% \end{macrocode} |
| 154 |
% |
| 155 |
% At first we suppress the patches from hyperref. This will only work with the next |
| 156 |
% hyperref! |
| 157 |
% \begin{macrocode} |
| 158 |
\def\hyper@nopatch@bib{} |
| 159 |
% \end{macrocode} |
| 160 |
|
| 161 |
% \begin{macro}{\@extra@binfo,\@extra@b@citeb} |
| 162 |
% These are taken from hyperref, they are for chapterbib compatibility (and also |
| 163 |
% signal to chapterbib not to change the citation commands) |
| 164 |
% \begin{macrocode} |
| 165 |
\providecommand*\@extra@binfo{}% |
| 166 |
\providecommand\@extra@b@citeb{} |
| 167 |
% \end{macrocode} |
| 168 |
% \end{macro} |
| 169 |
% |
| 170 |
% \begin{variable}{\l_@@_bib_citekey_tl} |
| 171 |
% We can't pass the cite key everywhere as argument so we store it: |
| 172 |
% \begin{macrocode} |
| 173 |
\tl_new:N\l_@@_bib_citekey_tl |
| 174 |
% \end{macrocode} |
| 175 |
% \end{variable} |
| 176 |
% |
| 177 |
%\subsection{Handling the bibliography} |
| 178 |
% \begin{macro}{\@lbibitem} |
| 179 |
% The item command if an optional argument is used. |
| 180 |
% |
| 181 |
% We only prepend some code. |
| 182 |
% \begin{macrocode} |
| 183 |
\AddToHookWithArguments{cmd/@lbibitem/before} |
| 184 |
{ |
| 185 |
% \end{macrocode} |
| 186 |
% we store the target name for the label. |
| 187 |
% \begin{macrocode} |
| 188 |
\tl_set:Nn\l_@@_bib_citekey_tl{#2} |
| 189 |
} |
| 190 |
% \end{macrocode} |
| 191 |
% The target is added at the begin of the paragraph. We give that a label as |
| 192 |
% it perhaps need to be removed by packages |
| 193 |
% \begin{macrocode} |
| 194 |
\AddToHookWithArguments{cmd/@lbibitem/before}[latex-lab-testphase-bib/target] |
| 195 |
{ |
| 196 |
\AddToHookNext{para/begin} |
| 197 |
{ |
| 198 |
\makebox[0pt][r]{\MakeLinkTarget*{cite.#2\@extra@b@citeb}\hspace{\leftmargin}} |
| 199 |
} |
| 200 |
} |
| 201 |
% \end{macrocode} |
| 202 |
% we make a copy to be able to reinstate the definition. This is e.g. |
| 203 |
% currently needed with hyperref. |
| 204 |
% \begin{macrocode} |
| 205 |
\let\@kernel@copy@lbibitem\@lbibitem |
| 206 |
% \end{macrocode} |
| 207 |
% \end{macro} |
| 208 |
% |
| 209 |
% \begin{macro}{\@bibitem} |
| 210 |
% Similar for \cs{@bibitem}. |
| 211 |
% TODO: If hyperref is loaded we will get a second target from the refstepcounter, |
| 212 |
% but this is ignored for now. |
| 213 |
% \begin{macrocode} |
| 214 |
\AddToHookWithArguments{cmd/@bibitem/before} |
| 215 |
{ |
| 216 |
% \end{macrocode} |
| 217 |
% we store the target name for the label. |
| 218 |
% \begin{macrocode} |
| 219 |
\tl_set:Nn\l_@@_bib_citekey_tl{#1} |
| 220 |
} |
| 221 |
% \end{macrocode} |
| 222 |
% |
| 223 |
% The target is added at the begin of the paragraph. We give that a label as |
| 224 |
% it perhaps need to be removed by packages |
| 225 |
% \begin{macrocode} |
| 226 |
\AddToHookWithArguments{cmd/@bibitem/before}[latex-lab-testphase-bib/target] |
| 227 |
{ |
| 228 |
\AddToHookNext{para/begin} |
| 229 |
{ |
| 230 |
\makebox[0pt][r]{\MakeLinkTarget*{cite.#1\@extra@b@citeb}\hspace{\leftmargin}} |
| 231 |
} |
| 232 |
} |
| 233 |
% \end{macrocode} |
| 234 |
% \begin{macrocode} |
| 235 |
\let\@kernel@copy@bibitem\@bibitem |
| 236 |
% \end{macrocode} |
| 237 |
% \end{macro} |
| 238 |
% |
| 239 |
% TODO The LI-structure should set a label, we redefine the internal command locally for |
| 240 |
% now, but perhaps this need a recipe? |
| 241 |
% |
| 242 |
% \begin{macrocode} |
| 243 |
\AddToHook{env/thebibliography/begin} |
| 244 |
{ |
| 245 |
\cs_set:Npn \__block_list_item_begin: |
| 246 |
{ |
| 247 |
\tag_struct_begin:n |
| 248 |
{ |
| 249 |
tag=\LItag, |
| 250 |
label= cite.\l_@@_bib_citekey_tl\@extra@b@citeb |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
% \end{macrocode} |
| 255 |
% |
| 256 |
% \subsection{Handling citation commands} |
| 257 |
% We redefine similar to hyperref the \cs{bibcite} command to inject link and |
| 258 |
% structure. Even if it looks a bit odd it is now used for many years and so |
| 259 |
% hopefully compatible with various packages. But differently to hyperref we use |
| 260 |
% the new hooks with arguments. |
| 261 |
% TODO: consider hook name. |
| 262 |
% \begin{macrocode} |
| 263 |
\NewMirroredHookPairWithArguments{bibcite/before}{bibcite/after}{2} |
| 264 |
\def\bibcite#1#2{% |
| 265 |
\@newl@bel{b}{#1\@extra@binfo}{% |
| 266 |
\UseHookWithArguments{bibcite/before}{2}{#1}{#2} |
| 267 |
#2 |
| 268 |
\UseHookWithArguments{bibcite/after}{2}{#1}{#2} |
| 269 |
}% |
| 270 |
}% |
| 271 |
\let\@kernel@copy@bibcite\bibcite |
| 272 |
% \end{macrocode} |
| 273 |
% Now we add the tagging structure. |
| 274 |
% TODO: with the next tagpdf version it should no longer be |
| 275 |
% needed to expand the ref key. |
| 276 |
% \begin{macrocode} |
| 277 |
\AddToHookWithArguments{bibcite/before} |
| 278 |
{ |
| 279 |
\tag_mc_end_push: |
| 280 |
\exp_args:Ne\tagstructbegin{tag=Reference,ref=cite.#1\@extra@b@citeb} |
| 281 |
\tagmcbegin{} |
| 282 |
} |
| 283 |
\AddToHookWithArguments{bibcite/after}[tag] |
| 284 |
{ |
| 285 |
\tag_mc_end: |
| 286 |
\tagstructend |
| 287 |
\tag_mc_begin_pop:n{} |
| 288 |
} |
| 289 |
% \end{macrocode} |
| 290 |
% At last the code for hyperref, the link will be inside the reference, but |
| 291 |
% this can be changed with a rule. |
| 292 |
% \begin{macrocode} |
| 293 |
\AddToHook{package/hyperref/after} |
| 294 |
{ |
| 295 |
\AddToHookWithArguments{bibcite/before} |
| 296 |
{ |
| 297 |
\hyper@linkstart{cite}{cite.#1\@extra@b@citeb} |
| 298 |
} |
| 299 |
\AddToHookWithArguments{bibcite/after}{\hyper@linkend} |
| 300 |
} |
| 301 |
% \end{macrocode} |
| 302 |
% |
| 303 |
% \subsection{Natbib and biblatex support} |
| 304 |
% When hyperref is loaded, both natbib and biblatex use \cs{hyper@natlinkstart} |
| 305 |
% and \cs{hyper@natlinkend} to handle the links. We can use the generic hooks to |
| 306 |
% add the tagging code (and the link code from hyperref). |
| 307 |
% We need in part different code for both systems: |
| 308 |
% with biblatex we have to take care that only the first |
| 309 |
% structure sets a label, and if |
| 310 |
% hyperref is not loaded (or deactivated) we will need additional code |
| 311 |
% but this currently doesn't exist. |
| 312 |
% We assume that no document loads both package -- that will probably break. |
| 313 |
% \begin{macrocode} |
| 314 |
\newcommand\hyper@natlinkstart[1]{} |
| 315 |
\newcommand\hyper@natlinkend{} |
| 316 |
% \end{macrocode} |
| 317 |
% With natbib we need to change the hooks to avoid duplicated target as it sets |
| 318 |
% the anchor too. We can not simply empty \cs{hyper@natanchorstart} as that is |
| 319 |
% used by biblatex. |
| 320 |
% \begin{macrocode} |
| 321 |
\AddToHook{package/natbib/after} |
| 322 |
{ |
| 323 |
\RemoveFromHook{cmd/@bibitem/before} [latex-lab-testphase-bib/target] |
| 324 |
\RemoveFromHook{cmd/@lbibitem/before}[latex-lab-testphase-bib/target] |
| 325 |
} |
| 326 |
% \end{macrocode} |
| 327 |
% This can be shared by both packages: it will work with natbib with and without hyperref. |
| 328 |
% With biblatex it will work without hyperref as long as \cs{@extra@b@citeb} is empty |
| 329 |
% \begin{macrocode} |
| 330 |
\AddToHookWithArguments{cmd/hyper@natlinkstart/before} |
| 331 |
{ |
| 332 |
\leavevmode |
| 333 |
\tag_mc_end_push: |
| 334 |
\exp_args:Ne\tag_struct_begin:n{tag=Reference,ref=cite.#1\@extra@b@citeb} |
| 335 |
\tag_mc_begin:n{} |
| 336 |
} |
| 337 |
\AddToHook{cmd/hyper@natlinkend/after} |
| 338 |
{ |
| 339 |
\tag_mc_end: |
| 340 |
\tag_struct_end: |
| 341 |
\tag_mc_begin_pop:n{} |
| 342 |
} |
| 343 |
|
| 344 |
% \end{macrocode} |
| 345 |
% if hyperref is loaded we have to revert its definition of the natbib hooks |
| 346 |
% and add its code through the generic hooks. |
| 347 |
% TODO: allow to suppress the natbib code in hyperref. |
| 348 |
% |
| 349 |
% \begin{macrocode} |
| 350 |
\AddToHook{package/hyperref/after} |
| 351 |
{ |
| 352 |
\renewcommand\hyper@natlinkstart[1]{} |
| 353 |
\renewcommand\hyper@natlinkend{} |
| 354 |
\AddToHookWithArguments{cmd/hyper@natlinkstart/before} |
| 355 |
{ |
| 356 |
\Hy@backout{#1}% |
| 357 |
% \end{macrocode} |
| 358 |
% natbib passes the \cs{@extra@b@citeb} in the argument, and biblatex |
| 359 |
% the refsection, so we only need to add the prefix \texttt{cite.}. |
| 360 |
% \begin{macrocode} |
| 361 |
\hyper@linkstart{cite}{cite.#1}% |
| 362 |
\def\hyper@nat@current{#1} |
| 363 |
} |
| 364 |
\AddToHook{cmd/hyper@natlinkend/after} |
| 365 |
{ |
| 366 |
\hyper@linkend |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
% \end{macrocode} |
| 371 |
% and now special biblatex code. The list item code has to test if the |
| 372 |
% anchor is already known: |
| 373 |
% \begin{macrocode} |
| 374 |
\AddToHook{cmd/blx@bibinit/after} |
| 375 |
{ |
| 376 |
\cs_set:Npn \__block_list_item_begin: |
| 377 |
{ |
| 378 |
\xifinlist{\the\c@refsection @\abx@field@entrykey}{\blx@anchors} |
| 379 |
{ |
| 380 |
\tag_struct_begin:n |
| 381 |
{ |
| 382 |
tag=\LItag, |
| 383 |
} |
| 384 |
} |
| 385 |
{ |
| 386 |
\tag_struct_begin:n |
| 387 |
{ |
| 388 |
tag=\LItag, |
| 389 |
label= cite.\the\c@refsection @\abx@field@entrykey |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
% \end{macrocode} |
| 395 |
% biblatex without hyperref is currently not supported but we at least avoid that it |
| 396 |
% errors: |
| 397 |
% \begin{macrocode} |
| 398 |
\AddToHook{package/biblatex/after} |
| 399 |
{ |
| 400 |
\appto\blx@mknohyperref |
| 401 |
{ |
| 402 |
\let\blx@anchors\@empty |
| 403 |
\protected\def\blx@anchor{% |
| 404 |
\xifinlist{\the\c@refsection @\abx@field@entrykey}{\blx@anchors} |
| 405 |
{} |
| 406 |
{\listxadd\blx@anchors{\the\c@refsection @\abx@field@entrykey}}}% |
| 407 |
} |
| 408 |
} |
| 409 |
% \end{macrocode} |
| 410 |
% \begin{macrocode} |
| 411 |
%</package> |
| 412 |
% \end{macrocode} |
| 413 |
% \begin{macrocode} |
| 414 |
%<*latex-lab> |
| 415 |
\ProvidesFile{bib-latex-lab-testphase.ltx} |
| 416 |
[\ltlabbibdate\space v\ltlabbibversion\space |
| 417 |
latex-lab wrapper bib] |
| 418 |
|
| 419 |
\RequirePackage{latex-lab-testphase-bib} |
| 420 |
|
| 421 |
%</latex-lab> |
| 422 |
% \end{macrocode} |