Debian Codegraph @

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

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