| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: latex-lab-bib.dtx (C) Copyright 2023-2026 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 latex-lab bundle is developed in the LaTeX2e GitHub. |
| 14 |
% Issues may be reported at |
| 15 |
% |
| 16 |
% https://github.com/latex3/latex2e/issues |
| 17 |
% |
| 18 |
\def\ltlabbibdate{2026-04-27} |
| 19 |
\def\ltlabbibversion{0.81e} |
| 20 |
%<*driver> |
| 21 |
\DocumentMetadata{tagging=on,pdfstandard=ua-2} |
| 22 |
\documentclass{l3in2edoc} |
| 23 |
\EnableCrossrefs |
| 24 |
\CodelineIndex |
| 25 |
\begin{document} |
| 26 |
\DocInput{latex-lab-bib.dtx} |
| 27 |
\end{document} |
| 28 |
%</driver> |
| 29 |
% |
| 30 |
% \fi |
| 31 |
% |
| 32 |
% \title{The \textsf{latex-lab-bib} package\\ |
| 33 |
% Changes and additions to the kernel related to tagging and links in citations and |
| 34 |
% bibliography entries} |
| 35 |
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}} |
| 36 |
% \date{v\ltlabbibversion\ \ltlabbibdate} |
| 37 |
% |
| 38 |
% \maketitle |
| 39 |
% |
| 40 |
% \providecommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}} |
| 41 |
% |
| 42 |
% \begin{abstract} |
| 43 |
% \end{abstract} |
| 44 |
% |
| 45 |
% \section{Introduction} |
| 46 |
% |
| 47 |
% The followings contains small changes to improve tagging of |
| 48 |
% bibliography entries and citations. |
| 49 |
% |
| 50 |
% The tagging of the standard bibliography is actually quite straightforward: |
| 51 |
% A bibliography is typically a list with a heading and the code which tags sectioning |
| 52 |
% commands and lists handles that. |
| 53 |
% |
| 54 |
% There are here only two problems: |
| 55 |
% |
| 56 |
% \begin{itemize} |
| 57 |
% \item The structure number of the \texttt{LI} element |
| 58 |
% created by a \cs{bibitem} must be recorded somehow to allow to reference it in |
| 59 |
% a \cs{cite}. |
| 60 |
% \item \pkg{hyperref} redefines the item command and so breaks the list structure |
| 61 |
% see \url{https://github.com/latex3/latex2e/discussions/1010#discussioncomment-5565418} |
| 62 |
% \end{itemize} |
| 63 |
% |
| 64 |
% Both problems are rather easy to resolve, but it must be checked if other packages |
| 65 |
% interfere again by redefining the commands. |
| 66 |
% |
| 67 |
% More difficult is the tagging of citation commands. Citations should be inside |
| 68 |
% a Reference structure and contain a /Ref entry pointing to the relevant |
| 69 |
% item in the bibliography. For simple citations like |
| 70 |
% \enquote{[1]} or \enquote{Doody (2023)} this is easy, but it is not obvious how |
| 71 |
% to handle combined citations like \enquote{Doody (2003,2018)} (or even compressed citations |
| 72 |
% like \enquote{[1-3]}). The current implementation follows the links: whatever \pkg{hyperref} would |
| 73 |
% link is set as the reference. |
| 74 |
% |
| 75 |
% There exist various packages which over the years tried to improve and extend |
| 76 |
% the bibliography commands. We discuss here three: \pkg{natbib}, \pkg{chapterbib} and \pkg{biblatex}. |
| 77 |
% |
| 78 |
% \begin{description} |
| 79 |
% \item[\pkg{natbib}] It is rather easy to support \pkg{natbib}: it has hooks for links and the tagging code |
| 80 |
% can follow. Only a bit coordination with \pkg{hyperref} is needed to avoid that \pkg{hyperref} |
| 81 |
% remove the tagging code again. |
| 82 |
% |
| 83 |
% \item[chapterbib] In standard LaTeX every bib entry has an unique label which points to the |
| 84 |
% (mandatory and unique) bibliography and the target created by \pkg{hyperref} |
| 85 |
% has the simple form \texttt{cite.}\meta{key}. |
| 86 |
% If a package that support multiple bibliographies is used (e.g. chapterbib) |
| 87 |
% this is no longer works: a bib entry \texttt{doody} |
| 88 |
% can in one chapter get the label \enquote{[1]} |
| 89 |
% and in the other \enquote{[5]} or even \enquote{Doo19} and naturally links |
| 90 |
% should jump to the relevant chapter bibliography. chapterbib solves this |
| 91 |
% by creating bib keys with a suffix: when reading the \texttt{.aux} files it will |
| 92 |
% create the keys \texttt{doody@-1} and \texttt{doody@-2} where the number is related |
| 93 |
% to the chapter/include, and in the document and in the document \verb+\cite{doody}+ |
| 94 |
% will look for \texttt{doody@-1} and \texttt{doody@-2} depending on the number of the |
| 95 |
% current include. For some unknown reason chapterbib uses two commands |
| 96 |
% to handle the suffix: the command \cs{@extra@binfo} is written to the aux-files |
| 97 |
% and used when processing the \cs{bibcite} commands, |
| 98 |
% but in the document \cs{@extra@b@citeb} is used. Supporting this is |
| 99 |
% straightforward: one only has to take care that the tagging code uses |
| 100 |
% \cs{@extra@b@citeb} in the relevant places too. |
| 101 |
% |
| 102 |
% \item[\pkg{biblatex}] \pkg{biblatex} supports multiple bibliographies out-of-the-box. |
| 103 |
% It numbers the link target by refsection and uses then the name |
| 104 |
% \verb+\the\c@refsection @+\meta{key}. |
| 105 |
% |
| 106 |
% Printing a bibliography is not required, in this case you get an engine warning |
| 107 |
% and links jump to the begin of the document: |
| 108 |
% \begin{verbatim} |
| 109 |
% name{cite.0@doody} has been referenced but does not exist |
| 110 |
% \end{verbatim} |
| 111 |
% |
| 112 |
% Bibliographies can be printed more than once by refsection. To avoid duplicated |
| 113 |
% target, \pkg{biblatex} stores the names of the targets in a list and if later |
| 114 |
% it detects that a target name has already been used in a |
| 115 |
% bibliography no new target is created for this item. This means a citation |
| 116 |
% will normally jump to the first bibliography which shows the entry. |
| 117 |
% |
| 118 |
% The tagging code has to mimic this code. This means that it can't label every item, |
| 119 |
% but has to test if this anchor is already known. |
| 120 |
% \end{description} |
| 121 |
% |
| 122 |
% \section{Provided or redefined commands} |
| 123 |
% |
| 124 |
% \begin{function}{\@extra@binfo,\@extra@b@citeb} |
| 125 |
% |
| 126 |
% These are taken from \pkg{hyperref}, they are for chapterbib compatibility (and also |
| 127 |
% signal to chapterbib not to change the citation commands) |
| 128 |
% \end{function} |
| 129 |
% |
| 130 |
% \begin{function}{\@bibitem,\@lbibitem} |
| 131 |
% The internal item commands. |
| 132 |
% \end{function} |
| 133 |
% |
| 134 |
% \section{Implementation} |
| 135 |
% \begin{macrocode} |
| 136 |
%<*package> |
| 137 |
%<@@=tag> |
| 138 |
% \end{macrocode} |
| 139 |
% \changes{v0.81e}{2026-04-27}{Revised implementation.} |
| 140 |
% \begin{macrocode} |
| 141 |
\ProvidesExplPackage {latex-lab-testphase-bib} {\ltlabbibdate} {\ltlabbibversion} |
| 142 |
{Code related to the tagging of bibliography and cite command} |
| 143 |
% \end{macrocode} |
| 144 |
% |
| 145 |
% |
| 146 |
% \begin{macro}{\@extra@binfo,\@extra@b@citeb} |
| 147 |
% These are taken from \pkg{hyperref}, they are for chapterbib compatibility (and also |
| 148 |
% signal to chapterbib not to change the citation commands) |
| 149 |
% \begin{macrocode} |
| 150 |
\providecommand*\@extra@binfo{}% |
| 151 |
\providecommand\@extra@b@citeb{} |
| 152 |
% \end{macrocode} |
| 153 |
% \end{macro} |
| 154 |
% |
| 155 |
% |
| 156 |
%\subsection{Handling the bibliography} |
| 157 |
% \begin{macro}{\@lbibitem} |
| 158 |
% This is the command used if \cs{bibitem} has an optional argument. |
| 159 |
% |
| 160 |
% The link target is now added by the block code in the \cs{item} command (as long as the |
| 161 |
% bibliography is built as a list). |
| 162 |
% We only need to change the name. |
| 163 |
% This also works if \pkg{hyperref} is not loaded as the kernel \cs{MakeLinkTarget} also contains |
| 164 |
% the \texttt{target/setname/after} hook. |
| 165 |
% |
| 166 |
% \begin{macrocode} |
| 167 |
\AddToHookWithArguments{cmd/@lbibitem/before}[latex-lab-testphase-bib/target] |
| 168 |
{ |
| 169 |
\ExpandArgs{e}\NextLinkTarget{cite.#2\@extra@b@citeb} |
| 170 |
} |
| 171 |
% \end{macrocode} |
| 172 |
% We make a copy to be able to reinstate the definition. This is, e.g., |
| 173 |
% currently needed with \pkg{hyperref}. |
| 174 |
% \begin{macrocode} |
| 175 |
\let\@kernel@copy@lbibitem\@lbibitem |
| 176 |
% \end{macrocode} |
| 177 |
% \end{macro} |
| 178 |
% |
| 179 |
% \begin{macro}{\@bibitem} |
| 180 |
% Similar for \cs{@bibitem}. |
| 181 |
% |
| 182 |
% The target is added by the block code. We only need to change the name. |
| 183 |
% \begin{macrocode} |
| 184 |
\AddToHookWithArguments{cmd/@bibitem/before}[latex-lab-testphase-bib/target] |
| 185 |
{ |
| 186 |
\ExpandArgs{e}\NextLinkTarget{cite.#1\@extra@b@citeb} |
| 187 |
} |
| 188 |
% \end{macrocode} |
| 189 |
% \begin{macrocode} |
| 190 |
\let\@kernel@copy@bibitem\@bibitem |
| 191 |
% \end{macrocode} |
| 192 |
% \end{macro} |
| 193 |
% |
| 194 |
% \subsection{Handling citation commands} |
| 195 |
% We redefine similar to \pkg{hyperref} the \cs{bibcite} command to inject link and |
| 196 |
% structure. Even if it looks a bit odd it is now used for many years and so |
| 197 |
% hopefully compatible with various packages. But differently to \pkg{hyperref} we use |
| 198 |
% the new hooks with arguments. |
| 199 |
% TODO: consider hook name. |
| 200 |
% \begin{macrocode} |
| 201 |
\NewMirroredHookPairWithArguments{bibcite/before}{bibcite/after}{2} |
| 202 |
\def\bibcite#1#2{% |
| 203 |
\@newl@bel{b}{#1\@extra@binfo}{% |
| 204 |
\UseHookWithArguments{bibcite/before}{2}{#1}{#2} |
| 205 |
#2 |
| 206 |
\UseHookWithArguments{bibcite/after}{2}{#1}{#2} |
| 207 |
}% |
| 208 |
}% |
| 209 |
\let\@kernel@copy@bibcite\bibcite |
| 210 |
% \end{macrocode} |
| 211 |
% Now we add the tagging structure. The target is in the structure of the label, |
| 212 |
% so we use the tagpdf command that retrieve the parent structure, i.e., the \texttt{LI}. |
| 213 |
% \begin{macrocode} |
| 214 |
\NewTaggingSocket{bib/cite/before}{1} |
| 215 |
\NewTaggingSocketPlug{bib/cite/before}{kernel} |
| 216 |
{ |
| 217 |
\tag_mc_end_push: |
| 218 |
\tagstructbegin{tag=\UseStructureName{cite}} |
| 219 |
\tag_struct_gput:nne{\tag_get:n{struct_num}}{ref_dest_parent}{#1} |
| 220 |
\tagmcbegin{} |
| 221 |
} |
| 222 |
\AssignTaggingSocketPlug{bib/cite/before}{kernel} |
| 223 |
\AddToHookWithArguments{bibcite/before} |
| 224 |
{ |
| 225 |
\UseTaggingSocket{bib/cite/before}{cite.#1\@extra@b@citeb} |
| 226 |
} |
| 227 |
\AddToHookWithArguments{bibcite/after} |
| 228 |
{ |
| 229 |
\UseTaggingSocket{inline/end} |
| 230 |
} |
| 231 |
% \end{macrocode} |
| 232 |
% |
| 233 |
% The package \pkg{hyperref} uses the hook too and the link will be inside the reference. |
| 234 |
% |
| 235 |
% \subsection{\pkg{natbib} and \pkg{biblatex} support} |
| 236 |
% Both \pkg{natbib} and \pkg{biblatex} use \cs{hyper@natlinkstart}, \cs{hyper@natlinkend} |
| 237 |
% and \cs{hyper@natanchorstart} to handle the links and anchors. The commands can be also |
| 238 |
% used to handle tagging, both if \pkg{hyperref} is loaded and if not. We only need to ensure |
| 239 |
% that they use \cs{MakeLinkTarget} also if \pkg{hyperref} is not active (and that \pkg{hyperref} |
| 240 |
% does not overwrite that again if used). |
| 241 |
% |
| 242 |
% \pkg{natbib} calls \verb+\hyper@natanchorstart{key\@extra@b@citeb}+. With \pkg{hyperref} loaded this |
| 243 |
% gives the anchor \verb+cite.key\@extra@b@citeb+. |
| 244 |
% \pkg{biblatex} calls \verb+\hyper@natanchorstart{\the\c@refsection @\abx@field@entrykey}+ |
| 245 |
% and this gives \verb+cite.\the\c@refsection @\abx@field@entrykey+. |
| 246 |
% |
| 247 |
% We assume that no document loads both package -- that will probably break. |
| 248 |
% \begin{macrocode} |
| 249 |
\newcommand\hyper@natlinkstart[1]{} |
| 250 |
\newcommand\hyper@natlinkend{} |
| 251 |
\newcommand\hyper@natanchorstart[1]{} |
| 252 |
% \end{macrocode} |
| 253 |
% We overwrite the anchor command at begin document so that this is also used with \pkg{hyperref}. |
| 254 |
% \begin{macrocode} |
| 255 |
\AtBeginDocument{\renewcommand\hyper@natanchorstart[1]{\MakeLinkTarget*{cite.#1}}} |
| 256 |
% \end{macrocode} |
| 257 |
% |
| 258 |
% The code for the citations can be shared by both packages, as they both use the |
| 259 |
% \pkg{natbib} hooks. |
| 260 |
% The structure this references is currently the |
| 261 |
% itemlabel for \pkg{natbib}, and the itembody for \pkg{biblatex}. TODO: move this up one level. |
| 262 |
% \changes{0.81e}{2026-04-27}{Remove \cs{@extra@b@citeb}, tagging issue \#1282} |
| 263 |
% \begin{macrocode} |
| 264 |
\AddToHookWithArguments{cmd/hyper@natlinkstart/before} |
| 265 |
{ |
| 266 |
\leavevmode |
| 267 |
\UseTaggingSocket{bib/cite/before}{cite.#1} |
| 268 |
} |
| 269 |
\AddToHook{cmd/hyper@natlinkend/after} |
| 270 |
{ |
| 271 |
\UseTaggingSocket{inline/end} |
| 272 |
} |
| 273 |
|
| 274 |
% \end{macrocode} |
| 275 |
% |
| 276 |
% \begin{macrocode} |
| 277 |
%</package> |
| 278 |
% \end{macrocode} |