Debian Codegraph @

texmf-dist/source/latex/hyperref/hyperref-linktarget.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: hyperref-linktarget.dtx
4 %
5 % Copyright (C) 2022-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "Hyperref bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/hyperref
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \DocumentMetadata{tagging=on}
27 \documentclass[full]{l3in2edoc}
28 \usepackage{array,booktabs}
29 \hypersetup{pdfauthor=The LaTeX Project,
30 pdftitle=Make link targets (hyperref bundle)}
31 \usepackage[skins]{tcolorbox}
32 \definecolor{myblue}{RGB}{00,33,99}
33 \newtcolorbox{doccomment}[1][]{enhanced,frame hidden,colback=myblue!10,fontupper=\footnotesize,#1}
34 %\newtcbox{smalldoccomment}{size=small,on line,enhanced,frame hidden,colback=myblue!10,fontupper=\footnotesize}
35 \begin{document}
36 \DocInput{\jobname.dtx}
37 \end{document}
38 %</driver>
39 % \fi
40 % \title{^^A
41 % Make link targets ^^A
42 % \\
43 % hyperref bundle
44 % }
45 %
46 % \author{^^A
47 % The \LaTeX{} Project\thanks
48 % {^^A
49 % E-mail:
50 % \href{mailto:[email protected]}
51 % {[email protected]}^^A
52 % }^^A
53 % }
54 %
55 % \date{Version 2026-04-24 v7.01q}
56 %
57 % \maketitle
58 % \begin{documentation}
59 % \section{Commands to create and adapt targets}
60 % This module provides commands to create targets.
61 % Their goal is to unify and replace
62 % a number of user and internal hyperref commands and to provide a clear
63 % interface for package authors and users.
64 % \subsection{The main command}
65 % \begin{function}{\MakeLinkTarget}
66 % \begin{syntax}
67 % \cs{MakeLinkTarget}\oarg{prefix}\Arg{counter}\\
68 % \cs{MakeLinkTarget}\oarg{prefix}\{\} \\
69 % \cs{MakeLinkTarget}*\Arg{manual target}
70 % \end{syntax}
71 % \end{function}
72 %
73 % \cs{MakeLinkTarget} creates a target for an internal link, (called a destination
74 % if a PDF is created). In vertical mode the target is created where the command is issued,
75 % in horizontal mode it is typically (in a PDF) raised by the current \cs{normalbaselineskip}
76 % (similar to the targets created by \cs{refstepcounter}).
77 %
78 % The arguments allow to control the name of the target/anchor/destination. While technically
79 % any unique name (e.g. some number) would work, names related to the actual counter both
80 % simplifies the debugging and referencing destinations from external documents. Also \cs{autoref}
81 % makes use of the name to identify the type of a label: it splits off the part until the first period and
82 % then looks up for a defined name. So if you create a target name \texttt{ABC.CDE.1.2}, \cs{autoref} tries
83 % to use \cs{ABCautorefname}.
84 %
85 % The name is then stored \emph{globally}\footnote{This means that the deprecated \pkg{hyperref}
86 % option \texttt{localanchorname} is ignored.} in \cs{@currentHref} and used for example in the
87 % next \cs{label}.
88 % A target name must be unique across a document. It is up to the users and package
89 % authors to ensure this by following the advice given below.
90 %
91 % Targets are not created if \cs{ifmeasuring@} is true.
92 %
93 % \begin{doccomment}
94 % A global \cs{@currentHref} means that the target name and
95 % the label name can get out of sync: if
96 % for example a numbered equation is used between a section and a \cs{label},
97 % a reference will show the section number but a link will jump to the equation.
98 %
99 % \pkg{hyperref} allows to switch to local assignment of \cs{@currentHref}
100 % and so to align with the other \cs{label} values with the
101 % (so-called experimental) option \texttt{localanchorname}. But while on a document
102 % level this can work, it makes it difficult for package authors
103 % to add reliable link targets that can be referenced if it is unclear
104 % if the assignment is local or global.
105 %
106 % So the choice was made to set \cs{@currentHref} globally always
107 % (and to deprecate \texttt{localanchorname}):
108 % This is the default in \pkg{hyperref} since ever and it didn't lead to
109 % major problems. It has the advantage to
110 % allow to put an anchor in a box and move it around. E.g. in the following
111 % example the two \cs{ref} commands
112 % reference the section but the second jumps to the top of the rule:
113 %
114 % \begin{verbatim}
115 % \section{An example}\label{sec:title}
116 % \newpage \raisebox{3cm}[0pt][0pt]{\phantomsection}%
117 % \rule{1cm}{3cm}\label{sec:page2}
118 % page two
119 % \newpage
120 % \ref{sec:title}, \ref{sec:page2}
121 % \end{verbatim}
122 %
123 % \end{doccomment}
124 %
125 %
126 %
127 % \subsection*{Target names from counters}
128 % \begin{syntax}
129 % \cs{MakeLinkTarget}\oarg{prefix}\Arg{counter}
130 % \end{syntax}
131 %
132 % If the mandatory argument is not empty it is interpreted as a \LaTeX{} counter name.
133 % The counter is not stepped by the command.
134 % If the \meta{counter} doesn't exist, a warning is issued and no target is created.
135 %
136 % The target name is created as expansion of
137 % \texttt{\meta{prefix}.\cs{theH\meta{counter}}} (and so both the prefix
138 % and \cs{theH\meta{counter}} should be expandable).
139 % The default prefix is the counter name \meta{counter}.
140 %
141 % For example:
142 %
143 % \medskip
144 % \noindent
145 % {\ttfamily
146 % \begin{tabular}{@{}l@{}ll}
147 % \cs{MakeLinkTarget}\{section\} &$\Rightarrow$ section.\cs{theHsection} & $\Rightarrow$ section.1.1\\
148 % \cs{MakeLinkTarget}[sec]\{section\} &$\Rightarrow$ sec.\cs{theHsection}
149 % \end{tabular}
150 % }
151 % \medskip
152 %
153 % \cs{theH\meta{counter}} must be defined:
154 % \cs{MakeLinkTarget} does not try like other \pkg{hyperref} commands to
155 % construct the command on-the-fly but warns and creates no
156 % target if it doesn't exist.
157 % Be aware that if \pkg{hyperref} is used with the option \texttt{implicit=false} it
158 % does not predefine and create \cs{theH\meta{counter}} representations
159 % so targets could be missing!\footnote{It is planned that \LaTeX{} directly defines
160 % the \cs{theH\meta{counter}} for all counters created with \cs{newcounter}.}
161 %
162 %
163 % Typically \cs{theH\meta{counter}} should expand to numbers
164 % and periods and make use of parent counters to give a unique representation.
165 % The prefix can be a more or less arbitrary string. Spaces are allowed (but not
166 % really recommended).
167 % A star at the end should be avoided to prevent clashes with
168 % the names created when the internal counter is used.
169 % The use of non-ASCII chars with pdflatex depends on the \LaTeX{} version: With newer
170 % version many of them are safe and with a format 2022-06-01 or newer
171 % it should be even possible to use chars in a target name which
172 % are undeclared and can't be typeset in the document.
173 % But despite the fact that it works, it is recommended to stick to ASCII
174 % and to avoid spaces: It is unclear if all PDF viewers and editors can handle
175 % them, also they give rather unreadable names in the PDF like |(\360\237\246\206)|
176 % and so make debugging harder.
177 %
178 %
179 % Using a special prefix can be useful if the actual counter
180 % has an internal name like e.g. \texttt{tcb@cnt@example}.
181 % \texttt{\cs{MakeLinkTarget}[tcbexample]\{tcb@cnt@example\}} then gives
182 % a nicer looking target name. Care should be taken not to clash
183 % with existing counter names and names for \cs{autoref}
184 % should be adjusted if needed.
185 %
186 %
187 % \subsection*{Targets using the internal counter}
188 % \begin{syntax}
189 % \cs{MakeLinkTarget}\oarg{prefix}\{\}
190 % \end{syntax}
191 %
192 % If the mandatory argument is empty a target name based on
193 % an internal absolute counter is created.
194 % The counter is stepped at every call (and also by other \pkg{hyperref} commands).
195 % The prefix is added with a star and a period (see above for
196 % the allowed chars). The default prefix is \texttt{page}.
197 %
198 %
199 % \begin{syntax}
200 % \begin{tabular}{@{}ll}
201 % \cs{MakeLinkTarget}\texttt{\{\}} &$\Rightarrow$ \texttt{page*.1}\\
202 % \cs{MakeLinkTarget}\texttt{\{\}} &$\Rightarrow$ \texttt{page*.2}\\
203 % \cs{MakeLinkTarget}[section]\texttt{\{\}} &$\Rightarrow$ \texttt{section*.3}\\
204 % \end{tabular}
205 % \end{syntax}
206 %
207 % As an example the
208 % \pkg{hyperref} command \cs{phantomsection} is equivalent to
209 % \texttt{\cs{MakeLinkTarget}[section]\{\}}.
210 %
211 % \subsection*{Manual target names}
212 % \begin{syntax}
213 % \cs{MakeLinkTarget}\texttt{*}\Arg{manual target name}
214 % \end{syntax}
215 %
216 % If the starred variant is used a manual target name is created.
217 %
218 % \texttt{\cs{MakeLinkTarget}*\{destname\}} is roughly equivalent to
219 % \texttt{\cs{hypertarget}\{destname\}\{\}} but unlike the
220 % latter it also raises the destination in a PDF, there is no text argument,
221 % and---most importantly---it updates \cs{@currentHref}, that means the target
222 % name is stored by the next \cs{label}, something that \cs{hypertarget} doesn't do).
223 % The target name is expanded, see the remarks about prefixes above regarding
224 % the allowed chars.
225 %
226 % When creating manually targets care should be taken to avoid
227 % clashes with automatic target names.
228 % As all automatic targets contain at least one period,
229 % names without a period are recommended.
230 %
231 % \begin{doccomment}
232 % The second (text) argument of \cs{hypertarget} is only used
233 % if the option \texttt{nesting} is set to true---but this option isn't used
234 % anywhere (in PDF the idea of a text in the anchor or nesting of anchors
235 % makes no sense anyway but also when html is produced e.g. with \texttt{make4ht}
236 % it is not used). So probably the option will be deprecated and removed.
237 % \end{doccomment}
238 %
239 % \subsection{Manipulate the next target name}
240 %
241 % Targets are sometimes created in places where it is difficult to inject
242 % a label to retrieve the target name for use e.g. in a bookmark.
243 % The next command allows to change the next target name:
244 %
245 % \begin{function}{\NextLinkTarget}
246 % \begin{syntax}
247 % \cs{NextLinkTarget}\Arg{manual target}
248 % \end{syntax}
249 %
250 % The command changes the next target name to \meta{manual target}.
251 % It does the same as the \cs{hypersetup} key
252 % \texttt{next-anchor} and also affects targets created by \cs{refstepcounter}.
253 %\end{function}
254 %
255 % A use case are bookmarks for the table of contents
256 % where you know that the heading will create
257 % a target:
258 %
259 % \begin{verbatim}
260 % \documentclass{book}
261 % \usepackage{bookmark}
262 %
263 % \begin{document}
264 % \bookmark[dest=toc]{Table of Contents}
265 % \NextLinkTarget{toc}
266 % \tableofcontents
267 % \chapter{A}
268 % \end{document}
269 % \end{verbatim}
270 %
271 %
272 % \subsection*{Hooks}
273 %
274 % \begin{function}{makelinktarget}
275 % The hook \texttt{makelinktarget}\footnote{The hook uses a plain name
276 % without reference to the \pkg{hyperref} package in anticipation of the move
277 % of this code into the \LaTeX{} kernel.}
278 % is executed at the begin of the commands. It is inside a group and
279 % so can be used to locally change settings. See below for an example.
280 % \end{function}
281 %
282 %
283 % \subsection*{Suppressing the target}
284 %
285 % \begin{function}{\LinkTargetOn,\LinkTargetOff}
286 % \begin{syntax}
287 % \cs{LinkTargetOn}\\
288 % \cs{LinkTargetOff}
289 % \end{syntax}
290 % \end{function}
291 %
292 % Targets are not created if \cs{ifmeasuring@} is true. With
293 % these commands it is also possible to switch on and off locally the creation of a target with
294 % \cs{MakeLinkTarget}.
295 % The switches are also honored by \cs{refstepcounter}\footnote{currently
296 % \cs{refstepcounter} doesn't use \cs{MakeLinkTarget} itself but this
297 % will probably change.}.
298 % This allows to suppress the target
299 % from an internal \cs{refstepcounter} and replace it by some manual version
300 % by using grouping:
301 %
302 % \begin{verbatim}
303 % \LinkTargetOff %suppress anchor in internal refstepcounter
304 % ...
305 % \refstepcounter{...}
306 % ...
307 % {\LinkTargetOn\MakeLinkTarget*{mytarget}} %create manual anchor
308 % ...
309 % \LinkTargetOn
310 % \end{verbatim}
311 %
312 %
313 % \subsection*{Raising the target}
314 %
315 % In horizontal mode the target is raised by the current value of \cs{normalbaselineskip}.
316 %
317 % To change this the hook can be used e.g. to double the value everywhere:
318 %
319 % \begin{verbatim}
320 % \AddToHook{cmd/MakeLinkTarget/before}
321 % {\setlength\normalbaselineskip{2\normalbaselineskip}}
322 % \leavevmode\MakeLinkTarget{section}
323 % \end{verbatim}
324 %
325 %
326 % \subsection{Changing all target names}
327 %
328 % \begin{function}{\SetLinkTargetFilter}
329 % \begin{syntax}
330 % \cs{SetLinkTargetFilter}\Arg{filter code using \#1}
331 % \end{syntax}
332 % \end{function}
333 %
334 % \pkg{hyperref} provides the command \cs{HyperDestNameFilter} to change all target names.
335 % It is applied\footnote{In the backend code, so it depends actually on
336 % the driver if it is honored or not}
337 % to every target name and is also used in references,
338 % but it doesn't change \cs{@currentHref} itself. So after
339 %
340 % \begin{verbatim}
341 % \renewcommand*{\HyperDestNameFilter}[1]{docA-#1}
342 % \end{verbatim}
343 %
344 % you would get in the PDF everywhere the prefix \texttt{docA}
345 %
346 % \begin{verbatim}
347 % %destination names:
348 % <<
349 % /Names [(docA-Doc-Start) 7 0 R (docA-chapter.1) 8 0 R (docA-page.1) 6 0 R]
350 % /Limits [(docA-Doc-Start) (docA-page.1)]
351 % >>
352 % %link to a chapter
353 % /A << /S /GoTo /D (docA-chapter.1) >>
354 % %link from the bookmark
355 % << /S /GoTo /D (docA-chapter.1) >>
356 % \end{verbatim}
357 %
358 % but the label info in the \texttt{.aux} would show only \texttt{chapter.1}:
359 %
360 % \begin{verbatim}
361 % \newlabel{chap}{{1}{1}{Title}{chapter.1}{}}
362 % \end{verbatim}
363 %
364 % and so \cs{autoref} is still able to extract the counter name.
365 %
366 % \cs{MakeLinkTarget} uses this filter too: it would break internal link
367 % commands if it would ignore it.
368 % To stay compatible with future development the filter should not be redefined
369 % directly but be set with \cs{SetLinkTargetFilter}.
370 % The command can only be used in the preamble.
371 %
372 % \begin{verbatim}
373 % \SetLinkTargetFilter{docA-#1}
374 % \end{verbatim}
375 % \end{documentation}
376 %
377 % \begin{implementation}
378 % \section{Implementation}
379 % \begin{macrocode}
380 %<@@=hyp>
381 %<*header>
382 \ProvidesExplPackage{hyperref-linktarget}{2022-06-20}{v7.00s}
383 {Making targets, destinations and anchors -- module of hyperref}
384 %</header>
385 %<package-include>\ExplSyntaxOn
386 % \end{macrocode}
387 % \subsection{Variables}
388 % \begin{macrocode}
389 %<*package>
390 % \end{macrocode}
391 % \begin{macro}{\l_@@_target_create_bool}
392 % This boolean decides if a target is created at all.
393 % (it will replace \cs{@skiphyperref} long term)
394 % \begin{macrocode}
395 \bool_new:N \l_@@_target_create_bool
396 \bool_set_true:N \l_@@_target_create_bool
397 % \end{macrocode}
398 % \end{macro}
399 % \begin{macro}{makelinktarget}
400 % This hook is used to adapt for example the raising
401 % \begin{macrocode}
402 \hook_new:n {makelinktarget}
403 % \end{macrocode}
404 % \end{macro}
405 % \subsection{Helper commands}
406 % \begin{macro}{\_@@_target_raise:n}
407 % We need a command to raise the targets.
408 % It is mostly a copy from the hyperref command
409 % but we removed the hooks and use \cs{normalbaselineskip}.
410 % TODO: The code to save/restore the space factor should
411 % be replaced by kernel methods.
412 % change 2024-11-06: add a kern, see thm-anchor test.
413 % change 2026-02-22: use the hyp/raisedlink socket for varwidth support.
414 % \begin{macrocode}
415 \cs_new_protected:Npn \@@_target_raise:n #1
416 {
417 \mode_if_vertical:TF
418 { \UseSocket{hyp/raisedlink}{#1} }%
419 {
420 \Hy@SaveSpaceFactor
421 \penalty\@M
422 \smash
423 {
424 \box_move_up:nn
425 { \normalbaselineskip }
426 {
427 \hbox:n
428 {
429 \Hy@RestoreSpaceFactor
430 #1
431 \Hy@SaveSpaceFactor
432 }
433 }
434 }
435 \kern0pt
436 \Hy@RestoreSpaceFactor
437 }
438 }
439 % \end{macrocode}
440 % \end{macro}
441 % \subsection{Providing the commands}
442 % In anticipation of the addition of the main commands to the
443 % kernel as no-ops we provide them:
444 % \begin{macrocode}
445 \ProvideDocumentCommand\LinkTargetOn{}{}
446 \ProvideDocumentCommand\LinkTargetOff{}{}
447 \ProvideDocumentCommand\MakeLinkTarget{sO{}m}{}
448 \ProvideDocumentCommand\NextLinkTarget{m}{}
449 % \end{macrocode}
450 %
451 % \subsection{Target on and off switch}
452 % \begin{macro}{\LinkTargetOn,\LinkTargetOff}
453 % \begin{macrocode}
454 \RenewDocumentCommand\LinkTargetOn {}
455 {
456 \bool_set_true:N \l_@@_target_create_bool
457 }
458
459
460 \RenewDocumentCommand\LinkTargetOff {}
461 {
462 \bool_set_false:N \l_@@_target_create_bool
463 }
464 % \end{macrocode}
465 % \end{macro}
466 %
467 % \begin{macro}{\MakeLinkTarget}
468 % This is the main command. To keep it simple
469 % we allow an optional argument also for the manual
470 % command but ignore it for now.
471 % \begin{macrocode}
472 \RenewDocumentCommand\MakeLinkTarget {s O{} m}
473 {
474 \bool_lazy_and:nnT
475 { \l_@@_target_create_bool }
476 { ! \legacy_if_p:n { measuring@ } }
477 {
478 \group_begin:
479 \hook_use:n { makelinktarget }
480 \IfBooleanTF {#1}
481 {
482 \@@_target_manual:nn {#2}{#3}
483 }
484 {
485 \@@_target_counter:nn {#2}{#3}
486 }
487 \group_end:
488 \UseTaggingSocket{recordtarget}
489 }
490 }
491 % \end{macrocode}
492 % \end{macro}
493 % \begin{macro}{\@@_target_manual:nn}
494 % This is the code for the manual target name. The prefix is simply ignored.
495 % \begin{macrocode}
496 \cs_new_protected:Npn \@@_target_manual:nn #1 #2 %#1 prefix, #2 name
497 {
498 \tl_gset:Ne \@currentHref {#2}
499 \hook_use:n {__hyp/target/setname}
500 \@onelevel@sanitize\@currentHref
501 \@@_target_raise:n {\hyper@anchorstart{\@currentHref}\hyper@anchorend}
502 }
503 % \end{macrocode}
504 % \end{macro}
505 % \begin{macro}{\@@_target_counter:nn}
506 % The code for counter related targets must be split into the case
507 % where the internal counter is used, and where a user counter is used
508 % \begin{macrocode}
509 \cs_new_protected:Npn \@@_target_counter:nn #1 #2 %#1 prefix, #2 counter or empty
510 {
511 \tl_if_blank:nTF {#2}
512 {
513 \@@_target_counter_anon:n {#1}
514 }
515 {
516 \@@_target_counter_doc:nn {#1}{#2}
517 }
518 }
519
520 % \end{macrocode}
521 %\end{macro}
522 %\begin{macro}{\@@_target_counter_anon:n}
523 % This creates the target with the internal count.
524 % We use the same (tex) count \cs{Hy@linkcounter} than the other
525 % hyperref commands.
526 % \begin{macrocode}
527 \cs_new_protected:Npn \@@_target_counter_anon:n #1
528 {
529 \int_gincr:N\Hy@linkcounter
530 \tl_gset:Ne \@currentHref
531 {\tl_if_blank:nTF{#1}{page}{#1}*.\int_use:N\Hy@linkcounter}
532 \hook_use:n {__hyp/target/setname}
533 \@onelevel@sanitize\@currentHref
534 \@@_target_raise:n {\hyper@anchorstart{\@currentHref}\hyper@anchorend}
535 }
536 % \end{macrocode}
537 % \end{macro}
538
539 % \begin{macro}{\@@_target_counter_doc:nn}
540 % And now the target with the user counter. We warn if
541 % the counter or the representation doesn't exist
542 % \begin{macrocode}
543 \cs_new_protected:Npn \@@_target_counter_doc:nn #1 #2
544 {
545 \bool_lazy_or:nnTF { \cs_if_free_p:c {c@#2} } { \cs_if_free_p:c {theH#2} }
546 {
547 \PackageWarning {hyperref}{Counter~'#2'~or~the~representation~'\string\theH#2`\MessageBreak
548 don't~exist.~No~target~created.}{}
549 }
550 {
551 \tl_gset:Ne \@currentHref {\tl_if_blank:nTF{#1}{#2}{#1}.\use:c{theH#2}}
552 \hook_use:n {@@/target/setname}
553 \@onelevel@sanitize\@currentHref
554 \@@_target_raise:n {\hyper@anchorstart{\@currentHref}\hyper@anchorend}
555 }
556 }
557 % \end{macrocode}
558 % \end{macro}
559 %
560 % \begin{macro}{\NextLinkTarget}
561 % we rely on the internal hook to set the next target name:
562 % \begin{macrocode}
563 \RenewDocumentCommand\NextLinkTarget {m}
564 {
565 \hook_gput_next_code:nn {@@/target/setname}
566 {
567 \tl_gset:Ne \@currentHref {#1}
568 }
569 }
570
571 % \end{macrocode}
572 % \end{macro}
573 % \begin{macro}{\SetLinkTargetFilter}
574 % This is an interface to \cs{HyperDestNameFilter}
575 % \begin{macrocode}
576 \NewDocumentCommand\SetLinkTargetFilter {m}
577 {
578 \cs_set:Npn \HyperDestNameFilter ##1 {#1}
579 }
580 \@onlypreamble \SetLinkTargetFilter
581 % \end{macrocode}
582 % \end{macro}
583 %
584 % \begin{macrocode}
585 %</package>
586 %<@@=>.
587 %<package-include>\ExplSyntaxOff
588 % \end{macrocode}
589 %\end{implementation}
590 % \Finale
591 \endinput