Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-marginpar.dtx
4 %%
5 % Copyright (C) 2024-2025 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 % https://www.latex-project.org/lppl.txt
13 %
14 %
15 % The development version of the bundle can be found below
16 %
17 % https://github.com/latex3/latex2e/required/latex-lab
18 %
19 % for those people who are interested or want to report an issue.
20 %
21 \def\ltlabmarginpardate{2025-07-23}
22 \def\ltlabmarginparversion{0.85h}
23
24 %<*driver>
25 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
26 \documentclass[kernel]{l3doc}
27 \usepackage{latex-lab-testphase-l3doc}
28 \EnableCrossrefs
29 \CodelineIndex
30 \begin{document}
31 \DocInput{latex-lab-marginpar.dtx}
32 \end{document}
33 %</driver>
34 %
35 % \fi
36 %
37 % \title{The \textsf{latex-lab-marginpar} package\\
38 % Changes related to the tagging of margin notes}
39 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
40 % \date{v\ltlabmarginparversion\ \ltlabmarginpardate}
41 %
42 % \maketitle
43 %
44 % \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
45 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
46 % \newcommand{\docclass}{document class \marginpar{\raggedright document class
47 % customizations}}
48 %
49 % \providecommand\hook[1]{\texttt{#1\DescribeHook[noprint]{#1}}}
50 % \providecommand\socket[1]{\texttt{#1\DescribeSocket[noprint]{#1}}}
51 % \providecommand\plug[1]{\texttt{#1\DescribePlug[noprint]{#1}}}
52 %
53 % \NewDocElement[printtype=\textit{socket},idxtype=socket,idxgroup=Sockets]{Socket}{socketdecl}
54 % \NewDocElement[printtype=\textit{hook},idxtype=hook,idxgroup=Hooks]{Hook}{hookdecl}
55 % \NewDocElement[printtype=\textit{plug},idxtype=plug,idxgroup=Plugs]{Plug}{plugdecl}
56 %
57 % \begin{abstract}
58 % \end{abstract}
59 %
60 % \section{Introduction}
61 %
62 % This module contains changes to improve the tagging (in the standard classes) of
63 % margin notes created with the \cs{marginpar} command.
64 %
65 % Such margin notes are rather small but nevertheless tagging is not trivial and poses
66 % a number of interesting problems both regarding the structure and the implementation.
67 %
68 % \subsection{Structure}
69 % \cs{marginpar} creates small boxes in the margin of a page.
70 % While they are technically floats they also typically
71 % relate to the paragraph beside them, so their structure element should be placed near
72 % such a paragraph.
73 %
74 % They can be tagged either as artifacts (if they are merely distracting decoration),
75 % as small headings before the paragraph, or as |Aside|. Unlike the PDF 1.7 fallback |Note|
76 % the structure |Aside| is not allowed inside |P|,
77 % so if |Aside| is used, it must be placed before or after the current |P| in the surrounding |text-unit|,
78 % or it must split the |P|. Splitting is probably not so good as
79 % |\marginpar| is often used somewhere in the middle of sentence.
80 % The best default is probably to use |Aside|, find the parent |text-unit| and add it there.
81 %
82 % \subsection{Implementation}
83 %
84 % |marginpar| has an optional argument which allows to define a different content
85 % on left/right margins for odd/even pages (depending on twoside and \cs{reversemarginpar}).
86 %
87 % The current implementation stores *both* arguments in boxes and
88 % decides in the output routine which
89 % one to use. This is quite problematic for tagging, as the unused box produces
90 % structure objects, literals, labels, MCID numbers,
91 % and perhaps OBJR-objects from links
92 % which must be later thrown away again (or are thrown away by the engine).
93 % While it is theoretically not impossible to create
94 % both boxes while taging is active but to keep everything on hold and insert
95 % the real structure only when the box is used it is in practice quite difficult,
96 % slow and error prone.
97 %
98 % There are a number of options to avoid this hassle.
99 %
100 % \begin{description}
101 % \item[Minimal tagging] One option is to disable tagging when the boxes
102 % are built and when the box is used to surround them with a simple Aside.
103 %
104 % \begin{description}
105 % \item[Pro:] easy to implement
106 % \item[Contra:]
107 % Content that perhaps needed tagging (|\LaTeX| logo with /ActualText, images, links) is not tagged.
108 % \end{description}
109 %
110 % \item[Remaking the box]
111 % Tagging is deactivated when the boxes are created.
112 % The arguments are stored and when the box should be used in the output routine
113 % (and when it is known which one will be used) the box is (re)created, now with proper tagging.
114 %
115 % Contra: The content can change e.g. if it uses counters or macros that are redefine later before the
116 % output routine is called. As tagging is done later, it is not trivial to insert the
117 % |Aside| in the right place in the paragraph structure.
118 %
119 % \item[Two pass compilation]
120 % A label is used to detect which argument/boxed is used and
121 % tagging is activated only for this one.
122 %
123 % Contra: costs a |label| and requires a two pass compilation (which is needed anyway).
124 % This should be ok. |\marginnote| uses a label too, and the same
125 % label could also be used to resolve problems if a margin note
126 % moves to a new page (and so replace the mparhack package).
127 % \end{description}
128 %
129 % The last option seems the best and is therefore implemented.
130 %
131 %
132 % \section{Implementation}
133 % \begin{macrocode}
134 %<*package>
135 %<@@=tag>
136 % \end{macrocode}
137 % \begin{macrocode}
138 \ProvidesExplPackage {latex-lab-testphase-marginpar} {\ltlabmarginpardate} {\ltlabmarginparversion}
139 {Changes related to the tagging of the margin notes}
140 % \end{macrocode}
141 % \subsection{Variables}
142 % We need a variable to make the label unique.
143 % Todo: Not sure about the name.
144 % \begin{macro}{\g__kernel_marginpar_int}
145 % \begin{macrocode}
146 \int_new:N \g__kernel_marginpar_int
147 % \end{macrocode}
148 % \end{macro}
149 %
150 % \subsection{Tagging sockets}
151 %
152 % \begin{socketdecl}{tagsupport/marginpar/begin,tagsupport/marginpar/end}
153 % The sockets are declared in lttagging.
154 % \changes{0.85e}{2025/02/21}{moved sockets into lttagging}
155 % \begin{macrocode}
156 %\socket_new:nn {tagsupport/marginpar/begin}{0}
157 %\socket_new:nn {tagsupport/marginpar/end}{0}
158 % \end{macrocode}
159 % \end{socketdecl}
160 %
161 % \begin{macro}{\@@_get_marginpar_parent:N,\@@_get_marginpar_parent_aux:nn}
162 % In a paragraph we must retrieve
163 % the structure number of the surrounding Part structure
164 % TODO: if this is needed in more places this should
165 % go into tagpdf.
166 % \changes{v0.85h}{2025/07/23}{Use Sect as parent, if there is no Part/text-unit}
167 % \begin{macrocode}
168 \cs_new_protected:Npn\@@_get_marginpar_parent_aux:nn #1 #2
169 {
170 \str_if_eq:eeT{\use_ii:nn #1}{Part}{\seq_put_right:Nn\l_@@_tmpa_seq {#2}}
171 \str_if_eq:eeT{\use_ii:nn #1}{Sect}{\seq_put_right:Nn\l_@@_tmpa_seq {#2}}
172 }
173 \cs_new_protected:Npn \@@_get_marginpar_parent:N #1
174 {
175 \seq_clear:N\l_@@_tmpa_seq
176 \seq_map_pairwise_function:NNN
177 \g_@@_struct_tag_stack_seq
178 \g_@@_struct_stack_seq
179 \@@_get_marginpar_parent_aux:nn
180 \seq_put_right:Nn\l_@@_tmpa_seq {2}
181 \seq_get_left:NN \l_@@_tmpa_seq #1
182 }
183 \cs_generate_variant:Nn \tag_struct_begin:n {e}
184 % \end{macrocode}
185 % \end{macro}
186 % \begin{plugdecl}{tagsupport/marginpar/begin,tagsupport/marginpar/end}
187 % \begin{macrocode}
188 \NewTaggingSocketPlug{marginpar/begin}{default}
189 {
190 \if_mode_horizontal:
191 \tag_mc_end:
192 \@@_get_marginpar_parent:N \l_@@_tmpa_tl
193 \tag_struct_begin:e{tag=Aside,parent=\l_@@_tmpa_tl}%
194 \else:
195 \tag_struct_begin:n{tag=Aside}%
196 \fi:
197 }
198 \NewTaggingSocketPlug{marginpar/end}{default}
199 {
200 \tag_struct_end:
201 \if_mode_horizontal: \tag_mc_begin:n{} \fi:
202 }
203 \AssignTaggingSocketPlug{marginpar/begin}{default}
204 \AssignTaggingSocketPlug{marginpar/end}{default}
205 % \end{macrocode}
206 % \end{plugdecl}
207
208 % \subsection{Kernel command changes}
209 % \begin{macro}[no-user-doc]{\@savemarbox }
210 % We add sockets that add a tagging structure Aside:
211 % \begin{macrocode}
212 \long\def \@savemarbox #1#2{%
213 \UseTaggingSocket{marginpar/begin}
214 \global\setbox #1%
215 \color@vbox
216 \vtop{%
217 \hsize\marginparwidth
218 \@parboxrestore
219 \@marginparreset
220 #2\par
221 \@minipagefalse
222 \outer@nobreak
223 }%
224 \color@endbox
225 \UseTaggingSocket{marginpar/end}
226 }
227 % \end{macrocode}
228 % \end{macro}
229 %
230 % \begin{macro}[no-user-doc]{\@ympar}
231 % We must avoid that \cs{@xympar} creates tagging structure:
232 % \begin{macrocode}
233 \long\def\@ympar#1{%
234 \@savemarbox\@marbox{#1}%
235 \global\setbox\@currbox\copy\@marbox
236 \tag_suspend:n{\@ympar}
237 \@xympar
238 \tag_resume:n{\@ympar}}
239 % \end{macrocode}
240 % \end{macro}
241 %
242 % \begin{macro}[no-user-doc]{\@xmpar}
243 % \cs{@xmpar} is the command used if an optional argument is present.
244 % In this case we must stop tagging for \cs{@xympar} as above
245 % but also decide which of the two @savemarbox should have tagging active.
246 % We add a label to \cs{@marbox}. If that exist tagging is activated for this
247 % box else for the other one.
248 % \begin{macrocode}
249 \long\def\@xmpar[#1]#2{%
250 \int_gincr:N\g__kernel_marginpar_int
251 \property_if_recorded:eTF { tag_marginpar-opt-\int_use:N\g__kernel_marginpar_int }
252 {
253 \@savemarbox\@marbox {#1
254 \property_record:ee
255 { tag_marginpar-opt-\int_use:N\g__kernel_marginpar_int }{page}
256 }%
257 \tag_suspend:n{\@xmpar}
258 \@savemarbox\@currbox{#2}%
259 \tag_resume:n{\@xmpar}
260 }
261 % \end{macrocode}
262 % order matters! the tagged box should be first so that it can pick up
263 % the correct text-unit number.
264 % \begin{macrocode}
265 {
266 \@savemarbox\@currbox{#2}%
267 \tag_suspend:n{\@xmpar}
268 \@savemarbox\@marbox{#1
269 \property_record:ee
270 { tag_marginpar-opt-\int_use:N\g__kernel_marginpar_int }{page}}
271 \tag_resume:n{\@xmpar}
272 }
273 \tag_suspend:n{\@xmpar}
274 \@xympar
275 \tag_resume:n{\@xmpar}
276 }
277 %</package>
278 % \end{macrocode}
279 % \end{macro}
280 %
281 % \begin{macrocode}
282 %<*latex-lab>
283 \ProvidesFile{marginpar-latex-lab-testphase.ltx}
284 [\ltlabmarginpardate\space v\ltlabmarginparversion\space
285 Changes related to the tagging of the margin notes]
286
287 \RequirePackage{latex-lab-testphase-marginpar}
288
289 %</latex-lab>
290 % \end{macrocode}