summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex-dev/latex-lab/latex-lab-toc-hyperref-changes.dtx
blob: ea39e3a6a58fd9b16b948bff1050fb8b7367c04f (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
% \iffalse meta-comment
%
%% File: toc-hyperref-changes.dtx (C) Copyright 2022-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.
%
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-toc-hyperref-changes.dtx}
\end{document}
%</driver>
%
% \fi
% \title{The \textsf{toc-hyperref-changes} package\\
% Changes and additions to hyperref related to toc entries}
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
% \date{Version 0.81 2023-02-10}
%
% \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 various change needed for hyperref for the new toc tagging
% code. They should be only loaded if hyperref is used!
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \section{Changes for hyperref}
% \subsection{\cs{contentsline},\cs{addcontentsline}}
% The standard \cs{contentsline} and \cs{addcontentsline}
% should also be used with hyperref (together
% with more changes below) so we have to disable the redefinition in hyperref (this 
% requires hyperref version 7.00v!
%    \begin{macrocode}
\def\hyper@nopatch@toc{}
\AddToHook{package/hyperref/after}
 {
   \@ifpackagelater{hyperref}{2023-02-07}{}
   {\PackageWarning{latex-lab-testphase-toc-tagging}{hyperref too old}{}}
 }  
%    \end{macrocode}
%
% \subsection{Add links to toc entries}
%    \begin{macrocode}
\ExplSyntaxOn
\AddToHook{package/hyperref/after}
 {
   \AddToHook{contentsline/text/before}[hyp]
     {
       \tl_if_blank:VF \@contentsline@arg@two%text
        {
          \tl_if_blank:VF\@contentsline@arg@four%destination
            {
              \int_case:nnF {\Hy@linktoc}
                {
                  {0}{} %none
                  {1}{\hyper@linkstart{link}{\@contentsline@arg@four}} %section
                  {2}{} %page
                }
                {\hyper@linkstart{link}{\@contentsline@arg@four}} %all
            }
          }
      }
    \AddToHook{contentsline/text/after}[hyp]
      {
        \tl_if_blank:VF \@contentsline@arg@two
         {
           \tl_if_blank:VF\@contentsline@arg@four
             {
               \int_case:nnF {\Hy@linktoc}
                 {
                   {0}{} %none
                   {1}{\hyper@linkend} %section
                   {2}{} %page
                 }
                 {\hyper@linkend} %all
             } % none
           }
       }

    \AddToHook{contentsline/page/before}[hyp]
      {
        \tl_if_blank:VF \@contentsline@arg@three
         {
           \tl_if_blank:VF\@contentsline@arg@two
             {
               \int_case:nnF {\Hy@linktoc}
                 {
                   {0}{} %none
                   {1}{} %section
                   {2}{\hyper@linkstart{link}{\@contentsline@arg@four}} %page
                 }
                 {\hyper@linkstart{link}{\@contentsline@arg@four}} %all
             } % none
           }
       }
    \AddToHook{contentsline/page/after}[hyp]
      {
        \tl_if_blank:VF \@contentsline@arg@three
         {
           \tl_if_blank:VF\@contentsline@arg@four
             {
               \int_case:nnF {\Hy@linktoc}
                 {
                   {0}{} %none
                   {1}{} %section
                   {2}{\hyper@linkend} %page
                 }
                 {\hyper@linkend} %all
             } % none
           }
       }
    \AddToHook{cmd/addcontentsline/before}[hyp]
     {%
       \ExpandArgs{ooo}
       \Hy@addcontentsline@addbookmark
         {\addcontentsline@arg@one}
         {\addcontentsline@arg@two}
         {\addcontentsline@arg@three}%
     }   
   }    
\ExplSyntaxOff
%    \end{macrocode}
% \subsection{Add bookmark code to \cs{addcontentsline}}
%
%    \begin{macrocode}
\newcommand\Hy@addcontentsline@addbookmark[3]%#1 toc type, #2 level, #3 content
  {%
   \ifx\@currentHref\@empty
    \Hy@Warning{%
      No destination for bookmark of \string\addcontentsline,%
      \MessageBreak destination is added%
     }%
    \phantomsection
  \fi
  \begingroup
    \expandafter\ifx\csname toclevel@#2\endcsname\relax
      \begingroup
        \def\Hy@tempa{#1}%
        \ifx\Hy@tempa\Hy@bookmarkstype
          \Hy@WarningNoLine{%
            bookmark level for unknown #2 defaults to 0%
          }%
        \else
          \Hy@Info{bookmark level for unknown #2 defaults to 0}%
        \fi
      \endgroup
      \expandafter\gdef\csname toclevel@#2\endcsname{0}%
    \fi
    \edef\Hy@toclevel{\csname toclevel@#2\endcsname}%
    \Hy@writebookmark{\csname the#2\endcsname}%
      {#3}%
      {\@currentHref}%
      {\Hy@toclevel}%
      {#1}%
    \ifHy@verbose
      \begingroup
        \def\Hy@tempa{#3}%
        \@onelevel@sanitize\Hy@tempa
        \let\temp@online\on@line
        \let\on@line\@empty
        \Hy@Info{%
          bookmark\temp@online:\MessageBreak
          thecounter {\csname the#2\endcsname}\MessageBreak
          text {\Hy@tempa}\MessageBreak
          reference {\@currentHref}\MessageBreak
          toclevel {\Hy@toclevel}\MessageBreak
          type {#1}%
        }%
      \endgroup
    \fi
   \endgroup
  }
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}