Debian Codegraph @

texmf-dist/source/latex-dev/latex-lab/latex-lab-title.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-title.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\ltlabtitledate{2026-05-07}
19 \def\ltlabtitleversion{0.85f}
20
21 %<*driver>
22 \documentclass{l3in2edoc}
23 \EnableCrossrefs
24 \CodelineIndex
25 \begin{document}
26 \DocInput{latex-lab-title.dtx}
27 \end{document}
28 %</driver>
29 %
30 % \fi
31 %
32 % \title{The \textsf{latex-lab-title} package\\
33 % Changes related to the tagging of the title}
34 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
35 % \date{v\ltlabtitleversion\ \ltlabtitledate}
36 %
37 % \maketitle
38 %
39 % \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
40 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
41 % \newcommand{\docclass}{document class \marginpar{\raggedright document class
42 % customizations}}
43 %
44 % \providecommand\hook[1]{\texttt{#1}}
45 %
46 % \begin{abstract}
47 % \end{abstract}
48 %
49 % \section{Introduction}
50 %
51 % This module contains changes to improve the tagging (in the standard classes) of
52 % the title created with the \cs{maketitle} command. It also improves the
53 % setting of the metadata related to the title and the author.
54 %
55 % For basic tagging of the printed title there are basically three things to do:
56 %
57 % \begin{itemize}
58 % \item The actual title should be tagged with the \texttt{Title} tag.
59 % \item The tabular used to format the author list should \emph{not} be tagged as a tabular.
60 % \item \cs{maketitle} redefines footnote internals. These must be made tagging aware.
61 % \end{itemize}
62 %
63 % A second task related to title is to store the authors and the title text
64 % (or a shorter version) inside the XMP-metadata and (in PDF 1.7 or lower) in
65 % the Info dictionary. Currently this can only be set if hyperref
66 % is loaded and requires the use of the \texttt{pdftitle} and \texttt{pdfauthor} keys.
67 % The new code therefore extends the \cs{title} and \cs{author} commands: They
68 % stores their argument and use them at the end of the document
69 % for the PDF metadata if the data hasn't been given in another way.
70 % The code also gives \cs{title} and \cs{author}
71 % an optional argument where the PDF title or author can be given with
72 % in a key-value syntax. As with hyperref it is possible to store titles
73 % in more than one language:
74 %
75 % \begin{verbatim}
76 % \title
77 % [pdftitle =
78 % {[en]English Title,[de] Deutscher Titel,[fr]{titre français, avec comma}}]
79 % {Document title}
80 % \end{verbatim}
81 %
82 % It is also possible to set a subtitle which is then stored in the XMP-metadata:
83 %
84 % \begin{verbatim}
85 % \title
86 % [pdfsubtitle =
87 % {[en]English Subtitle,[de] Deutscher Subtitel,[fr]{subtitre français, avec comma}}]
88 % {Document title}
89 % \end{verbatim}
90
91 %
92 % If using the \texttt{pdfauthor} key authors should be separated
93 % by commas, and to hide commas in a name inside braces if needed:
94 %
95 % \begin{verbatim}
96 % \author[pdfauthor = {Bär, Peter Anteater, {Riley, the sloth}}]{\ldots}
97 % \end{verbatim}
98 %
99 % If hyperref is loaded there is no difference to the \texttt{pdftitle} and
100 % \texttt{pdfauthor} key used in \cs{hypersetup}. Both can be used (and the last
101 % key used will win).
102 %
103 % \subsection{Open questions and TODOs}
104 %
105 % \begin{itemize}
106 % \item Writing into the Info dictionary needs to convert the input into a PDF string.
107 % This is here done with a simple version of hyperref's \cs{pdfstringdef}, similar code
108 % exist also in the generic hyperref driver. This should be moved into a better place
109 % module.
110 %
111 % \item Is it sensible to enhance \cs{author} and \cs{title} with an optional argument as done here?
112 % An advantage is that it is rather light-weight and
113 % doesn't require to decide how this values should be set in \cs{DocumentMetadata} (and would also work
114 % without \cs{DocumentMetadata}. But a problem could be that various classes and packages already
115 % extend this commands with other optional arguments.
116 %
117 % \item Some of the definitions related to metadata should perhaps be moved into l3pdfmeta.
118 %
119 % \item Are the names \texttt{pdftitle} and \texttt{pdfauthor} ok?
120 %
121 % \item The patch for \cs{thanks} to get a rlap-footnotemarker looks wrong. This
122 % probably means that some configuration option is missing in the footnote code.
123 % \end{itemize}
124 %
125 %
126 %
127 % \section{Implementation}
128 % \begin{macrocode}
129 %<*package>
130 %<@@=tag>
131 % \end{macrocode}
132 % \begin{macrocode}
133 \ProvidesExplPackage {latex-lab-testphase-title} {\ltlabtitledate} {\ltlabtitleversion}
134 {Changes related to the tagging of the title}
135 % \end{macrocode}
136 %
137 % \subsection{\cs{maketitle} in article class}
138 %
139 % \begin{macrocode}
140 \cs_new_protected:Npn \@@_patch_thanks:n #1
141 {
142 \rlap{\footnotemark}
143 \protected@xdef\@thanks{\@thanks
144 \protect\footnotetext[\the\c@footnote]{#1}}
145 }
146 % \end{macrocode}
147 % The no-titlepage version of article, report and book
148 % \begin{macrocode}
149 \cs_new_protected:Npn \@@_patch_maketitle:
150 {
151 \par
152 \begingroup
153 % \end{macrocode}
154 % Disable table tagging
155 % \begin{macrocode}
156 \tagpdfsetup{table/tagging=false}%
157 \renewcommand\thefootnote{\@fnsymbol\c@footnote}%
158 % \end{macrocode}
159 % the original definition redefines \cs{@makefnmark} and
160 % \cs{@makefntext} to get an rlap-mark in the text without
161 % affecting the mark in the note (which gives by the way
162 % a wrong link area with hyperref). There seem to be currently
163 % no good way in the footnote to configure this, so we redefine
164 % \cs{thanks} instead
165 % \begin{macrocode}
166 \cs_set_eq:NN \thanks \@@_patch_thanks:n
167 \if@twocolumn
168 \ifnum \col@number=\@ne
169 \@maketitle
170 \else
171 \twocolumn[\@maketitle]%
172 \fi
173 \else
174 \newpage
175 \global\@topnum\z@ % Prevents figures from going at top of page.
176 \@maketitle
177 \fi
178 \thispagestyle{plain}\@thanks
179 \endgroup
180 \setcounter{footnote}{0}%
181 \global\let\thanks\relax
182 \global\let\maketitle\relax
183 \global\let\@maketitle\relax
184 \global\let\@thanks\@empty
185 \global\let\@author\@empty
186 \global\let\@date\@empty
187 \global\let\@title\@empty
188 \global\let\title\relax
189 \global\let\author\relax
190 \global\let\date\relax
191 \global\let\and\relax
192 }
193 % \end{macrocode}
194 % We must also change \cs{@maketitle} to insert a \texttt{Title} tag.
195 % We have here two options: We can surround the whole block with a \texttt{Title}
196 % or only the title text (but not both).
197 % As default the title text has the \texttt{Title} tag and the block is a \texttt{Part} (from
198 % the symbolic name \texttt{para/semantic}),
199 % but this can be changed by reassigning the symbolic name \texttt{title/block}
200 % and changing the plug of the tagging socket to the transparent plug, e.g.,
201 % \begin{verbatim}
202 % \AssignStructureRole{title/block}{Title}
203 % \AssignTaggingSocketPlug{title}{transparent}
204 % \end{verbatim}
205 % Note that in PDF 1.7 the \texttt{Title} tag is not a standard tag, it is a user defined
206 % and by default rolemapped to \texttt{P}. If \texttt{Title} should be used for the
207 % title block the rolemapping must be adjusted, e.g., to \texttt{Part} or \texttt{Sect}.
208 %
209 % \begin{macrocode}
210 \AssignStructureRole{title/block}{\UseStructureName{para/semantic}}
211 \IfSocketExistsF{tagsupport/title}
212 {
213 \NewTaggingSocket{title}{2}
214 }
215 \NewTaggingSocketPlug{title}{kernel}
216 {
217 \pdf_version_compare:NnTF > {1.7}
218 {\tag_struct_begin:n{tag=\UseStructureName{title/text}}#2\tag_struct_end:}
219 {\AssignStructureRole{para/textblock}{\UseStructureName{title/text}}#2}%
220 }
221 \AssignTaggingSocketPlug{title}{kernel}
222 \cs_new_protected:Npn \@@_patch_@maketitle:
223 {
224 \newpage
225 \null
226 \vskip 2em%
227 \begin{center}%
228 [tagging-suppress-paras=true,
229 tagging-recipe=standalone,
230 tag-name=\UseStructureName{title/block}]
231 \let \footnote \thanks
232 % \end{macrocode}
233 % By default this socket adds \texttt{Title} around the title text.
234 % In PDF 1.7 \texttt{Title} is rolemapped to P and the socket
235 % adapts the \texttt{para/textblock} to avoid
236 % that we get an \texttt{P} inside a \texttt{P}.
237 % One can assign the \texttt{transparent} plug to the socket if
238 % \texttt{Title} should be used for the title block instead.
239 % \begin{macrocode}
240 {\LARGE\UseTaggingSocket{title}{}{\@title}\par}
241 \vskip 1.5em%
242 {\large
243 \lineskip .5em%
244 \begin{tabular}[t]{c}%
245 \@author
246 \end{tabular}\par}%
247 \vskip 1em%
248 {\large \@date}%
249 \end{center}%
250 \par
251 \vskip 1.5em
252 }
253
254 % \end{macrocode}
255 % The titlepage variant:
256 % \changes{0.85f}{2026-05-07}{Added support for title/block tag}
257 % \begin{macrocode}
258 \cs_new_protected:Npn \@@_patch_maketitle_page:
259 {\begin{titlepage}%
260 % \end{macrocode}
261 % disable table tagging
262 % \begin{macrocode}
263 \tagpdfsetup{table/tagging=false}%
264 \let\footnotesize\small
265 \let\footnoterule\relax
266 \let \footnote \thanks
267 \null\vfil
268 \vskip 60\p@
269 % \end{macrocode}
270 % \begin{macrocode}
271 \begin{center}%
272 [tagging-suppress-paras=true,
273 tagging-recipe=standalone,
274 tag-name=\UseStructureName{title/block}]
275 % \end{macrocode}
276 % By default this socket adds \texttt{Title} around the title text.
277 % In PDF 1.7 \texttt{Title} is rolemapped to P and the socket
278 % adapts the \texttt{para/textblock} to avoid
279 % that we get an \texttt{P} inside a \texttt{P}.
280 % One can assign the \texttt{transparent} plug to the socket if
281 % \texttt{Title} should be used for the title block instead.
282 % \begin{macrocode}
283 {\LARGE\UseTaggingSocket{title}{}{\@title}\par}
284 \vskip 3em%
285 {\large
286 \lineskip .75em%
287 \begin{tabular}[t]{c}%
288 \@author
289 \end{tabular}\par}%
290 \vskip 1.5em%
291 {\large \@date \par}% % Set date in \large size.
292 \end{center}\par
293 \@thanks
294 \vfil\null
295 \end{titlepage}%
296 \setcounter{footnote}{0}%
297 \global\let\thanks\relax
298 \global\let\maketitle\relax
299 \global\let\@thanks\@empty
300 \global\let\@author\@empty
301 \global\let\@date\@empty
302 \global\let\@title\@empty
303 \global\let\title\relax
304 \global\let\author\relax
305 \global\let\date\relax
306 \global\let\and\relax
307 }
308
309 % \end{macrocode}
310 % Map the new commands onto \cs{maketitle}:
311 % \begin{macrocode}
312 \AddToHook{class/article/after}
313 {
314 \if@titlepage
315 \cs_set_eq:NN \maketitle \@@_patch_maketitle_page:
316 \else
317 \cs_set_eq:NN \maketitle \@@_patch_maketitle:
318 \cs_set_eq:NN \@maketitle \@@_patch_@maketitle:
319 \fi
320 }
321 \AddToHook{class/report/after}
322 {
323 \if@titlepage
324 \cs_set_eq:NN \maketitle \@@_patch_maketitle_page:
325 \else
326 \cs_set_eq:NN \maketitle \@@_patch_maketitle:
327 \cs_set_eq:NN \@maketitle \@@_patch_@maketitle:
328 \fi
329 }
330 \AddToHook{class/book/after}
331 {
332 \if@titlepage
333 \cs_set_eq:NN \maketitle \@@_patch_maketitle_page:
334 \else
335 \cs_set_eq:NN \maketitle \@@_patch_maketitle:
336 \cs_set_eq:NN \@maketitle \@@_patch_@maketitle:
337 \fi
338 }
339 % \end{macrocode}
340 %
341 % \subsection{Helper commands to set metadata}
342 %
343 % Some temp variables
344 % \begin{macrocode}
345 \str_new:N \g_@@_title_tmpa_str
346 \str_new:N \l_@@_title_tmpa_str
347 \tl_new:N \l_@@_title_tmpa_tl
348 \seq_new:N \l_@@_title_tmpa_seq
349 % \end{macrocode}
350 %
351 % We ensure that the default definitions
352 % of \cs{@title} and \cs{@author} are robust:
353 % \begin{macrocode}
354 \protected\def\@title{\@latex@error{No~\noexpand\title given}\@ehc}
355 \protected\def\@author{\@latex@warning@no@line{No~\noexpand\author given}}
356 % \end{macrocode}
357 % A helper command to convert the title into a pdfstring similar to
358 % \cs{pdfstringdef}.
359 %\changes{0.85e}{2026-04-29}{The command makes now use of \cs{pdf_purify:nN}}
360 % \begin{macrocode}
361 \cs_new_protected:Npn \@@_title_pdfstring:nnN #1 #2 #3 % #1 text, #2 e.g. utf16/hex
362 {
363 \group_begin:
364 \pdf_purify:nN {#1}\l_@@_title_tmpa_tl
365 \pdf_string_from_unicode:nVN { #2 } \l_@@_title_tmpa_tl \l_@@_title_tmpa_str
366 \str_gset_eq:NN \g_@@_title_tmpa_str\l_@@_title_tmpa_str
367 \group_end:
368 \str_set_eq:NN #3 \g_@@_title_tmpa_str
369 }
370 \cs_generate_variant:Nn\@@_title_pdfstring:nnN {e}
371 % \end{macrocode}
372
373 % \subsection{Extend title to set metadata}
374 % At first a variable to store the title, as \cs{@title} is emptied by \LaTeX.
375 % \begin{macrocode}
376 \tl_new:N \g_@@_title_title_tl
377 % \end{macrocode}
378 % Now we redefine \cs{title} so that it stores the title, and processes
379 % keys in the optional argument. We use \texttt{hyp} as module
380 % name for the key as this means that if hyperref is loaded its definition
381 % of \texttt{pdftitle} will be used -- at some time probably this
382 % should be moved out of hyperref so that we have only one definition.
383 %
384 % \begin{macrocode}
385 \RenewDocumentCommand\title{O{}m}
386 {
387 \gdef\@title{#2}
388 \tl_gset_eq:NN\g_@@_title_title_tl\@title
389 \keys_set:nn {hyp}{#1}
390 }
391 % \end{macrocode}
392 % Now we define the \texttt{pdftitle} key. This is more or less
393 % the same definition as in the generic hyperref driver.
394 % \begin{macrocode}
395 \regex_new:N\l_@@_title_optlang_regex
396 \regex_set:Nn\l_@@_title_optlang_regex {\A\[([A-Za-z\-]+)\](.*)}
397 \cs_generate_variant:Nn \clist_item:nn {on}
398 % \end{macrocode}
399 % and now the keys.
400 % \begin{macrocode}
401 \keys_define:nn { hyp }
402 {
403 pdftitle .code:n =
404 {
405 \tl_if_blank:nTF {#1}
406 {
407 \pdfmanagement_remove:nn {Info}{Title}
408 }
409 {
410 \tl_set:Ne\l_@@_title_tmpa_tl {\clist_item:on{#1}{1}}
411 \regex_extract_once:NVN
412 \l_@@_title_optlang_regex
413 \l_@@_title_tmpa_tl
414 \l_@@_title_tmpa_seq
415 \seq_if_empty:NTF\l_@@_title_tmpa_seq
416 {
417 \@@_title_pdfstring:nnN {#1}{utf16/hex}\l_@@_title_tmpa_str
418 }
419 {
420 \@@_title_pdfstring:enN
421 {\seq_item:Nn \l_@@_title_tmpa_seq{3}}{utf16/hex}\l_@@_title_tmpa_str
422 }
423 \str_if_eq:VnF\l_@@_title_tmpa_str{<FEFF>}
424 {
425 \pdfmanagement_add:nne {Info}{Title}{\l_@@_title_tmpa_str}
426 }
427 }
428 \AddToDocumentProperties[hyperref]{pdftitle}{#1}
429 }
430 ,pdfsubtitle .code:n = { \AddToDocumentProperties[hyperref]{pdfsubtitle}{#1} }
431 }
432 % \end{macrocode}
433 %
434 % \subsection{Extend \cs{author} to set metadata}
435 %
436 % At first a variable to store the authors, as \cs{@author} is emptied by \LaTeX.
437 % \begin{macrocode}
438 \tl_new:N \g_@@_title_author_tl
439 % \end{macrocode}
440 % Now we redefine \cs{author} so that it stores the authors, and processes
441 % keys in the optional argument. We use \texttt{hyp} as module
442 % name for the key as this means that if hyperref is loaded its definition
443 % of \texttt{pdfauthor} will be used -- at some time probably this
444 % should be moved out of hyperref so that we have only one definition.
445 %
446 % \begin{macrocode}
447 \RenewDocumentCommand\author{O{}m}
448 {
449 \gdef\@author{#2}
450 \tl_gset_eq:NN\g_@@_title_author_tl\@author
451 \keys_set:nn {hyp}{#1}
452 }
453 % \end{macrocode}
454 %
455 % Now we define the \texttt{pdfauthor} key. This is more or less
456 % the same definition as in the generic hyperref driver.
457
458 % \begin{macrocode}
459 \keys_define:nn { hyp }
460 {
461 pdfauthor .code:n =
462 {
463 \tl_if_blank:nTF {#1}
464 {
465 \pdfmanagement_remove:nn {Info}{Author}
466 }
467 {
468 \tl_set:Ne\l_@@_title_tmpa_tl {\clist_item:on{#1}{1}}
469 \regex_extract_once:NVN
470 \l_@@_title_optlang_regex
471 \l_@@_title_tmpa_tl
472 \l_@@_title_tmpa_seq
473 \seq_if_empty:NTF\l_@@_title_tmpa_seq
474 {
475 \@@_title_pdfstring:nnN {#1}{utf16/hex}\l_@@_title_tmpa_str
476 }
477 {
478 \@@_title_pdfstring:enN
479 {\seq_item:Nn \l_@@_title_tmpa_seq{3}}{utf16/hex}\l_@@_title_tmpa_str
480 }
481 \str_if_eq:VnF\l_@@_title_tmpa_str{<FEFF>}
482 {
483 \pdfmanagement_add:nne {Info}{Author}{\l_@@_title_tmpa_str}
484 }
485 }
486 \AddToDocumentProperties[hyperref]{pdfauthor}{#1}
487 }
488 }
489 % \end{macrocode}
490 % \subsection{Fallback for classes and packages that redefine \cs{title} or \cs{author}}
491 % If a class redefines \cs{author} and \cs{title} again, we try to retrieve at
492 % least the values.
493 % \begin{macrocode}
494 \AddToHook{cmd/maketitle/before}
495 {
496 \tl_gset_eq:NN \g_@@_title_author_tl\@author
497 \tl_gset_eq:NN \g_@@_title_title_tl\@title
498 }
499 % \end{macrocode}
500 %
501 % \subsection{Finalize document}
502 % At last we set the title and the author at the end of document
503 % if that hasn't happened yet:
504 % \begin{macrocode}
505 \AddToHook{shipout/lastpage}
506 {
507 \tl_if_empty:eT{\GetDocumentProperties{hyperref/pdftitle}}
508 {
509 \group_begin:
510 \cs_set_eq:NN\thanks \use_none:n
511 \str_set:Ne \l_@@_title_tmpa_str {\text_purify:n { \g_@@_title_title_tl } }
512 \keys_set:ne{hyp}{pdftitle={\exp_not:V\l_@@_title_tmpa_str}}
513 \group_end:
514 }
515 \tl_if_empty:eT{\GetDocumentProperties{hyperref/pdfauthor}}
516 {
517 \group_begin:
518 \cs_set_eq:NN\thanks \use_none:n
519 \cs_set:Npn \and {,}
520 \str_set:Ne \l_@@_title_tmpa_str {\text_purify:n { \g_@@_title_author_tl } }
521 \keys_set:ne{hyp}{pdfauthor={\exp_not:V\l_@@_title_tmpa_str}}
522 \group_end:
523 }
524 % \end{macrocode}
525 % force display title, if an UA-standard is detected.
526 % \begin{macrocode}
527 \tl_if_empty:eF{\GetDocumentProperties{document/pdfstandard-UA}}
528 {
529 \pdfmanagement_add:nnn {Catalog / ViewerPreferences } { DisplayDocTitle } { true }
530 }
531 }
532 \DeclareHookRule{shipout/lastpage}{latex-lab-testphase-title}{before}{pdfmanagement-testphase}
533 %</package>
534 % \end{macrocode}
535
536 % \begin{macrocode}
537 %<*latex-lab>
538 \ProvidesFile{title-latex-lab-testphase.ltx}
539 [\ltlabtitledate\space v\ltlabtitleversion\space
540 Changes related to the tagging of the title]
541
542 \RequirePackage{latex-lab-testphase-title}
543
544 %</latex-lab>
545 % \end{macrocode}