Debian Codegraph @

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

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