Debian Codegraph @

texmf-dist/source/latex-dev/base/ltmarks.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: ltmarks.dtx
4 %% Copyright (C) 2022-2026
5 %% Frank Mittelbach, The 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 %%% From File: ltmarks.dtx
16 %
17 %<*driver>
18 % \fi
19 \ProvidesFile{ltmarks.dtx}
20 [2026-03-23 v1.1f LaTeX Kernel (Marks)]
21 % \iffalse
22 %
23 \documentclass{l3in2edoc}
24 \GetFileInfo{ltmarks.dtx}
25
26 \EnableCrossrefs
27 \CodelineIndex
28 \begin{document}
29 \DocInput{ltmarks.dtx}
30 \end{document}
31 %</driver>
32 %
33 % \fi
34 %
35 % \providecommand\hook[1]{\texttt{#1}}
36 % \providecommand\env[1]{\texttt{#1}}
37 %
38 %
39 %
40 % \title{The marks mechanism\thanks{This file has version
41 % \fileversion\ dated \filedate, \copyright\ \LaTeX\
42 % Project.}}
43 % \author{^^A
44 % Frank Mittelbach, \LaTeX{} Project\thanks
45 % {^^A
46 % E-mail:
47 % \href{mailto:[email protected]}
48 % {[email protected]}^^A
49 % }^^A
50 % }
51 %
52 % \maketitle
53 %
54 %
55 % \begin{abstract}
56 % Marks are used to communicate information about the content of a
57 % page to the output routine. For example, in order to construct
58 % running headers, the output routine needs information about which
59 % section names are present on a page, and this information is
60 % passed to it through the mark system. However, marks may also be
61 % used for other purposes. This module provides a generalized
62 % mechanism for marks of independent classes.
63 % \end{abstract}
64 %
65 % \tableofcontents
66 %
67 % ^^A \begin{documentation}
68 %
69 % \section{Introduction}
70 %
71 % The \TeX{} engines offer a low-level mark mechanism to
72 % communicate information about the content of the current page to
73 % the asynchronous operating output routine. It works by placing
74 % \cs{mark} commands into the source document. When the material
75 % for the current page is assembled in box 255, \TeX{} scans for
76 % such marks and sets the commands \cs{topmark}, \cs{firstmark} and
77 % \cs{botmark}. The \cs{firstmark} receives the content of the
78 % first \cs{mark} seen in box 255 and \cs{botmark} the content of
79 % the last mark seen. The \cs{topmark} holds the content of the
80 % last mark seen on the previous page or more exactly the value of
81 % \cs{botmark} from the previous page. If there are no marks on
82 % the current page then all three are made equal to the
83 % \cs{botmark} from the previous page.
84 %
85 % This mechanism works well for simple formats (such as plain \TeX)
86 % whose output routines are only called to generate pages. It
87 % fails, however, in \LaTeX{} (and other more complex formats),
88 % because here the output routine is sometimes called without
89 % producing a page, e.g., when encountering a float and placing it
90 % into one of the float regions. In that case the output routine is
91 % called, determines where to place the float, alters the goal for
92 % assembling text material (if the float was added to the top or
93 % bottom region) and then it resumes collecting textual material.
94 %
95 % As a result the \cs{botmark} gets updated and so \cs{topmark} no
96 % longer reflects the situation at the top of the next page when that
97 % page is finally boxed.
98 %
99 % Another problem for \LaTeX{} was that it wanted to use several
100 % \enquote{independent} marks and in the early implementations of
101 % \TeX{} there was only a single \cs{mark} command available.
102 % For that reason \LaTeX{} implemented its own mark
103 % mechanism where the marks always contained two parts with their
104 % own interfaces: \cs{markboth} and \cs{markright} to set marks and
105 % \cs{leftmark} and \cs{rightmark} to retrieve them.
106 %
107 % However, this extended mechanism (while supporting scenarios such
108 % as chapter/section marks) was far from general. The mark
109 % situation at the top of a page (i.e., \cs{topmark}) remained
110 % unusable and the two marks offered were not really independent of
111 % each other because \cs{markboth} (as the name indicates) was
112 % always setting both.
113 %
114 % The new mechanism overcomes both
115 % issues:
116 % \begin{itemize}
117 % \item
118 % It provides arbitrarily many, fully independent named marks, that
119 % can be allocated and, from that point onwards, used.
120 % \item
121 % It offers access for each such marks to retrieve its top,
122 % first, and bottom values separately.
123 % \item
124 % Furthermore, the mechanism is augmented to give access to marks
125 % in different \enquote{regions} which may not be just full pages.
126 % \end{itemize}
127 %
128 %
129 % \section{Design-level and code-level interfaces}
130 %
131 % The interfaces are mainly meant for package developers, but they
132 % are usable (with appropriate care) also in the document
133 % preamble, for example, when setting up special running headers
134 % with \pkg{fancyhdr}, etc. They are therefore available both as
135 % CamelCase commands as well as commands for use in the L3
136 % programming layer. Both are described together below.
137 %
138 % \begin{function}{\NewMarkClass,\mark_new_class:n}
139 % \begin{syntax}
140 % \cs{NewMarkClass} \Arg{class}
141 % \cs{mark_new_class:n} \Arg{class}
142 % \end{syntax}
143 % Declares a new \meta{class} of marks to be tracked by \LaTeX{}. Each \meta{class}
144 % must be declared before it is used.
145 %
146 % Mark classes can only be declared before \verb=\begin{document}=.
147 % \end{function}
148 %
149 % \begin{function}{\InsertMark,\mark_insert:nn}
150 % \begin{syntax}
151 % \cs{InsertMark} \Arg{class} \Arg{text}
152 % \cs{mark_insert:nn} \Arg{class} \Arg{text}
153 % \end{syntax}
154 % Adds a mark to the current galley for the \meta{class}, containing the
155 % \meta{text}.
156 %
157 % It has no effect in places in which you can't place floats, e.g.,
158 % a mark inside a box or inside a footnote never shows up anywhere.
159 %
160 % If used in vertical mode it obeys \LaTeX's internal
161 % \texttt{@nobreak} switch, i.e., it does not introduce a
162 % breakpoint if used after a heading. If used in horizontal mode it
163 % doesn't handle spacing (like, for example, \cs{index} or
164 % \cs{label} does, so it should be attached to material that is
165 % typeset.
166 % \end{function}
167 %
168 % \begin{variable}{insertmark}
169 % \begin{syntax}
170 % \cs{AddToHook} \texttt{\{insertmark\}} \Arg{code}
171 % \end{syntax}
172 % When marks are inserted, the mark content may need some special
173 % treatment, e.g., by default \cs{label}, \cs{index}, and
174 % \cs{glossary} do not expand at this time (but only later if and when the
175 % mark content is actually used.
176 % In order to allow packages to augment or alter this setup there is
177 % a public hook \hook{insertmark} that is executed at this point. It
178 % runs in a group so local modification to commands are only applied
179 % to the \meta{text} argument of \cs{InsertMark} or \cs{mark_insert:nn}.
180 % \end{variable}
181 %
182 % \begin{function}[EXP]{\TopMark, \FirstMark, \LastMark,
183 % \mark_use_top:nn,\mark_use_first:nn,\mark_use_last:nn,}
184 % \begin{syntax}
185 % \cs{TopMark} \oarg{region} \Arg{class}
186 % \cs{FirstMark} \oarg{region} \Arg{class}
187 % \cs{LastMark} \oarg{region} \Arg{class}
188 % \cs{mark_use_top:nn} \Arg{region} \Arg{class}
189 % \cs{mark_use_first:nn} \Arg{region} \Arg{class}
190 % \cs{mark_use_last:nn} \Arg{region} \Arg{class}
191 % \end{syntax}
192 % These functions expand to the appropriate mark \meta{text} for
193 % the given \meta{class} in the specified \meta{region}.
194 % The default \meta{region} in the design-level commands is \texttt{page}.
195 % Note that with the L3 layer commands there are no
196 % optional arguments, i.e., both arguments have to be provided.
197 % \begin{texnote}
198 % The result is returned within the \tn{unexpanded}
199 % primitive (\cs{exp_not:n}), which means that the \meta{text}
200 % does not expand further when appearing in an \texttt{x}-type
201 % or \texttt{e}-type argument expansion.
202 % \end{texnote}
203 %
204 % The \enquote{first} and \enquote{last} marks are
205 % those seen first and last in the current region/page, respectively. The
206 % \enquote{top} mark is the last mark of the \meta{class} seen
207 % in an earlier region, i.e., the \meta{text} what would be \enquote{current} at the
208 % very top of the region.
209 %
210 % \noindent\llap{\bfseries Important!\qquad}\indent
211 % The commands are only meaningful inside the output routine, in
212 % other places their result is (while not random) unpredictable due
213 % to the way \LaTeX{} cuts text material into pages.
214 % There is, however, one exception: if you produce multiple columns
215 % using the \pkg{multicol} package, it is possible to retrieve mark
216 % values from the regions \texttt{first-column},
217 % \texttt{last-column}, \texttt{mcol-1},
218 % \texttt{mcol-2},\ldots\ directly after the environment has
219 % ended. This can, for example, be useful if a \env{multicols} has
220 % been be used inside a box.
221 % \end{function}
222 %
223 %
224 %
225 % Currently, \meta{region} is one of
226 % \texttt{page},
227 % \texttt{previous-page},
228 % \texttt{column},
229 % \texttt{previous-column},
230 % \texttt{first-column},
231 % \texttt{last-column}, and
232 % \texttt{mcol-1} (first column in a \env{multicols}),
233 % \texttt{mcol-2} (second column in a \env{multicols}),
234 % up to
235 % \texttt{mcol-20} (twentieth column in a \env{multicols}).
236 % See section~\ref{sec:regions} for discussion of how these regions
237 % behave and how one can make use of them.
238 %
239 %
240 % \begin{function}[EXP]{\IfMarksEqualTF,\IfMarksEqualT,\IfMarksEqualF,\mark_if_eq:nnnnTF,\mark_if_eq:nnnnnnTF}
241 % \begin{syntax}
242 % \cs{IfMarksEqualTF} \oarg{region} \Arg{class} \Arg{pos_1} \Arg{pos_2} \Arg{true} \Arg{false}
243 % \cs{mark_if_eq:nnnnTF} \Arg{region} \Arg{class} \Arg{pos_1} \Arg{pos_2} \Arg{true} \Arg{false}
244 % \cs{mark_if_eq:nnnnnnTF} \Arg{region_1} \Arg{class_1} \Arg{pos_1}
245 % \verb= = \Arg{region_2} \Arg{class_2} \Arg{pos_2} \Arg{true} \Arg{false}
246 % \end{syntax}
247 % These conditionals allow you to compare the content of two marks
248 % and act based on the result. The commands work in an expansion
249 % context, if necessary.
250 % \end{function}
251 %
252 % It is quite common when programming with marks to need to
253 % interrogate conditions such as whether marks have appeared on a
254 % previous page, or if there are multiple marks present on the
255 % current page, and so on.
256 % The tests above allow for the construction of a variety of
257 % typical test scenarios, with three examples presented below.
258 %
259 % The first two conditionals cover only the common scenarios. Both
260 % marks are picked up from the same \meta{region} (by default
261 % \texttt{page}) and they have to be of the same
262 % \meta{class}.\footnote{If an undeclared mark class is used the
263 % tests return \emph{true} (not an error).}
264 % The \meta{pos\textsubscript{\itshape i}} argument can be either
265 % \texttt{top}, \texttt{first}, or \texttt{last}.
266 %
267 % Important to note is that the comparison is not with respect to
268 % the textual content of the marks but whether or not they
269 % originated from the same \cs{InsertMark} command (or the L3 layer
270 % version \cs{mark_insert:nn}).
271 %
272 % If you wish to compare marks across different regions or across
273 % different classes, you have to do it using the generic test only
274 % available in the L3 programming layer or do it manually, i.e.,
275 % get the marks and then compare the values yourself.\footnote{If
276 % two undeclared mark classes are compared the result is always
277 % \emph{true}; if a declared and an undeclared mark class is used
278 % it is always \emph{false}.}
279 %
280 % \subsection{Use cases for conditionals}
281 %
282 % However, the basic version is enough for the following typical use cases:
283 % \begin{description}
284 % \item[Test for at most one mark of class \texttt{myclass} on current
285 % page:]
286 %
287 % If the first and last mark in a region are the same then
288 % either there was no mark at all, or there was at most one. To test
289 % this on the current page:
290 %\begin{verbatim}
291 % \NewMarkClass{myclass}
292 % \IfMarksEqualTF{myclass}{first}{last}
293 % { <zero or one mark> }{ <two or more marks> }
294 %\end{verbatim}
295 %
296 % \item[Test for no mark of class \texttt{myclass} in the previous
297 % page:]
298 %
299 % If the top mark is the same as the first mark, there is no mark
300 % in the region at all. If we wanted to do this test for the
301 % previous page:
302 %\begin{verbatim}
303 % \IfMarksEqualTF[previous-page]{myclass}{top}{first}
304 % { <no marks> }{ <at least one mark> }
305 %\end{verbatim}
306 % Comparing \texttt{top} and \texttt{last} would give you the
307 % same result.
308 %
309 % \item[Test for zero, one, or more than one:]
310 %
311 % Combining the two tests from above you can test for zero, one
312 % or more than one mark.
313 %\begin{verbatim}
314 % \IfMarksEqualTF{myclass}{top}{first}
315 % { <no marks> }
316 % {\IfMarksEqualTF{myclass}{first}{last}
317 % { <exactly one mark> }{ <more than one mark> }}
318 %\end{verbatim}
319 %
320 % \end{description}
321 %
322 % If you need one of such tests more often (or if you want a separate
323 % command for it for readability), then consider defining:
324 %\begin{verbatim}
325 % \providecommand\IfNoMarkTF[2][page]{\IfMarksEqualTF[#1]{#2}{first}{last}}
326 %\end{verbatim}
327 %
328 %
329 %
330 % \subsection{Understanding regions}
331 % \label{sec:regions}
332 %
333 % If a page has just been finished then the region \texttt{page}
334 % refers to the current page and \texttt{previous-page}, as the name
335 % indicates, refers to the page before the current page. This
336 % means you are able to access mark information for the current page
337 % as well as for the page before (as long as you are inside the output
338 % routine) without the need to explicitly save that information
339 % beforehand. The \texttt{page} region is the region that is most
340 % often queried, which is why commands like \cs{FirstMark} use that
341 % region by default.
342 %
343 % In single column documents the \texttt{column} is the same as the
344 % \texttt{page} region, but in two-column documents (if not produced
345 % by \env{multicols}), \texttt{column} refers to the current column
346 % that just got finished and \text{previous-column} to the one
347 % previously finished. Code for running headers is (in standard
348 % \LaTeX{}) evaluated only after both columns have been assembled, which is
349 % another way of saying that in that case \texttt{previous-column}
350 % refers to the left column and \texttt{column} to the right column.
351 % However, to make these somewhat easier to use, there are also aliased names
352 % for these two regions: \texttt{first-column} and
353 % \texttt{last-column}.\footnote{The region is called \enquote{last-column} not
354 % \enquote{second-column} in anticipation of extending the mechanism to
355 % multiple columns, where first and last would still make sense.
356 % There aren't any
357 % \texttt{previous-first-column} and \texttt{previous-last-column}
358 % regions to access the corresponding
359 % columns from the previous page.}
360 %
361 % Note that you can only look backwards at already processed
362 % regions, e.g., in a \texttt{twoside} document finishing a recto
363 % (odd, right-hand) page you can access the data from the facing
364 % verso (left-hand) page, but if you are finishing a left-hand page
365 % you can't integrate data from the upcoming right-hand page. If
366 % such a scenario needs to be realized then it is necessary to save
367 % the left-hand page temporarily instead of finalizing it, process
368 % material for the right-hand page and once both are ready, attach
369 % running headers and footers and shipout out both in one
370 % go.\footnote{As of now that scenario is not (yet) officially
371 % supported but it would be possible to achieve this using the
372 % shipout hooks to store the verso page and then on the next shipout
373 % use the hook to shipout both with running headers and footers
374 % attached.}
375 %
376 % The situation starts getting rather complex if you allow for
377 % multiple columns in the way they are supported by the
378 % \pkg{multicol} package. In this case you might have a variable
379 % number of \enquote{columns} on a single page to be shipped
380 % out. And even if not, then a \env{multicols} might start or end in
381 % the middle of the page; in either case, the regions \texttt{column} and
382 % \texttt{previous-column} become rather meaningless and you should
383 % therefore not use them.\footnote{They return something, because
384 % they represent the last two columns of the \env{multicols} when
385 % you are inside the output routine, but that is obviously of little
386 % use.}
387 % Instead, the algorithm offers \texttt{mcol-1}, \texttt{mcol-2},
388 % \texttt{mcol-3}, etc., to represent the columns in the
389 % \env{multicols} on the current page to be shipped out. If there is
390 % more than one \env{multicols} on the current page then in the output routine
391 % only the columns of the last one will be accessible.
392 %
393 % These provisions cover, out of the box, a number of layouts and use cases, but
394 % obviously not all. However, more cases can be supported by storing away
395 % mark information during the processing. Here is the full
396 % algorithm:
397 % \begin{itemize}
398 % \item
399
400 % The \texttt{column} region is used by the \enquote{current
401 % column} that is being built (moving through all columns with
402 % \texttt{previous-column} trailing behind (to handle top marks
403 % properly).
404 %
405 % \item
406
407 % When the \env{multicols} starts, the \texttt{column} region is
408 % cleared, i.e., from that point on it looks as if there have not
409 % been any marks so far. This will make sure that the top mark in
410 % the first column is always empty.
411 %
412 % \item
413 %
414 % If the \env{multicols} extends beyond the current page, then the
415 % material designated for the current page is split into columns.
416 % The \texttt{column} region is used to represent each column in
417 % turn.
418
419 % \begin{itemize}
420 % \item
421 %
422 % First we copy the current data from \texttt{column} to
423 % \texttt{previous-column}. Then the mark data from the current
424 % column is placed into the \texttt{column} region. Then we alias
425 % \texttt{column} to \texttt{mcol-1}.
426 %
427 % \item These steps are repeated for all columns of the \env{multicols}
428 % environment.
429 %
430 % \item Finally, the first and the last column of that page is also
431 % made available as \texttt{first-column} and
432 % \texttt{last-column}, respectively.
433 % \end{itemize}
434 %
435 % \item
436 % All those marks inside any of the columns are also available in the
437 % \texttt{page} region. Thus, if you are interested in the top,
438 % first, or last mark of a specific class on the whole page you
439 % simply need to query for it in the \texttt{page} region.
440 %
441 % \item
442 % If the \env{multicols} continues across several pages then
443 % this algorithm above is repeated for each page, except that the \texttt{column}
444 % region is not cleared again. This means that the top mark of
445 % the first column of the next page will be the last mark of the
446 % last column from the previous page.
447 %
448 % \item
449 % When the \env{multicols} finishes the remaining material for the
450 % current page is balanced to produce columns of roughly equal height.
451 %
452 % \item
453 % Again \texttt{column} and \texttt{previous-column} are used
454 % while this balancing happens and \texttt{mcol-1},
455 % \texttt{mcol-2}, etc., are used to represent the column
456 % regions and \texttt{first-column} and \texttt{last-column} are
457 % set appropriately.
458 %
459 % \item
460 % Then the balanced set of columns is returned back to the page
461 % (since there may be space for further material). In addition, all
462 % marks inside that material are reinserted so that they become
463 % available in the \texttt{page} region.
464 %
465 % \item As a side effect, it is possible (and useful in certain
466 % circumstances) to query for mark classes directly after the
467 % \env{multicols} has ended without the need to be inside the output
468 % routine. The regions that can be queried this way are
469 % \texttt{mcol-1}, \texttt{mcol-2}, etc.\ (up to the number of columns
470 % the multicol had) and \texttt{first-column} and \text{last-column}.
471 %
472 % \end{itemize}
473 %
474 %
475 %
476 % \subsection{Debugging mark code}
477 %
478 %
479 % \begin{function}{\DebugMarksOn,\DebugMarksOff,
480 % \mark_debug_on:,\mark_debug_off:}
481 % \begin{syntax}
482 % \cs{DebugMarksOn} \ldots\ \cs{DebugMarksOff}
483 % \end{syntax}
484 %
485 % Commands to turn the debugging of mark code on or off. The
486 % debugging output is rather coarse and not really intended for
487 % normal use at this point in time.
488 %
489 % \end{function}
490 %
491 %
492 %
493 % \section{Application examples}
494 %
495 % If you want to figure out if a break was taken at a specific point,
496 % e.g., whether a heading appears at the top of the page,
497 % you can do something like this:
498 %\begin{verbatim}
499 % \newcounter{breakcounter}
500 % \NewMarkClass{break}
501 % \newcommand\markedbreak[1]{\stepcounter{breakcounter}%
502 % \InsertMark{break}{\arabic{breakcounter}%
503 % \penalty #1\relax
504 % \InsertMark{break}{-\arabic{breakcounter}}
505 %\end{verbatim}
506 % To test if the break was taken you can test if
507 % \verb=\TopMark{break}= is positive (taken) or negative (not taken)
508 % or zero (there was never any marked break so far).
509 % The absolute value can be used to keep track of which break it
510 % was (with some further coding).
511 %
512 %
513 % \emph{to be extended with additional application examples}
514 %
515 %
516 %
517 % \section{Legacy \LaTeXe{} interface}
518 %
519 % Here we describe the interfaces that \LaTeXe{} offered since the
520 % early nineties and some minor extensions.
521 %
522 % \subsection{Legacy design-level and document-level interfaces}
523 %
524 % \begin{function}{\markboth, \markright}
525 % \begin{syntax}
526 % \cs{markboth} \Arg{left} \Arg{right}
527 % \cs{markright} \Arg{right}
528 % \end{syntax}
529 % \LaTeXe{} uses two marks which aren't fully independent. A
530 % \enquote{left} mark generated by the first argument of \cs{markboth}
531 % and a \enquote{right} mark generated by the second argument of
532 % \cs{markboth} or by the only argument of \cs{markright}. The
533 % command \cs{markboth} and \cs{markright} are in turn called from
534 % heading commands such as \cs{chaptermark} or \cs{sectionmark} and
535 % their behavior is controlled by the document class.
536 %
537 % For example, in the \cls{article} class with \texttt{twoside} in
538 % force the \cs{sectionmark} will issue \cs{markboth} with an empty
539 % second argument and \cs{subsectionmark} will issue
540 % \cs{markright}. As a result the left mark will contain chapter
541 % titles and the right mark subsection titles.
542 %
543 % Note, however, that in one-sided documents the standard behavior is
544 % that only \cs{markright} is used, i.e., there will only be
545 % right-marks but no left marks!
546 % \end{function}
547 %
548 % \begin{function}[EXP]{\leftmark, \rightmark}
549 % \begin{syntax}
550 % \cs{leftmark}
551 % \cs{rightmark}
552 % \end{syntax}
553 % These functions return the appropriate mark value from the current page
554 % and work as before, that is \cs{leftmark} will get the last (!)
555 % left mark from the page and \cs{rightmark} the first (!) right
556 % mark.
557 %
558 % In other words they work reasonably well if you want to show the
559 % section title that is current when you are about to turn the page and
560 % also show the first subsection title on the current page (or the last
561 % from the previous page if there wasn't one). Other combinations
562 % can't be shown using this interface.
563 %
564 % The commands are fully expandable, because this is how they have
565 % been always defined in \LaTeX{}. However, this is of course
566 % only true if the content of the mark they return is itself
567 % expandable and does not contain any fragile material. Given that
568 % this can't be guaranteed for arbitrary content, a programmer using
569 % them in this way should use \cs{protected@edef} and \emph{not}
570 % \cs{edef} to avoid bad surprises as far as this is possible, or use
571 % the new interfaces (\cs{TopMark}, \cs{FirstMark}, and \cs{LastMark})
572 % which return the \meta{text} in \cs{exp_not:n} to prevent
573 % uncontrolled expansion.
574 % \end{function}
575 %
576 %
577 % \subsection{Legacy interface extensions}
578 %
579 % The new implementation adds three mark classes: \texttt{2e-left},
580 % \texttt{2e-right} and \texttt{2e-right-nonempty} and patches
581 % \cs{markboth} and \cs{markright} slightly so that they also update
582 % these new mark classes, so that the new classes work with existing
583 % document classes.
584 %
585 % As a result you can use \verb=\LastMark{2e-left}= and
586 % \verb=\FirstMark{2e-right}= instead of \cs{leftmark} and
587 % \cs{rightmark}. But more importantly, you can use any of the other
588 % retrieval commands to get a different status value from those
589 % marks, e.g., \verb=\LastMark{2e-right}= would return the last
590 % subsection on the page (instead of the first as
591 % returned by \cs{rightmark}).
592 %
593 % The difference between \texttt{2e-right} and
594 % \texttt{2e-right-nonempty} is that the latter will only be updated
595 % if the material for the mark is not empty. Thus
596 % \verb=\markboth{title}{}= as issued by, say, \cs{sectionmark},
597 % sets a \texttt{2e-left} mark with \texttt{title} and a
598 % \texttt{2e-right} mark with the empty string but does not add a
599 % \texttt{2e-right-nonempty} mark.
600 %
601 % Thus, if you have a section at the start of a page and you would
602 % ask for \verb=\FirstMark{2e-right}= you would get an empty string
603 % even if there are subsections on that page. But
604 % \texttt{2e-right-nonempty} would then give you the first or last subsection
605 % on that page. Of course, nothing is simple. If there are no
606 % subsections it would tell you the last subsection from an earlier
607 % page. We therefore need comparison tools, e.g., if top and
608 % first are identical you know that the value is
609 % bogus, i.e., a suitable implementation would be
610 %\begin{verbatim}
611 % \IfMarksEqualTF{2e-right-nonempty}{top}{first}
612 % { <appropriate action if there was no real mark> }
613 % {\FirstMark{2e-right-nonempty}}
614 %\end{verbatim}
615 %
616 %
617 %
618 % \section{Notes on the mechanism}
619 %
620 % In contrast to vanilla \TeX, \eTeX{} extends the mark system to
621 % allow multiple independent marks. However, it does not solve the
622 % \cs{topmark} problem which means that \LaTeX{} still needs to manage
623 % marks almost independently of \TeX{}. The reason for this is that
624 % the more complex output routine used by \LaTeX{} to handle floats
625 % (and related structures) means that \tn{topmark(s)} remain
626 % unreliable. Each time the output routine is fired up, \TeX{} moves
627 % \tn{botmark} to \tn{topmark}, and while \eTeX{} extends this to
628 % multiple registers the fundamental concept remains the same. That
629 % means that the state of marks needs to be tracked by \LaTeX{}
630 % itself. An early implementation of this package used \TeX{}'s
631 % \tn{botmark} only to ensure the correct interaction with the output
632 % routine (this was before the \eTeX{} mechanism was even
633 % available). However, other than in a prototype implementation for
634 % \LaTeX3, this package was never made public.
635 %
636 % The new implementation now uses \eTeX{}'s marks as they have some
637 % advantages, because with them we can leave the mark text within the
638 % galley and only extract the marks during the output routine when we
639 % are finally shipping out a page or storing away a column for use in
640 % the next page. That means we do not have to maintain a global data
641 % structure that we have to keep in sync with informational marks in
642 % the galley but can rely on everything being in one place and thus
643 % manipulations (e.g.~reordering of material) will take the marks with
644 % them without a need for updating a fragile linkage.
645
646 % To allow for completely independent marks we use the following
647 % procedure:
648 % \begin{itemize}
649 % \item
650 %
651 % For every type of marks we allocate a mark class so
652 % that in the output routine \TeX{} can calculate for each class
653 % the current
654 % top, first, and bottom mark independently. For this we use
655 % \cs{newmarks}, i.e., one marks register per class.
656 %
657 % \item
658 %
659 % As already mentioned firing up an output routine without
660 % shipping out a page means that \TeX's top marks get wrong so it
661 % is impossible to rely on \TeX's approach directly. What we do
662 % instead is to keep track of the real marks (for the last page or
663 % more generally last region) in some global variables.
664 %
665 % \item
666 %
667 % These variables are updated in the output routine at defined
668 % places, i.e., when we do real output processing but not if we
669 % use special output routines to do internal housekeeping.
670 %
671 % \item
672 %
673 % The trick we use to get correctly updated variables is the
674 % following: the material that contains new marks (for example the
675 % page to be shipped out) is stored in a box. We then use \TeX{}
676 % primitive box splitting functions by splitting off the largest
677 % amount possible (which should be the whole box if nothing goes
678 % really wrong). While that seems a rather pointless thing to do, it
679 % has one important side effect: \TeX{} sets up first and bottom
680 % marks for each mark class from the material it has split off. This
681 % way we get the first and last marks (if there have been any) from
682 % the material in the box.
683 %
684 % \item
685 %
686 % The top marks are simply the last marks from the previous
687 % page or region. And if there hasn't been a first or bottom mark in
688 % the box then the new top mark also becomes new first and last mark
689 % for that class.
690 %
691 % \item
692 %
693 % That mark data is then stored in global token lists for use
694 % during the output routine and legacy commands such as
695 % \cs{leftmark} or new commands such as \cs{TopMark} simply access
696 % the data stored in these token lists.
697 % \end{itemize}
698 % That's about it in a nutshell. Of course, there are some details to
699 % be taken care of---those are discussed in the implementation sections.
700 %
701 %
702 %
703 %
704 % \section{Public interfaces for packages such as \pkg{multicol}}
705 %
706 % The functions in this section are public so that packages can make
707 % use of them. However, this must be done with great care, e.g.,
708 % \cs{mark_update_structure_from_material:nn} updates the global mark
709 % structure and can therefore be used only in places where such an
710 % update is meaningful, e.g., in special output routines. Elsewhere, a
711 % change to the mark structure would break the whole mechanism and
712 % querying the marks would return incorrect data.
713 %
714 % \begin{function}{\mark_update_structure_from_material:nn}
715 % \begin{syntax}
716 % \cs{mark_update_structure_from_material:nn} \Arg{region} \Arg{material with marks}
717 % \end{syntax}
718 % Helper function that inspects the marks
719 % inside the second argument and assigns new mark values based on
720 % that to the \meta{region} given in the first argument.
721 % For this it first copies the mark structure from \meta{region} to
722 % \texttt{previous-}\meta{region} and then takes all last mark
723 % values currently in the region and makes them the new top mark
724 % values. Finally it assigns new first and last values for all mark
725 % classes based on what was found in the second argument.
726 %
727 % As a consequence, the allowed values for \meta{region} are
728 % \texttt{page} and \texttt{column} because only they have
729 % \texttt{previous-...} counterparts.
730 %
731 % Another important aspect to keep in mind is that marks are recognized
732 % only if they appear on the top level, e.g., if we want to
733 % process material stored in boxes we need to put it unboxed (using
734 % \cs{unvcopy} etc.)\ into the second argument.
735 % \end{function}
736 %
737 %
738 %
739 % \begin{function}{\mark_copy_structure:nn}
740 % \begin{syntax}
741 % \cs{mark_copy_structure:nn} \Arg{alias} \Arg{source}
742 % \end{syntax}
743 % Helper function that copies all mark values in the \meta{source}
744 % region to \meta{alias}, i.e., make the structures identical. Used
745 % to update the \texttt{previous-...} structures inside
746 % \cs{mark_update_structure_from_material:nn} and \texttt{first-column} and
747 % \texttt{last-column} structures inside the internal commands
748 % \cs{__mark_update_singlecol_structures:} or
749 % \cs{__mark__update_dblcol_structures:}.
750 % \end{function}
751 %
752 %
753 %
754 % \begin{function}{\mark_set_structure_to_err:n}
755 % \begin{syntax}
756 % \cs{mark_set_structure_to_err:n} \Arg{region}
757 % \end{syntax}
758 % Helper function that sets all mark values in the \meta{region} to
759 % an error message. This is currently used for \texttt{last-column}
760 % at times where using marks from it would be questionable/wrong, i.e.,
761 % when we have just processed the first column in a two-column document.
762 % \end{function}
763 %
764 %
765 %
766 % \begin{function}{\mark_clear_structure:n}
767 % \begin{syntax}
768 % \cs{mark_clear_structure:n} \Arg{region}
769 % \end{syntax}
770 % Helper function that sets all mark values in the \meta{region} to
771 % empty. This is currently used for \texttt{column} when a multicol
772 % environment starts; this is because it wouldn't make sense if the top mark
773 % in the first column returned the last mark from a previous
774 % multicol (which may have been much earlier, with intermediate
775 % material).
776 % \end{function}
777 %
778 %
779 % \begin{function}{\mark_get_marks_for_reinsertion:nNN}
780 % \begin{syntax}
781 % \cs{mark_get_marks_for_reinsertion:nNN} \Arg{source}
782 % \qquad \meta{token-list-var for collecting first marks}
783 % \qquad \meta{token-list-var for collecting last marks}
784 % \end{syntax}
785 % Helper function for extracting marks that would otherwise get
786 % lost, for example when they are hidden inside a box. This helper
787 % does not update mark structures and can therefore be used outside
788 % the output routine as well.
789 %
790 % It collects all the top-level marks from inside the \meta{source}
791 % and then adds suitable \cs{mark_insert:nn} commands to each of the two token
792 % lists. These token lists can then be executed at the right place
793 % to reinsert the marks, e.g., directly after the box. This is, for
794 % example, going to be used\footnote{Probably not before 2025, though.}
795 % by \pkg{multicol} when a short balanced
796 % \env{multicols} is returned to the galley for typesetting.
797 %
798 % If the \meta{source} consists of a single vertical box (plus
799 % possibly followed by some glue but nothing else) then the box is
800 % unpacked and the top-level marks are collected from its
801 % content. However, if it is not a vertical box or there are
802 % other data then nothing is unpacked and you have to do the
803 % unpacking yourself to get at the marks inside.
804 %
805 % It is quite likely that one only needs a single token list for
806 % returning the \cs{mark_insert:nn} statements. If that is the case
807 % this command may change to take only two arguments.
808 % \end{function}
809 %
810 %
811 % \section{Internal functions for the standard output routine of \LaTeX{}}
812 %
813 % The functions in this section are tied to the output routine and used in the
814 % interface to \LaTeXe{} and perhaps at some later time within a new
815 % output routine
816 % for \LaTeX. They are not (yet) meant for general use and are
817 % therefore made internal, even though we already use them in
818 % \pkg{multicol}.
819 % Internal means that \verb|@@| automatically gets
820 % replaced in the code (and in the documentation) so we have to give
821 % it a suitable value.
822 % \begin{macrocode}
823 %<@@=mark>
824 % \end{macrocode}
825 %
826 % \begin{function}{\@@_update_singlecol_structures:}
827 % \begin{syntax}
828 % \cs{@@_update_singlecol_structures:}
829 % \end{syntax}
830 % \LaTeXe{} integration function in case we are doing single column
831 % layouts. It assumes that the page content is already stored in
832 % \cs{@outputbox} and processes the marks inside that box. It is
833 % called as part of \cs{@opcol}.
834 % \end{function}
835 %
836 %
837 % \begin{function}{\@@_update_dblcol_structures:}
838 % \begin{syntax}
839 % \cs{@@_update_singlecol_structures:}
840 % \end{syntax}
841 % \LaTeXe{} integration function mark used when we are doing double
842 % column documents. It assumes that the page content is already
843 % stored in \cs{@outputbox} and processes the marks inside that
844 % box. It then does different post-processing depending on the start
845 % of the switch \cs{if@firstcolumn}. If we are in the second column
846 % it also has to update page marks, otherwise it only updates column
847 % marks. It too is called as part of \cs{@opcol}.
848 % \end{function}
849 %
850 %
851 % ^^A \end{documentation}
852 %
853 %
854 %
855 %
856 %
857 % \StopEventually{\setlength\IndexMin{200pt} \PrintIndex }
858 %
859 %
860 % \section{The Implementation}
861 %
862 %
863 %
864 % \begin{macrocode}
865 %<*2ekernel|latexrelease>
866 % \end{macrocode}
867 %
868 % \begin{macrocode}
869 \ExplSyntaxOn
870 % \end{macrocode}
871 %
872 % \begin{macrocode}
873 %<latexrelease>\NewModuleRelease{2022/06/01}{ltmarks}
874 %<latexrelease> {Marks~handling}
875 % \end{macrocode}
876 %
877 % \changes{v1.1f}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
878 % expansion in expl3 code}
879 %
880 % \subsection{Allocating new mark classes}
881 %
882 %
883 % \begin{variable}{\g_@@_classes_seq}
884 % A list holding all the mark classes that have been declared.
885 % \begin{macrocode}
886 \seq_new:N \g_@@_classes_seq
887 % \end{macrocode}
888 % \end{variable}
889 %
890 %
891 %
892 %
893 % \begin{macro}{\mark_new_class:n,\@@_new_class:nn}
894 %
895 % A mark class is created by initializing a number of data
896 % structures. First, we get a register number to refer to the mark
897 % class. The new mark class is then added to the
898 % \cs{g_@@_classes_seq} sequence to be able to easily loop over all
899 % classes. Finally a number of top-level global token lists are
900 % declared that hold various versions of the mark for access.
901 % \begin{macrocode}
902 \cs_new_protected:Npn \mark_new_class:n #1
903 {
904 \seq_if_in:NnTF \g_@@_classes_seq {#1}
905 {
906 \msg_error:nnn { mark } { class-already-defined }
907 {#1}
908 }
909 { \@@_new_class:nn {#1} }
910 }
911 % \end{macrocode}
912 % This is only available in the preamble.
913 % \changes{v1.0c}{2022/05/06}{Wrong command made \cs{@onlypreamble}}
914 % \begin{macrocode}
915 \@onlypreamble \mark_new_class:n
916 % \end{macrocode}
917 % The internal command carries out the necessary allocations.
918 % \begin{macrocode}
919 \cs_new_protected:Npn \@@_new_class:nn #1
920 {
921 %<*trace>
922 \@@_debug:n { \iow_term:e { Marks:~new~mark:~#1~\msg_line_context: } }
923 %</trace>
924 % \end{macrocode}
925 % Use the \LaTeXe{} interface for now as the L3 programming layer
926 % doesn't have one for marks yet.
927 % \begin{macrocode}
928 \exp_args:Nc \newmarks {c_@@_class_ #1 _mark}
929 % \end{macrocode}
930 % Remember the new class in the sequence.
931 % \begin{macrocode}
932 \seq_gput_right:Nn \g_@@_classes_seq {#1}
933 % \end{macrocode}
934 % \begin{macrocode}
935 \@@_init_region:nn {page}{#1}
936 % \end{macrocode}
937 % For the \texttt{page} region we also keep track of the
938 % \texttt{previous-page}.
939 % \begin{macrocode}
940 \@@_init_region:nn {previous-page}{#1}
941 % \end{macrocode}
942 % Same game for \texttt{column} and \texttt{previous-column}
943 % \begin{macrocode}
944 \@@_init_region:nn {column}{#1}
945 \@@_init_region:nn {previous-column}{#1}
946 % \end{macrocode}
947 % But for columns we also allocate token lists for the alias
948 % regions \texttt{first-column} and \texttt{last-column}.
949 % \begin{macrocode}
950 \@@_init_region:nn {first-column}{#1}
951 \@@_init_region:nn {last-column}{#1}
952 % \end{macrocode}
953 % To support multiple columns produced by the \pkg{multicol}
954 % package, we preallocate twenty alias regions (since this is the number of columns that
955 % \pkg{multicol} supports as a maximum). They are filled by copying
956 % the current \texttt{column} into the appropriate \texttt{mcol-...}.
957 % \begin{macrocode}
958 %fmi \@@_init_region:nn {mcol}{#1}
959 %fmi \@@_init_region:nn {previous-mcol}{#1}
960 \@@_init_region:nn {mcol-1}{#1}
961 \@@_init_region:nn {mcol-2}{#1}
962 \@@_init_region:nn {mcol-3}{#1}
963 \@@_init_region:nn {mcol-4}{#1}
964 \@@_init_region:nn {mcol-5}{#1}
965 \@@_init_region:nn {mcol-6}{#1}
966 \@@_init_region:nn {mcol-7}{#1}
967 \@@_init_region:nn {mcol-8}{#1}
968 \@@_init_region:nn {mcol-9}{#1}
969 \@@_init_region:nn {mcol-10}{#1}
970 \@@_init_region:nn {mcol-11}{#1}
971 \@@_init_region:nn {mcol-12}{#1}
972 \@@_init_region:nn {mcol-13}{#1}
973 \@@_init_region:nn {mcol-14}{#1}
974 \@@_init_region:nn {mcol-15}{#1}
975 \@@_init_region:nn {mcol-16}{#1}
976 \@@_init_region:nn {mcol-17}{#1}
977 \@@_init_region:nn {mcol-18}{#1}
978 \@@_init_region:nn {mcol-19}{#1}
979 \@@_init_region:nn {mcol-20}{#1}
980 % \end{macrocode}
981 % We also have to initialize the \texttt{saved-column} region that
982 % is used in \pkg{multicol}. Perhaps we should have a
983 % \cs{NewMarkRegion} so that it would be possible for other
984 % packages to add further regions. But let's wait and see if there is a real
985 % use case for new regions before making the interface more general.
986 % \changes{v1.1d}{2025/06/19}{Also initialize saved-column region (SX chat)}
987 % \begin{macrocode}
988 \@@_init_region:nn {saved-column}{#1}
989 }
990 % \end{macrocode}
991 % \end{macro}
992 %
993 %
994 % \begin{macro}{\@@_init_region:nn,\c_@@_empty_tl}
995 % \changes{v1.1b}{2024/12/03}{Fix inconsistent local/global assignment (gh/1574)}
996 %
997 % For each class (\texttt{\#2}) and region (\texttt{\#1}), we need
998 % three token lists: one for top, first, and last. The default value to be
999 % returned is \enquote{empty}.
1000 % \begin{macrocode}
1001 \cs_new_protected:Npn \@@_init_region:nn #1 #2 {
1002 \tl_new:c { g_@@_#1_top_ #2 _tl }
1003 \tl_new:c { g_@@_#1_first_ #2 _tl }
1004 \tl_new:c { g_@@_#1_last_ #2 _tl }
1005 \tl_gset_eq:cN { g_@@_#1_top_ #2 _tl } \c_@@_empty_tl
1006 \tl_gset_eq:cN { g_@@_#1_first_ #2 _tl } \c_@@_empty_tl
1007 \tl_gset_eq:cN { g_@@_#1_last_ #2 _tl } \c_@@_empty_tl
1008 }
1009 % \end{macrocode}
1010 %
1011 % All marks will have an identification in the form of a
1012 % number\footnote{There are a few cases where special
1013 % identification strings are used, e.g., \texttt{2.09-compat}.} that is
1014 % incremented each time a mark insertion happens; therefore the
1015 % initial empty values should also have such a number, so that data extraction
1016 % will be uniform.
1017 % \changes{v1.0g}{2024/05/31}{Initialize all marks with an id, use
1018 % \texttt{0} when a new class is made (gh/1359)}
1019 % \begin{macrocode}
1020 \tl_const:Nn \c_@@_empty_tl { \@@_value:nn{0}{} }
1021 % \end{macrocode}
1022 % \end{macro}
1023 %
1024 %
1025 % \subsection{Updating mark structures}
1026 %
1027 %
1028 % \begin{macro}{\l_@@_box,\l_@@_ii_box,\g_@@_tmp_tl,\g_@@_new_top_tl}
1029 % For some operations we need two temporary private boxes and two
1030 % private global token lists.
1031 % \begin{macrocode}
1032 \box_new:N \l_@@_box
1033 \box_new:N \l_@@_ii_box
1034 \tl_new:N \g_@@_tmp_tl
1035 \tl_new:N \g_@@_new_top_tl
1036 % \end{macrocode}
1037 % \end{macro}
1038 %
1039 %
1040 %
1041 % \begin{macro}{\@@_extract_and_handle_marks:nn}
1042 % \changes{v1.0e}{2024/01/29}{Macro added}
1043 %
1044 % This is the main macro to extract and handle marks inside some
1045 % vertical material. It is used by
1046 % \cs{mark_update_structure_from_material:nn} (for updating the mark
1047 % structure for a region based on the marks found) and by
1048 % \cs{mark_get_marks_for_reinsertion:nNN} (for extracting marks from
1049 % some material and prepare for reinserting them later (e.g., out
1050 % of a box that is placed as a box into the main galley).
1051 % \begin{macrocode}
1052 \cs_new_protected:Npn \@@_extract_and_handle_marks:nn #1#2 {
1053 % \end{macrocode}
1054 % This macro expects code to handle extracted marks in its first argument and
1055 % vertical material (not boxed or just consisting of a single
1056 % vertical box) as its second. It
1057 % extracts top-level mark information from \verb/#2/, stores them
1058 % as split marks and then calls
1059 % \verb/#1/ to make use of this information.
1060 %
1061 % If it finds a forced break in the material it removes it and then
1062 % restarts the attempt without it.
1063 %
1064 % We start with a group to keep most changes local.
1065 % \begin{macrocode}
1066 \group_begin:
1067 % \end{macrocode}
1068 %
1069 % Getting the first and last marks out of the material in \verb=#2=
1070 % is done by putting the material in a box and then doing a split
1071 % operation to the maximum size possible (which hopefully gets us
1072 % all of the content).\footnote{With normal column material cut
1073 % from the main galley we should always get all material in one go,
1074 % but in certain situations, for example, in a \pkg{multicols}
1075 % environment that contains some \cs{columnbreak}s a single split
1076 % operation will not be enough. Thus, this is something we need to
1077 % handle.} Because this action is used only to get the mark
1078 % values, we don't want any underfull box warnings so we (locally)
1079 % turn those off.
1080 %
1081 % \begin{macrocode}
1082 \dim_set_eq:NN \tex_splitmaxdepth:D \c_max_dim
1083 \int_set_eq:NN \tex_vbadness:D \c_max_int
1084 \dim_set_eq:NN \tex_vfuzz:D \c_max_dim
1085 % \end{macrocode}
1086 %
1087 % There is a further complication: if the material contains infinite
1088 % shrinking glue then a \tn{vsplit} operation will balk with a
1089 % low-level error. Now pages or columns, which are our main concern
1090 % here, can't have such infinite shrinkage if they are cut straight
1091 % from the galley, however the use of \tn{enlargethispage} actually
1092 % does add some at the very bottom (and also wraps the whole page
1093 % into a box by itself, so if we leave it this way then a) we get
1094 % this error and b) we don't see any marks because they are hidden
1095 % one level down).
1096 %
1097 % Another possible issue are packages or user code that place stray
1098 % \tn{vbox}es directly into the main galley (an example is
1099 % \pkg{marginnote} that attaches its marginals in this way). If such
1100 % boxes end up as the last item on the page we should not unpack
1101 % them.
1102 %
1103 % All these issues need to be handled, which is done in
1104 % \cs{@@_prepare_and_extract:nn}.
1105 %
1106 % \begin{macrocode}
1107 \@@_prepare_and_extract:nn {#1} {#2}
1108 % \end{macrocode}
1109 % Once all mark classes have been processed, the data structures are
1110 % updated and we can close the group, which undoes our local
1111 % changes and retains only the global ones.
1112 % \begin{macrocode}
1113 \group_end:
1114 }
1115 % \end{macrocode}
1116 % \end{macro}
1117 %
1118 %
1119 % \begin{macro}{\@@_prepare_and_extract:nn}
1120 %
1121 % This macro does the dirty work. It is not directly integrated in
1122 % \cs{@@_extract_and_handle_marks:nn} because we may have to call
1123 % it recursively if we find forced breaks.
1124 % \begin{macrocode}
1125 \cs_new_protected:Npn \@@_prepare_and_extract:nn #1#2 {
1126 % \end{macrocode}
1127 %
1128 % To handle the \cs{enlargethispage} case we do an \tn{unskip} to
1129 % get rid of any glue that is present at the very end of the material
1130 % and also check if we have then a \tn{vbox} as the last item and
1131 % if so unpack that too, but only under certain conditions, see
1132 % below. All this is temporary done in a group, just for getting
1133 % the marks out, so it doesn't affect the final page production.
1134 %
1135 % \begin{macrocode}
1136 \vbox_set:Nn \l_@@_box
1137 {
1138 #2
1139 \tex_unskip:D
1140 \box_set_to_last:N \l_@@_box
1141 % \end{macrocode}
1142 % After having removed the last box from the current list (if there
1143 % was one) we check whether the vertical list is now empty. If not,
1144 % then the last box is definitely not the one from
1145 % \tn{enlargethispage} and so we can, and should, leave it
1146 % alone. Otherwise we check if this last box is a \tn{vbox}.
1147 % \changes{v1.0d}{2022/06/01}{Extend the logic for detecting the marks
1148 % in the box (gh/836)}
1149 % \begin{macrocode}
1150 \int_compare:nNnT \tex_lastnodetype:D < 0
1151 {
1152 \box_if_vertical:NT \l_@@_box
1153 % \end{macrocode}
1154 % If it is, we unpack the box.
1155 % \begin{macrocode}
1156 { \vbox_unpack:N \l_@@_box }
1157 }
1158 % \end{macrocode}
1159 % If it wasn't a vbox, it was either an hbox or there was no box.
1160 % Given that we are only interested in the marks we don't need put
1161 % it back in that case.
1162 % \begin{macrocode}
1163 }
1164 % \end{macrocode}
1165 % We are now ready to \cs{vsplit} the box to get at the marks. If
1166 % the box contains some infinite negative glue the \TeX{} will
1167 % produce an error complaining about it but it will correctly find
1168 % the split marks. Given that we can't prevent that error, we
1169 % hide it from the user and ensure that \TeX{} doesn't stop. The
1170 % error message still shows in the log, but even that is mitigated
1171 % as best as possible---see the definition of
1172 % \cs{@@_vbox_set_split_to_maxdimen:NN} for the tricks employed.
1173 % \begin{macrocode}
1174 \@@_vbox_set_split_to_maxdimen:NN \l_@@_ii_box \l_@@_box
1175 % \end{macrocode}
1176 % After splitting we check if there is anything left in
1177 % \cs{l_@@_box}. If not then the above split has set some split marks
1178 % that we can then use to finish the extraction:
1179 % \begin{macrocode}
1180 \box_if_empty:NTF \l_@@_box
1181 { #1 }
1182 % \end{macrocode}
1183 % If we have a remainder after the split then this means that there
1184 % was some forced break in the material. We get rid of that by
1185 % combining the content of the two boxes and restart.
1186 % \begin{macrocode}
1187 {
1188 %<*trace>
1189 \@@_debug:n { \iow_term:e
1190 { Marks:~ mark~ extraction~needs~ recursion~
1191 \msg_line_context: } }
1192 %</trace>
1193 \@@_prepare_and_extract:nn {#1}
1194 { \vbox_unpack:N \l_@@_ii_box
1195 \vbox_unpack:N \l_@@_box }
1196 }
1197 }
1198 % \end{macrocode}
1199 % \end{macro}
1200 %
1201 %
1202 % \begin{macro}{\@@_vbox_set_split_to_maxdimen:NN}
1203 %
1204 % Split a box to get at its marks without pausing even if \TeX{} is
1205 % producing an error message because of infinite negative glue in
1206 % the box. If there is such an error we ensure that it only shows
1207 % up in the log but not on the terminal.
1208 %
1209 % With a recent \TeX{} engine that knows the primitive
1210 % \cs{ignoreprimitiveerror} we can turn this
1211 % error into a warning (simply by setting this primitive to the value 1).
1212 % \begin{macrocode}
1213 \if_cs_exist:N \tex_ignoreprimitiveerror:D
1214 % \end{macrocode}
1215 %
1216 % \changes{v1.1e}{2025/08/11}{Use \cs{ignoreprimitiveerror} if available (gh/1750)}
1217 % \begin{macrocode}
1218 \cs_new_protected:Npn \@@_vbox_set_split_to_maxdimen:NN #1#2 {
1219 \tl_set:Ne \l_@@_saved_parameters_tl
1220 { \tex_ignoreprimitiveerror:D
1221 \int_use:N \tex_ignoreprimitiveerror:D
1222 \scan_stop:
1223 }
1224 \tex_ignoreprimitiveerror:D 1 \scan_stop:
1225 \vbox_set_split_to_ht:NNn #1 #2 { \c_max_dim }
1226 \l_@@_saved_parameters_tl
1227 }
1228 % \end{macrocode}
1229 % With older \TeX{} engines we have to make use of David's hack
1230 % below to render the error (fairly) harmless and prevent \TeX{}
1231 % from stopping. But, of course, the above solution is better
1232 % because jumping over the error with a local change to the
1233 % interaction mode still means that \TeX{} thinks there was an
1234 % error in the run so the return code is no longer 0 (and that
1235 % might affect workflows that want to test for this).
1236 % \begin{macrocode}
1237 \else:
1238 % \end{macrocode}
1239 %
1240 % The nice low-level hack by DPC records in the \texttt{.log} that a glue
1241 % shrinkage error is harmless.
1242 %
1243 % We disguise \cs{c_max_dim} in an odd looking csname, which then
1244 % shows up as part of the display of an error message if that error
1245 % happens. This csname forms part of the error display so what
1246 % you get is something like
1247 %\begin{verbatim}
1248 % ! Infinite glue shrinkage found in box being split.
1249 % <argument> Infinite shrink error above ignored !
1250 % l. ... }
1251 %\end{verbatim}
1252 % which hopefully makes it clear that the error is harmless and
1253 % and should be ignored by the reader of the \texttt{.log}.
1254 % \begin{macrocode}
1255 \cs_set_eq:cN {Infinite~shrink~error~above~ignored~!}\c_max_dim
1256 % \end{macrocode}
1257 %
1258 % The whole definition of \cs{@@_vbox_set_split_to_maxdimen:NN}
1259 % below is fully expanded, so we have to use a lot of
1260 % \cs{exp_not:N} commands to prevent expansion where necessary.
1261 % \begin{macrocode}
1262 \cs_new_protected:Npe \@@_vbox_set_split_to_maxdimen:NN #1#2 {
1263 % \end{macrocode}
1264 % We start by saving the current interaction and escape char settings.
1265 % \begin{macrocode}
1266 \tl_set:Ne \exp_not:N \l_@@_saved_parameters_tl
1267 {
1268 \tex_interactionmode:D
1269 \exp_not:N \int_use:N \tex_interactionmode:D \scan_stop:
1270 \tex_escapechar:D
1271 \exp_not:N \int_use:N \tex_escapechar:D \scan_stop:
1272 }
1273 % \end{macrocode}
1274 % Then we change them so that no escape char is printed in the
1275 % error message (accounts for the missing backslash in front of
1276 % \verb/Infinite shrink .../) and we set the interaction to
1277 % \cs{nonstopmode} so that the error (if any) just goes into
1278 % the \texttt{.log} file and \TeX{} doesn't stop at that point.
1279 % \begin{macrocode}
1280 \tex_escapechar:D -1 \scan_stop:
1281 \tex_interactionmode:D 0 \scan_stop:
1282 % \end{macrocode}
1283 % Then we do the splitting of the box to \cs{c_max_dim} to get at
1284 % the marks. This may generate the error we are worried about,
1285 % i.e., if the box contains infinite negative glue. However, \TeX{}
1286 % makes this glue finite and continues, which means we get our split
1287 % marks which is really all we care about.
1288 % \begin{macrocode}
1289 \tex_setbox:D #1 \tex_vsplit:D #2 to
1290 % \end{macrocode}
1291 % The \cs{use:n} may seem pointless, and it is to some extent, but
1292 % we need it to get our disguised \cs{c_max_dim} displayed
1293 % properly as part of the error message if there is one. Without
1294 % it, the display would show only part of what we want it to show
1295 % (try it).
1296 % \begin{macrocode}
1297 \exp_not:N \use:n {
1298 \use:c{Infinite~shrink~error~above~ignored~!}
1299 }
1300 % \end{macrocode}
1301 % Finally, we change the escape char and the interaction mode back
1302 % to what it was before:
1303 % \begin{macrocode}
1304 \exp_not:N \l_@@_saved_parameters_tl
1305 }
1306 % \end{macrocode}
1307 %
1308 % \begin{macrocode}
1309 \fi:
1310 % \end{macrocode}
1311 % \end{macro}
1312 %
1313 %
1314 %
1315 % \begin{macro}{\l_@@_saved_parameters_tl}
1316 % The temporary variable used for resetting escape char and
1317 % interaction mode.
1318 % \begin{macrocode}
1319 \tl_new:N \l_@@_saved_parameters_tl
1320 % \end{macrocode}
1321 % \end{macro}
1322 %
1323 % \begin{macro}{\mark_update_structure_from_material:nn}
1324 %
1325 % \changes{v1.0e}{2024/01/29}{Macro renamed}
1326 %
1327 % This function updates the mark structures of a region. The first
1328 % argument is the region to update and second argument receives the
1329 % material that holds the marks. Out of this material we extract
1330 % the first and last marks for all classes (if there are any) to do
1331 % the assignments.
1332 %
1333 % \begin{macrocode}
1334 \cs_new_protected:Npn \mark_update_structure_from_material:nn #1#2 {
1335 \@@_extract_and_handle_marks:nn
1336 % \end{macrocode}
1337 %
1338 % Once the marks can be extracted we update the structure from the
1339 % split marks (code in \cs{@@_update_structure_from_splitmarks:n}).
1340 % \begin{macrocode}
1341 { \@@_update_structure_from_splitmarks:n {#1} }
1342 { #2 }
1343 }
1344 % \end{macrocode}
1345 % \end{macro}
1346 %
1347 %
1348 %
1349 % \begin{macro}{\@@_update_structure_from_splitmarks:n}
1350 % This macro is called after we have done a \cs{tex_vsplit:D}
1351 % operation and the mark data is in the split marks.
1352 % \begin{macrocode}
1353 \cs_new_protected:Npn \@@_update_structure_from_splitmarks:n #1 {
1354 % \end{macrocode}
1355 %
1356 % The first thing we do is to copy the current region structure to
1357 % \texttt{previous-...}; this leaves the current structure
1358 % untouched so we can update it class by class (as is necessary).
1359 % \begin{macrocode}
1360 \mark_copy_structure:nn { previous-#1 } {#1}
1361 % \end{macrocode}
1362 % After this action we can get first and last marks of the various
1363 % classes through \cs{tex_splitfirstmarks:D} and
1364 % \cs{tex_splitbotmarks:D}. So now we loop over all classes stored in
1365 % \cs{g_@@_classes_seq}.
1366 % \begin{macrocode}
1367 \seq_map_inline:Nn \g_@@_classes_seq
1368 {
1369 % \end{macrocode}
1370 % First action: get the last mark from the previous region, i.e.,
1371 % \verb=previous-#1=. But because it is also still inside \verb=#1=,
1372 % at the moment we use that to construct the name because this is a
1373 % tiny bit faster. Given that we
1374 % need this value in various assignments we store it away which
1375 % avoids unnecessary further csname generations.
1376 % \begin{macrocode}
1377 \tl_gset_eq:Nc \g_@@_new_top_tl { g_@@_#1_last_##1_tl }
1378 % \end{macrocode}
1379 % This will first of all become the new top mark for the current class.
1380 % \begin{macrocode}
1381 \tl_gset_eq:cN { g_@@_#1_top_##1_tl } \g_@@_new_top_tl
1382 % \end{macrocode}
1383 % Next action is to get ourselves the new last mark from the
1384 % material supplied.
1385 % \begin{macrocode}
1386 \tl_gset:No \g_@@_tmp_tl
1387 { \tex_splitbotmarks:D \use:c { c_@@_class_##1_mark } }
1388 % \end{macrocode}
1389 % If this mark doesn't exist then obviously neither does the first mark,
1390 % so both become the last mark from the previous region. We
1391 % have to be a little careful here: something like
1392 % \verb=\mark_insert:nn{foo}{}= adds an \enquote{empty} mark that should
1393 % not be confused with no mark at all. But no mark in our material
1394 % will result in \cs{g_@@_tmp_tl} being fully empty. This is why we
1395 % have to make sure that \enquote{empty} from \cs{mark_insert:nn} only
1396 % appears to be empty when typeset but fails the next test (see below how this
1397 % is done).
1398 % \begin{macrocode}
1399 \tl_if_empty:NTF \g_@@_tmp_tl
1400 {
1401 \tl_gset_eq:cN { g_@@_#1_last_ ##1_tl }
1402 \g_@@_new_top_tl
1403 \tl_gset_eq:cN { g_@@_#1_first_##1_tl }
1404 \g_@@_new_top_tl
1405 }
1406 % \end{macrocode}
1407 % If it wasn't empty, i.e., if it had a real value then we use this
1408 % value for our new last mark instead.
1409 % \begin{macrocode}
1410 {
1411 \tl_gset_eq:cN { g_@@_#1_last_##1_tl } \g_@@_tmp_tl
1412 % \end{macrocode}
1413 % Because we had a last mark we also have a first mark (which
1414 % might be the same, but might be not), so we pick that up and
1415 % assign it to the appropriate token list. This explains why we first
1416 % checked for the last mark because that makes the processing
1417 % faster in case there is none.
1418 % \begin{macrocode}
1419 \tl_gset:co { g_@@_#1_first_##1_tl }
1420 {
1421 \tex_splitfirstmarks:D
1422 \use:c { c_@@_class_##1_mark }
1423 }
1424 }
1425 }
1426 }
1427 % \end{macrocode}
1428 % \end{macro}
1429 %
1430 %
1431 %
1432 % \begin{macro}{\mark_get_marks_for_reinsertion:nNN}
1433 %
1434 % This function extracts the marks from the material in the first
1435 % argument but it does not update any the mark structures. Instead,
1436 % it collects the marks in the token lists given as the second and
1437 % third argument, in such a way that they can be reinserted by just
1438 % executing the token lists.\footnote{It is probably enough to
1439 % collect everything in a single token list as long as we put the
1440 % first marks first and the last marks last). But for extra
1441 % flexibility, I currently use 2 token lists. This might change when it is
1442 % really clear that this is never needed.}
1443 %
1444 % \begin{macrocode}
1445 \cs_new_protected:Npn \mark_get_marks_for_reinsertion:nNN #1#2#3 {
1446 % \end{macrocode}
1447 % First we clear the temporary token lists as we haven't seen any marks yet.
1448 % \begin{macrocode}
1449 \tl_gclear:N \g_@@_first_marks_tl
1450 \tl_gclear:N \g_@@_last_marks_tl
1451 % \end{macrocode}
1452 % Then we extract all top-level marks, thereby filling the token lists
1453 % with suitable \cs{mark_insert:nn} calls.
1454 % \begin{macrocode}
1455 \@@_extract_and_handle_marks:nn
1456 % \end{macrocode}
1457 % The first argument holds the code used for filling the token lists and
1458 % the second holds the material from which all marks should be extracted.
1459 % \begin{macrocode}
1460 \@@_get_from_splitmarks:
1461 { #1 }
1462 % \end{macrocode}
1463 %
1464 % Finally, we copy the updated (or not updated) temporary token
1465 % lists to the two that have been supplied when the function was
1466 % called. By convention \enquote{get} operations return their
1467 % values in local variables and \cs{@@_extract_and_handle_marks:nn}
1468 % runs in a group, which is why we have to use global temporary
1469 % variables for collecting.
1470 % \begin{macrocode}
1471 \tl_set_eq:NN #2 \g_@@_first_marks_tl
1472 \tl_set_eq:NN #3 \g_@@_last_marks_tl
1473 }
1474 % \end{macrocode}
1475 % \end{macro}
1476 %
1477 %
1478 % \begin{macro}{\@@_get_from_splitmarks:}
1479 % This function is called after we have done a \cs{vsplit} to
1480 % update the split marks. It loops through all mark classes to find
1481 % out if there are marks for this class and if so updates the
1482 % global tls used for collecting.
1483 % \begin{macrocode}
1484 \cs_new_protected:Npn \@@_get_from_splitmarks: {
1485 \seq_map_inline:Nn \g_@@_classes_seq
1486 {
1487 % \end{macrocode}
1488 % First we to get the last mark for the current class from the
1489 % material supplied.
1490 % \begin{macrocode}
1491 \tl_gset:No \g_@@_tmp_tl
1492 { \tex_splitbotmarks:D \use:c { c_@@_class_##1_mark } }
1493 % \end{macrocode}
1494 %
1495 % If this mark doesn't exist then obviously first mark doesn't
1496 % either, so we do nothing (other than issuing some debugging
1497 % info).
1498
1499 % We have to be a little careful here: something like
1500 % \verb=\mark_insert:nn{foo}{}= adds an \enquote{empty} mark that
1501 % we should not confuse with the case where there is no mark at
1502 % all.
1503 %
1504 % When there is no mark at all we get a truly empty
1505 % \cs{g_@@_tmp_tl} as a result. This is why we have to make sure
1506 % that an \enquote{empty} mark generated with \cs{mark_insert:nn}
1507 % only appears to be empty when it is typeset, but fails the next
1508 % test (see below how this is done).
1509 % \begin{macrocode}
1510 \tl_if_empty:NTF \g_@@_tmp_tl
1511 {
1512 %<*trace>
1513 \@@_debug:n { \iow_term:e { Marks:~no~ marks~
1514 for~ class~ '##1'~\msg_line_context: } }
1515 %</trace>
1516 }
1517 % \end{macrocode}
1518 %
1519 % If it wasn't empty, i.e., if it had a real value then we use this
1520 % value for our new last mark instead. This means we put an
1521 % appropriate \cs{mark_insert:nn} statement into
1522 % \cs{g_@@_last_marks_tl}.
1523 % \begin{macrocode}
1524 {
1525 %<*trace>
1526 \@@_debug:n { \iow_term:e { Marks:~ extract~ last~
1527 % \end{macrocode}
1528 % The mark content in \cs{g_@@_tmp_tl} may contain aribtrary code
1529 % that may react badly if it is expanded in a write. So we better
1530 % avoid that expansion, otherwise debugging might generate spurious
1531 % errors when turned on.
1532 % \changes{v1.1c}{2025/01/10}{Do not expand mark content while debugging}
1533 % \begin{macrocode}
1534 mark~ for~ class~ '##1'~ =~ \exp_not:o \g_@@_tmp_tl } }
1535 %</trace>
1536 \tl_gput_right:Ne \g_@@_last_marks_tl
1537 { \mark_insert:nn {##1} { \@@_drop_id:o { \g_@@_tmp_tl } } }
1538 % \end{macrocode}
1539 % Because we had a last mark we also have a first mark (which might
1540 % be the same, but might not be), so we pick that up and add it to
1541 % the \cs{g_@@_first_marks_tl} token list. This explains why we
1542 % first checked for the last mark because that makes the processing
1543 % faster in case there is none.
1544 % \begin{macrocode}
1545 %<*trace>
1546 \@@_debug:n { \iow_term:e {
1547 Marks:~ extract~ first~ mark~ for~ class~ '##1'~ =~
1548 % \end{macrocode}
1549 % Again no expansion for the mark content.
1550 % \begin{macrocode}
1551 \exp_not:o {
1552 \tex_splitfirstmarks:D
1553 \use:c { c_@@_class_##1_mark }
1554 }
1555 } }
1556 %</trace>
1557 \tl_gput_right:Ne \g_@@_first_marks_tl
1558 { \mark_insert:nn {##1}
1559 {
1560 % \end{macrocode}
1561 % We better drop the id from the returned value otherwise they
1562 % will accumulate in the marks when reinserted.
1563 % \begin{macrocode}
1564 \@@_drop_id:o {
1565 \tex_splitfirstmarks:D
1566 \use:c { c_@@_class_##1_mark }
1567 }
1568 }
1569 }
1570 }
1571 }
1572 }
1573 % \end{macrocode}
1574 % \end{macro}
1575 %
1576 %
1577 % \begin{macro}{\g_@@_first_marks_tl,\g_@@_last_marks_tl}
1578 % These are two global temporary variables used in the code above.
1579 % \begin{macrocode}
1580 \tl_new:N \g_@@_first_marks_tl
1581 \tl_new:N \g_@@_last_marks_tl
1582 % \end{macrocode}
1583 % \end{macro}
1584 %
1585 %
1586 % \begin{macro}{\mark_copy_structure:nn}
1587 % This function copies the structure for one region to another,
1588 % e.g., from \texttt{page} to \texttt{previous-page} above,
1589 % or later from \texttt{column} to \texttt{first-column}, etc.
1590 % \changes{v1.1a}{2024/11/09}{Macro renamed}
1591 % \begin{macrocode}
1592 \cs_new_protected:Npn \mark_copy_structure:nn #1#2 {
1593 % \end{macrocode}
1594 % This requires a simple loop through all mark classes copying the
1595 % token list from one name to the next.
1596 % \begin{macrocode}
1597 \seq_map_inline:Nn \g_@@_classes_seq
1598 {
1599 \tl_gset_eq:cc { g_@@_ #1 _top_ ##1 _tl }
1600 { g_@@_ #2 _top_ ##1 _tl }
1601 \tl_gset_eq:cc { g_@@_ #1 _first_ ##1 _tl }
1602 { g_@@_ #2 _first_ ##1 _tl }
1603 \tl_gset_eq:cc { g_@@_ #1 _last_ ##1 _tl }
1604 { g_@@_ #2 _last_ ##1 _tl }
1605 }
1606 }
1607 % \end{macrocode}
1608 % \end{macro}
1609 %
1610 %
1611 % \begin{macro}{\mark_clear_structure:n}
1612 % This function sets the structure of one region back to an initial
1613 % state, so that all classes return an empty value if queried.
1614 % \changes{v1.1a}{2024/11/09}{}
1615 % \begin{macrocode}
1616 \cs_new_protected:Npn \mark_clear_structure:n #1 {
1617 % \end{macrocode}
1618 % This requires a simple loop through all mark classes.
1619 % \begin{macrocode}
1620 \seq_map_inline:Nn \g_@@_classes_seq
1621 {
1622 \tl_gset_eq:cN { g_@@_ #1 _top_ ##1 _tl }
1623 \c_@@_empty_tl
1624 \tl_gset_eq:cN { g_@@_ #1 _first_ ##1 _tl }
1625 \c_@@_empty_tl
1626 \tl_gset_eq:cN { g_@@_ #1 _last_ ##1 _tl }
1627 \c_@@_empty_tl
1628 }
1629 }
1630 % \end{macrocode}
1631 % \end{macro}
1632 %
1633 %
1634 %
1635 % \begin{macro}{\mark_set_structure_to_err:n,\@@_error:n}
1636 % A slight variation is to install a fixed error message as the
1637 % value.
1638 % \begin{macrocode}
1639 \cs_new_protected:Npn \mark_set_structure_to_err:n #1 {
1640 \seq_map_inline:Nn \g_@@_classes_seq
1641 {
1642 \tl_gset:ce { g_@@_ #1 _top_ ##1 _tl } { \@@_value:nn{?}{\@@_error:nn {#1}{?} }}
1643 \tl_gset:ce { g_@@_ #1 _first_ ##1 _tl } { \@@_value:nn{?}{\@@_error:nn {#1}{?} }}
1644 \tl_gset:ce { g_@@_ #1 _last_ ##1 _tl } { \@@_value:nn{?}{\@@_error:nn {#1}{?} }}
1645 }
1646 }
1647 % \end{macrocode}
1648 % Given that this is used in only one place, we could hardwire the
1649 % argument which would be a bit more compact, but who knows,
1650 % perhaps we end up with another reason to use this error command
1651 % elsewhere, so for now we keep the argument.
1652 %
1653 % \begin{macrocode}
1654 \cs_new_protected:Npn \@@_error:nn #1#2 {
1655 \msg_error:nnnn { mark } { invalid-use } {#1} {#2}
1656 }
1657 % \end{macrocode}
1658 % \end{macro}
1659 %
1660 %
1661 %
1662 %
1663 % \subsection{Placing and retrieving marks}
1664 %
1665 %
1666 %
1667 % \begin{macro}{\mark_insert:nn}
1668 % This function puts a mark for some \meta{class} at the current point.
1669 % \begin{macrocode}
1670 \cs_new_protected:Npn \mark_insert:nn #1#2
1671 {
1672 \seq_if_in:NnTF \g_@@_classes_seq {#1}
1673 {
1674 % \end{macrocode}
1675 % We need to pass the evaluated argument into the mark but protected
1676 % commands should not expand including those protected using the \cs{protect} approach of
1677 % \LaTeXe{}. We also disable \cs{label} and the
1678 % like.\footnote{Straight copy from \texttt{latex.ltx} but is this
1679 % even correct? At least a label in a running header makes little
1680 % sense if it get set several times! Maybe that needs looking at in
1681 % the 2e kernel.}
1682 %
1683 % At this point the code eventually should get a public
1684 % (and a kernel) hook instead of a set of hardwired settings.
1685 % \begin{macrocode}
1686 \group_begin:
1687 % \end{macrocode}
1688 % Within the group we alter some comments, e.g, \cs{label} or
1689 % \cs{index}, to do the right at this point. This is done in the
1690 % kernel hook \cs{@kernel@before@insertmark} which is followed by
1691 % the public hook \hook{insertmark} that can be used by packages to
1692 % augment or alter that setup as necessary.
1693 % \begin{macrocode}
1694 \@kernel@before@insertmark
1695 \hook_use:n { insertmark }
1696 \unrestored@protected@xdef \g_@@_tmp_tl
1697 {
1698 % \end{macrocode}
1699 % To ensure that marks are unique we insert a hidden sequence
1700 % marker at the beginning of the content of the mark containing the
1701 % sequence number of the mark.
1702 % \changes{v1.0f}{2024/05/30}{Use sequence marker to make all marks
1703 % unique on nearby regions (gh/1359)}
1704 % \begin{macrocode}
1705 \@@_value:nn{ \int_use:N\g_@@_int }{#2}
1706 }
1707 %<*trace>
1708 \@@_debug:n{ \iow_term:e { Marks:~ set~#1~<-~
1709 '\tl_to_str:V \g_@@_tmp_tl' ~ \msg_line_context: } }
1710 %</trace>
1711 \tex_marks:D \use:c { c_@@_class_ #1 _mark }
1712 {
1713 % \end{macrocode}
1714 % Here is the trick to avoid truly empty marks: if the result from
1715 % the above processing is empty we add something which eventually
1716 % becomes empty, but not immediately; otherwise we just put
1717 % \cs{g_@@_tmp_tl} in.
1718 % \begin{macrocode}
1719 % This is no longer needed with 1.0f
1720 % \tl_if_empty:NTF \g_@@_tmp_tl
1721 % { \exp_not:n { \prg_do_nothing: } }
1722 % { \exp_not:o { \g_@@_tmp_tl } }
1723 \exp_not:o { \g_@@_tmp_tl }
1724 }
1725 \group_end:
1726 % \end{macrocode}
1727 % A mark introduces a possible break point and in certain
1728 % situations that should not happen in vertical mode in \LaTeX{}.
1729 % This may need some checking and possibly cleanup \ldots.
1730 % \begin{macrocode}
1731 \if@nobreak\ifvmode\nobreak\fi\fi
1732 }
1733 % \end{macrocode}
1734 % If the mark class was not known, raise an error.
1735 % \begin{macrocode}
1736 {
1737 \msg_error:nne { mark } { unknown-class }
1738 { \tl_to_str:n {#1} }
1739 }
1740 }
1741 % \end{macrocode}
1742 % \end{macro}
1743 %
1744 %
1745 % \begin{macro}{\@@_value:nn}
1746 % A hidden marker is placed into every mark added by
1747 % \cs{mark_insert:nn}. It will not show up in the output but
1748 % its argument (a counter value that is incremented) makes all
1749 % marks unique so the test for \enquote{equal} is not fooled by two
1750 % different marks having the same mark text.
1751 % \changes{v1.0f}{2024/05/30}{Use sequence marker to make all marks
1752 % unique on nearby regions (gh/1359)}
1753 % \begin{macrocode}
1754 \cs_new_protected:Npn \@@_value:nn #1#2 { #2 }
1755 % \end{macrocode}
1756 % \end{macro}
1757 %
1758 %
1759 % \begin{macro}[int]{\@kernel@before@insertmark}
1760 % \begin{macro}{insertmark}
1761 % By default \cs{label}, \cs{index}, and \cs{glossary} do nothing
1762 % when the mark is inserted.
1763 % \begin{macrocode}
1764 \int_new:N \g_@@_int
1765 \cs_new:Npn \@kernel@before@insertmark {
1766 \cs_set_eq:NN \label \scan_stop:
1767 \cs_set_eq:NN \index \scan_stop:
1768 \cs_set_eq:NN \glossary \scan_stop:
1769 % \end{macrocode}
1770 % We count each mark and use that to place a hidden marker in front
1771 % of the mark text. To ensure that there is no overflow (very
1772 % unlikely but you never know) we restart every 100000 marks. Thus,
1773 % if somebody puts more than that number of marks on a single page
1774 % you could construct a scenario in which that approach fails.
1775 % \changes{v1.0f}{2024/05/30}{Use sequence marker to make all marks
1776 % unique on nearby regions (gh/1359)}
1777 % \begin{macrocode}
1778 \int_compare:nNnTF \g_@@_int < {99999}
1779 { \int_gincr:N \g_@@_int }
1780 { \int_gzero:N \g_@@_int }
1781
1782 }
1783 % \end{macrocode}
1784 % The public hook to augment the setup.
1785 % \begin{macrocode}
1786 \hook_new:n {insertmark}
1787 % \end{macrocode}
1788 % \end{macro}
1789 % \end{macro}
1790 %
1791 % \begin{macro}[EXP]{\mark_use_top:nn, \mark_use_first:nn, \mark_use_last:nn}
1792 %
1793 % To retrieve the first, last or top region mark, we grab the
1794 % appropriate value stored in the corresponding token list variable
1795 % and pass its contents back. These functions should be used only
1796 % in output routines and only after \cs{mark_update_structure_from_material:nn} has acted,
1797 % otherwise their value will be wrong.
1798 %
1799 % \begin{macrocode}
1800 \cs_new:Npn \mark_use_first:nn #1#2 { \@@_use_check:nnn { g_@@_#1_first_#2_tl } {#1} {#2} }
1801 \cs_new:Npn \mark_use_last:nn #1#2 { \@@_use_check:nnn { g_@@_#1_last_#2_tl } {#1} {#2} }
1802 \cs_new:Npn \mark_use_top:nn #1#2 { \@@_use_check:nnn { g_@@_#1_top_#2_tl } {#1} {#2} }
1803 % \end{macrocode}
1804 %
1805 % If used with an unknown class or region these commands will generate an error.
1806 % If that happens in an expandable context then the error
1807 % generation is delayed (e.g., if used in a \cs{section}) and
1808 % happens when the code is finally used in typesetting, e.g., in the
1809 % TOC or a running header.
1810 % If used in a \cs{typeout} you only see something like
1811 % \verb=\__mark_error:n{page}=.
1812 % This is not too good, but probably better than low-level errors, I
1813 % guess, and I don't want to use an expandable error because of the
1814 % size restrictions in such error messages.
1815 % \changes{v1.1a}{2024/11/09}{}
1816 % \begin{macrocode}
1817 \cs_new:Npn \@@_use_check:nnn #1#2#3 {
1818 \tl_if_eq:cNTF {#1} \relax
1819 { \@@_error:nn {#2} {#3} }
1820 { \@@_drop_id:v {#1} }
1821 }
1822 % \end{macrocode}
1823 %
1824 % Each mark starts with an id and while the id does not print it is
1825 % nevertheless better to remove it when returning the mark, so that
1826 % downstream manipulation of the data doesn't have to deal with it.
1827 % \changes{v1.0g}{2024/05/31}{Remove the id when returning the mark value (gh/1359)}
1828 % This is what the \cs{exp_not:o} accomplishes.
1829 % \begin{macrocode}
1830 \cs_new:Npn \@@_drop_id:n #1 { \exp_not:o { #1 } }
1831 \cs_generate_variant:Nn \@@_drop_id:n { o, v }
1832 % \end{macrocode}
1833 % \end{macro}
1834 %
1835 %
1836 %
1837 %
1838 % \subsection{Comparing mark values}
1839 %
1840 %
1841 %
1842 % \begin{macro}[TF,EXP]{\mark_if_eq:nnnn,\mark_if_eq:nnnnnn}
1843 % Test if in a given region (\verb=#1=) for a given class
1844 % (\verb=#2=) the marks in position \verb=#3= and \verb=#4= (top,
1845 % first, or last) are identical
1846 % \begin{macrocode}
1847 \prg_new_conditional:Npnn \mark_if_eq:nnnn #1#2#3#4 { T , F , TF }
1848 {
1849 \tl_if_eq:ccTF { g_@@_ #1 _#3_ #2 _tl }
1850 { g_@@_ #1 _#4_ #2 _tl }
1851 \prg_return_true:
1852 \prg_return_false:
1853 }
1854 % \end{macrocode}
1855 % The fully general test (with two triplets of the form
1856 % \meta{region}, \meta{class}, and \meta{position}) is this:
1857 % \begin{macrocode}
1858 \prg_new_conditional:Npnn \mark_if_eq:nnnnnn #1#2#3#4#5#6 { T , F , TF }
1859 {
1860 \tl_if_eq:ccTF { g_@@_ #1 _#3_ #2 _tl }
1861 { g_@@_ #4 _#6_ #5 _tl }
1862 \prg_return_true:
1863 \prg_return_false:
1864 }
1865 % \end{macrocode}
1866 % \end{macro}
1867 %
1868 %
1869 %
1870 % \subsection{Messages}
1871 %
1872 % Mark errors are \LaTeX{} kernel errors:
1873 % \changes{v1.0d}{2022/06/01}{Marks are kernel errors}
1874 % \begin{macrocode}
1875 \prop_gput:Nnn \g_msg_module_type_prop { mark } { LaTeX }
1876 % \end{macrocode}
1877 %
1878 % \begin{macrocode}
1879 \msg_new:nnnn { mark } { class-already-defined }
1880 { Mark~class~'#1'~already~defined }
1881 {
1882 \c__msg_coding_error_text_tl
1883 LaTeX~was~asked~to~define~a~new~mark~class~called~'#1':~
1884 this~mark~class~already~exists.
1885 \c__msg_return_text_tl
1886 }
1887 % \end{macrocode}
1888 %
1889 % \begin{macrocode}
1890 \msg_new:nnnn { mark } { unknown-class }
1891 { Unknown~mark~class~'#1'. }
1892 {
1893 \c__msg_coding_error_text_tl
1894 LaTeX~was~asked~to~manipulate~a~mark~of~class~'#1',~
1895 but~this~class~of~marks~does~not~exist.
1896 }
1897 % \end{macrocode}
1898 %
1899 % The next error can also happen if the mark class is unknown, so this
1900 % should perhaps be separated into two different errors.
1901 % \begin{macrocode}
1902 \msg_new:nnnn { mark } { invalid-use }
1903 { Mark~region~'#1'~not~usable~or~class~'#2'~unknown }
1904 {
1905 \c__msg_coding_error_text_tl
1906 The~region~'#1'~is~either~not~known~or~data~for~it~
1907 still~needs~to~be~assembled,~e.g.,~last-column~
1908 while~building~the~first-column.~
1909 Also~possible:~the~class~namne~'#2'~is~misspelled.
1910 \c__msg_return_text_tl
1911 }
1912 % \end{macrocode}
1913 %
1914 %
1915 %
1916 %
1917 %
1918 % \subsection{Debugging the mark structures}
1919 %
1920 % Code and commands in this section are not final, it needs more
1921 % experimentation to see what kind of tracing information is going to
1922 % be useful in practice. For now the tracing is mainly meant to be used
1923 % for code testing and not so much for application testing.
1924 %
1925 % It is quite likely that the
1926 % commands and the behavior of the tracing might change in the
1927 % future once we gained some experience with it.
1928 %
1929 % \begin{macro}{\g_@@_debug_bool}
1930 % Holds the current debugging state.
1931 % \begin{macrocode}
1932 \bool_new:N \g_@@_debug_bool
1933 % \end{macrocode}
1934 % \end{macro}
1935 %
1936 % \begin{macro}{\mark_debug_on:,\mark_debug_off:}
1937 % \begin{macro}{\@@_debug:n}
1938 % \begin{macro}{\@@_debug_gset:}
1939 % Turns debugging on and off by redefining \cs{@@_debug:n}.
1940 % \begin{macrocode}
1941 \cs_new_eq:NN \@@_debug:n \use_none:n
1942 \cs_new_protected:Npn \mark_debug_on:
1943 {
1944 \bool_gset_true:N \g_@@_debug_bool
1945 \@@_debug_gset:
1946 }
1947 \cs_new_protected:Npn \mark_debug_off:
1948 {
1949 \bool_gset_false:N \g_@@_debug_bool
1950 \@@_debug_gset:
1951 }
1952 \cs_new_protected:Npn \@@_debug_gset:
1953 {
1954 \cs_gset_protected:Npe \@@_debug:n ##1
1955 { \bool_if:NT \g_@@_debug_bool {##1} }
1956 }
1957 % \end{macrocode}
1958 % \end{macro}
1959 % \end{macro}
1960 % \end{macro}
1961 %
1962 %
1963 %
1964 % \begin{macro}{\DebugMarksOn,\DebugMarksOff}
1965 % CamelCase commands for debugging.
1966 % \begin{macrocode}
1967 \cs_new_eq:NN \DebugMarksOn \mark_debug_on:
1968 \cs_new_eq:NN \DebugMarksOff \mark_debug_off:
1969 % \end{macrocode}
1970 % \end{macro}
1971 %
1972 %
1973 %
1974 % \begin{macro}{\@@_class_status:nnn}
1975 % Shows the mark values across all regions for one mark class
1976 % (\verb=#2=).
1977 %
1978 % The first argument gives some \meta{info} to help
1979 % in identifying where the command was called, the second is
1980 % the class and the third holds the number of \texttt{mcol-...} we
1981 % should display: inside a \env{multicols} environment this will be \cs{col@number},
1982 % in \LaTeX{}'s normal output routines it will be \texttt{0}.
1983 % \changes{v1.1a}{2024/11/09}{Add a third argument}
1984 % \begin{macrocode}
1985 %<*trace>
1986 \cs_new_protected:Npn \@@_class_status:nnn #1#2#3 {
1987 \typeout{ Marks:~#2~ #1:}
1988 \@@_region_status:nnn {#2}{ page~ (previous) } { previous-page }
1989 \@@_region_status:nnn {#2}{ page~ (current)~ } { page }
1990 \@@_region_status:nnn {#2}{ column~ (previous) }{ previous-column }
1991 \@@_region_status:nnn {#2}{ column~ (current)~ }{ column }
1992 \@@_region_status:nnn {#2}{ column~ (first) } { first-column }
1993 \@@_region_status:nnn {#2}{ column~ (last)~ } { last-column }
1994 % \end{macrocode}
1995 % Then finish by displaying a subset of the \texttt{mcol-...} regions: none (\texttt{0})
1996 % in the standard \LaTeX{} output routine and \cs{col@number}
1997 % within a \env{multicols} environment.
1998 % \begin{macrocode}
1999 \int_step_inline:nn {#3}
2000 {
2001 \@@_region_status:nnn {#2}{ column~ (##1)~ } { mcol-##1 }
2002 }
2003 }
2004 % \end{macrocode}
2005 % \end{macro}
2006 %
2007 %
2008 % \begin{macro}{\@@_region_status:nnn}
2009 %
2010 % Display the top, first, and last mark of a region unless none of
2011 % them exist or all of them are empty.
2012 % \begin{macrocode}
2013 \cs_new_protected:Npn \@@_region_status:nnn #1#2#3 {
2014 \group_begin:
2015 \cs_set:Npn \@@_value:nn ##1##2{ \exp_not:n{ {##1} ~ ##2 } }
2016 \tl_if_exist:cT { g_@@_#3_last_ #1 _tl }
2017 {
2018 \tl_if_eq:cNF { g_@@_#3_last_ #1 _tl } \c_@@_empty_tl
2019 {
2020 \typeout{\@spaces #2 =
2021 ~|~ \use:c { g_@@_#3_top_ #1 _tl } ~|~
2022 \use:c { g_@@_#3_first_ #1 _tl } ~|~
2023 \use:c { g_@@_#3_last_ #1 _tl } ~|
2024 }
2025 }
2026 }
2027 \group_end:
2028 }
2029 % \end{macrocode}
2030 % \end{macro}
2031 %
2032 %
2033 % \begin{macro}{\@@_status:nn}
2034 % Show a snapshot of all mark class values across all regions. The
2035 % first argument is a string to identify the output, the second
2036 % argument is the number of \texttt{mcol-...} regions to show. Outside
2037 % of a \env{multicols} environment this is normally set to 0.
2038 % \changes{v1.1a}{2024/11/09}{Add a second argument}
2039 % \begin{macrocode}
2040 \cs_new_protected:Npn \@@_status:nn #1#2
2041 {
2042 \seq_map_inline:Nn \g_@@_classes_seq
2043 { \@@_class_status:nnn {#1} {##1} {#2} }
2044 }
2045 %</trace>
2046 % \end{macrocode}
2047 % \end{macro}
2048 %
2049 %
2050 % \begin{macro}{\ShowMarksAt}
2051 % \changes{v1.0e}{2024/01/29}{Macro added}
2052 % Debugging helper that displays a snapshot of all known mark
2053 % structures. The first argument is a text string that is
2054 % displayed to help identifying when the snapshot was made. The
2055 % optional second one determines how many \texttt{mcol-...} regions
2056 % are displayed (by default 4).
2057 %
2058 % This may not stay like this (or at all), which is why it isn't
2059 % yet documented as an official command.
2060 % \begin{macrocode}
2061 \NewDocumentCommand \ShowMarksAt {m O{4} } {
2062 %<*trace>
2063 \@@_debug:n { \@@_status:nn {#1}{#2} }
2064 %</trace>
2065 }
2066 % \end{macrocode}
2067 % \end{macro}
2068 %
2069 %
2070 % \subsection{Designer-level interfaces}
2071 %
2072 %
2073 % \begin{macro}{\NewMarkClass,\InsertMark}
2074 % These two are identical to the L3 programming layer commands.
2075 % \begin{macrocode}
2076 \cs_new_eq:NN \NewMarkClass \mark_new_class:n
2077 \@onlypreamble \NewMarkClass
2078 % \end{macrocode}
2079 %
2080 % \begin{macrocode}
2081 \cs_new_eq:NN \InsertMark \mark_insert:nn
2082 % \end{macrocode}
2083 % \end{macro}
2084 %
2085 %
2086 % \begin{macro}[EXP]{\TopMark, \FirstMark, \LastMark}
2087 % The following commands take an optional argument that defaults to
2088 % page. There is no checking that the region is actually valid. If
2089 % not there is simply an empty return.
2090 % \begin{macrocode}
2091 \NewExpandableDocumentCommand \FirstMark { O{page} m }
2092 { \mark_use_first:nn {#1}{#2} }
2093 % \end{macrocode}
2094 %
2095 % \begin{macrocode}
2096 \NewExpandableDocumentCommand \LastMark { O{page} m }
2097 { \mark_use_last:nn {#1}{#2} }
2098 % \end{macrocode}
2099 %
2100 % \begin{macrocode}
2101 \NewExpandableDocumentCommand \TopMark { O{page} m }
2102 { \mark_use_top:nn {#1}{#2} }
2103 % \end{macrocode}
2104 % \end{macro}
2105 %
2106 %
2107 %
2108 % \begin{macro}[EXP]{\IfMarksEqualTF,\IfMarksEqualT,\IfMarksEqualF}
2109 % \changes{v1.0h}{2024/10/21}{Define \cs{IfMarksEqualT}, \cs{IfMarksEqualF}}
2110 % We only provide CamelCase commands for the case with one region
2111 % (optional) and one class. One could think of also providing a
2112 % version for the general case with several optional arguments, but
2113 % use cases for this are most likely rare, so not done yet.
2114 % \begin{macrocode}
2115 \NewExpandableDocumentCommand \IfMarksEqualTF {O{page}mmm} {
2116 \mark_if_eq:nnnnTF {#1}{#2}{#3}{#4}
2117 }
2118 \NewExpandableDocumentCommand \IfMarksEqualT {O{page}mmm} {
2119 \mark_if_eq:nnnnT {#1}{#2}{#3}{#4}
2120 }
2121 \NewExpandableDocumentCommand \IfMarksEqualF {O{page}mmm} {
2122 \mark_if_eq:nnnnF {#1}{#2}{#3}{#4}
2123 }
2124 % \end{macrocode}
2125 % \end{macro}
2126 %
2127 %
2128 %
2129 %
2130 % \section{\LaTeXe{} integration}
2131 %
2132 % \subsection{Core \LaTeXe{} integration}
2133 %
2134 % \begin{macro}{\@@_update_singlecol_structures:}
2135 % This command updates the mark structures if we are producing a
2136 % single column document.
2137 % \begin{macrocode}
2138 \cs_new_protected:Npn \@@_update_singlecol_structures: {
2139 % \end{macrocode}
2140 % First we update the \texttt{page} region (which also updates the
2141 % \texttt{previous-page}.
2142 %
2143 % The \cs{@outputbox} is normally in \cs{vbox} in \LaTeX{} but we
2144 % can't take that for granted (an \pkg{amsmath} test document
2145 % changed it to an \cs{hbox} just to trip me up) so we are a little
2146 % careful with unpack now.
2147 % \begin{macrocode}
2148 \box_if_vertical:NTF \@outputbox
2149 {
2150 \mark_update_structure_from_material:nn {page}
2151 { \vbox_unpack:N \@outputbox }
2152 }
2153 {
2154 \mark_update_structure_from_material:nn {page}
2155 { \hbox_unpack:N \@outputbox }
2156 }
2157 % \end{macrocode}
2158 % Then we provide the necessary updates for the aliases.
2159 % \begin{macrocode}
2160 \mark_copy_structure:nn {previous-column}{previous-page}
2161 \mark_copy_structure:nn {column}{page}
2162 \mark_copy_structure:nn {first-column}{page}
2163 \mark_copy_structure:nn {last-column}{page}
2164 %<*trace>
2165 % move this into status itself?
2166 \@@_debug:n
2167 {
2168 \@@_status:nn
2169 { in~ OR~ (
2170 \legacy_if:nTF {@twoside}
2171 { twoside-
2172 \int_if_odd:nTF \c@page
2173 { odd }{ even }
2174 }
2175 { oneside }
2176 )
2177 }
2178 {0}
2179 }
2180 %</trace>
2181 }
2182 % \end{macrocode}
2183 % \end{macro}
2184 %
2185 % \begin{macro}{\@@_update_dblcol_structures:}
2186 % This commands handles the updates if we are doing two-column pages.
2187 % \begin{macrocode}
2188 \cs_new_protected:Npn \@@_update_dblcol_structures: {
2189 % \end{macrocode}
2190 % First we update the \texttt{column} and \texttt{previous-column}
2191 % regions using the material assembled in \cs{@outputbox}.
2192 % \begin{macrocode}
2193 \box_if_vertical:NTF \@outputbox
2194 {
2195 \mark_update_structure_from_material:nn {column}
2196 { \vbox_unpack:N \@outputbox }
2197 }
2198 {
2199 \mark_update_structure_from_material:nn {column}
2200 { \hbox_unpack:N \@outputbox }
2201 }
2202 % \end{macrocode}
2203 % How we have to update the alias regions depends on whether or not
2204 % \cs{@opcol} was called to process the first column or to produce
2205 % the completed page
2206 % \begin{macrocode}
2207 \legacy_if:nTF {@firstcolumn}
2208 {
2209 % \end{macrocode}
2210 % If we are processing the first column then \texttt{column} is our
2211 % \texttt{first-column} and there is no \texttt{last-column} yet,
2212 % so we make those an error.
2213 % \begin{macrocode}
2214 \mark_copy_structure:nn {first-column}{column}
2215 \mark_set_structure_to_err:n {last-column}
2216 }
2217 {
2218 % \end{macrocode}
2219 % If we produce the completed page then the \texttt{first-column}
2220 % is the same as the new \texttt{previous-column}. However, the
2221 % structure should already be correct if you think about it
2222 % (because is was set to \texttt{column} last time which is now the
2223 % \texttt{previous-column}), thus there is no need to make an update.
2224 % \begin{macrocode}
2225 % \mark_copy_structure:nn {first-column}{previous-column}
2226 % \end{macrocode}
2227 % However, we now have a proper \texttt{last-column} so we assign that.
2228 % \begin{macrocode}
2229 \mark_copy_structure:nn {last-column}{column}
2230 % \end{macrocode}
2231 % What now remains doing is to update the \texttt{page} and
2232 % \texttt{previous-page} regions. For this we have to copy the
2233 % settings in \texttt{page} into \texttt{previous-page} and then
2234 % update \texttt{page} such that the top and first marks are taken
2235 % from the \texttt{first-column} region and the last marks are
2236 % taken from the \texttt{last-column} region. All this has to be
2237 % done for all mark classes so we loop over our sequence.
2238 %
2239 % Note that one loop is needed if we arrange the copy statements in
2240 % a suitable way.
2241 % \begin{macrocode}
2242 \seq_map_inline:Nn \g_@@_classes_seq
2243 {
2244 % \end{macrocode}
2245 % The \texttt{previous-page} updates need to come before the
2246 % updates for \texttt{page} region because otherwise the values
2247 % to copy are already overwritten.
2248 % necessary values.
2249 % \begin{macrocode}
2250 \tl_gset_eq:cc { g_@@_previous-page_top_ ##1 _tl }
2251 { g_@@_page_top_ ##1 _tl }
2252 \tl_gset_eq:cc { g_@@_previous-page_first_ ##1 _tl }
2253 { g_@@_page_first_ ##1 _tl }
2254 \tl_gset_eq:cc { g_@@_previous-page_last_ ##1 _tl }
2255 { g_@@_page_last_ ##1 _tl }
2256 % \end{macrocode}
2257 % To update the \texttt{top} we only have to copy what is in
2258 % \texttt{first-column}:
2259 % \begin{macrocode}
2260 \tl_gset_eq:cc { g_@@_page_top_ ##1 _tl }
2261 { g_@@_first-column_top_ ##1 _tl }
2262
2263 % \end{macrocode}
2264 % Updating the \texttt{first} mark for the \texttt{page} region is
2265 % more complicated. We first have to find out of there is any mark
2266 % in the first column (this can be done by comparing the \texttt{top} and
2267 % the \texttt{first} mark of that region).
2268 % \changes{v1.0f}{2024/05/30}{Correct logic for first mark in page
2269 % region if first column contains no marks (gh/1359)}
2270 % \begin{macrocode}
2271 \tl_if_eq:ccTF { g__@@_first-column_top_ ##1 _tl }
2272 { g__@@_first-column_first_ ##1 _tl }
2273 {
2274 % \end{macrocode}
2275 % If there is no mark in the first column we copy the first mark of
2276 % the last column. If that doesn't contain a mark we still get the
2277 % right result because the first mark is then equal to the top mark.
2278 % \begin{macrocode}
2279 \tl_gset_eq:cc { g_@@_page_first_ ##1 _tl }
2280 { g_@@_last-column_first_ ##1 _tl }
2281 }
2282 {
2283 % \end{macrocode}
2284 % On the other hand, if there is a mark in the first column we copy
2285 % over the \texttt{first} mark from that column.
2286 % \begin{macrocode}
2287 \tl_gset_eq:cc { g_@@_page_first_ ##1 _tl }
2288 { g_@@_first-column_first_ ##1 _tl }
2289 }
2290 % \end{macrocode}
2291 % The logic for the \texttt{last} page mark is again simple, we can
2292 % just copy the value in the \texttt{last} mark of the last column.
2293 % If that column doesn't contain any marks, then the value in
2294 % \texttt{last} will be automatically the same as the \texttt{last}
2295 % from the first column.
2296 % \begin{macrocode}
2297 \tl_gset_eq:cc { g_@@_page_last_ ##1 _tl }
2298 { g_@@_last-column_last_ ##1 _tl }
2299 }
2300 }
2301 %<*trace>
2302 \@@_debug:n
2303 {
2304 \@@_status:nn
2305 { in~ OR~ (
2306 \legacy_if:nTF {@twoside}
2307 { twoside-
2308 \int_if_odd:nTF \c@page
2309 { odd }{ even }
2310 }
2311 { oneside }
2312 \space
2313 \legacy_if:nTF {@firstcolumn}
2314 { first~ }{ second~ }
2315 column )
2316 }
2317 {0}
2318 }
2319 %</trace>
2320 }
2321 % \end{macrocode}
2322 % \end{macro}
2323 %
2324 %
2325 %
2326 % \subsection{Other \LaTeXe{} output routines}
2327 %
2328 % This section will cover support for packages that alter the
2329 % \LaTeX{} output routine (as necessary). The support for
2330 % \pkg{multicol} (for now) is handled directly in that package.
2331 %
2332 %
2333 %
2334 %
2335 % \begin{macrocode}
2336 %<@@=>
2337 % \end{macrocode}
2338 %
2339
2340
2341 % \begin{macro}[int]{\@expl@@@mark@update@singlecol@structures@@}
2342 % \begin{macrocode}
2343 \cs_new_eq:NN \@expl@@@mark@update@singlecol@structures@@
2344 \__mark_update_singlecol_structures:
2345 % \end{macrocode}
2346 % \end{macro}
2347 %
2348 %
2349 %
2350 % \begin{macro}[int]{\@expl@@@mark@update@dblcol@structures@@}
2351 % \begin{macrocode}
2352 \cs_new_eq:NN \@expl@@@mark@update@dblcol@structures@@
2353 \__mark_update_dblcol_structures:
2354 % \end{macrocode}
2355 % \end{macro}
2356 %
2357 %
2358 %
2359 %
2360 % \subsection{Rollback information}
2361 %
2362 %
2363 % \begin{macrocode}
2364 %<latexrelease>\IncludeInRelease{0000/00/00}{ltmarks}%
2365 %<latexrelease> {Undo~Marks~handling}
2366 %<latexrelease>
2367 % \end{macrocode}
2368 % We keep the interface commands around even if we roll back in
2369 % case they are used in packages that don't roll back. Not likely
2370 % to do a lot of good, but then there is not much we can do, but
2371 % this at least they won't give unknown csname errors.
2372 % \begin{macrocode}
2373 %<latexrelease>\DeclareRobustCommand \NewMarkClass[1]{}
2374 %<latexrelease>\DeclareRobustCommand \InsertMark[2]{}
2375 %<latexrelease>\RenewExpandableDocumentCommand \FirstMark { O{} m } { }
2376 %<latexrelease>\RenewExpandableDocumentCommand \LastMark { O{} m } { }
2377 %<latexrelease>\RenewExpandableDocumentCommand \TopMark { O{} m } { }
2378 %<latexrelease>\RenewExpandableDocumentCommand \IfMarksEqualTF { O{} mmm }{ }
2379 %<latexrelease>
2380 % \end{macrocode}
2381 % Same here, this avoided extra roll back code in the OR.
2382 % \begin{macrocode}
2383 %<latexrelease>\let \@expl@@@mark@update@singlecol@structures@@ \relax
2384 %<latexrelease>\let \@expl@@@mark@update@dblcol@structures@@ \relax
2385 %<latexrelease>
2386 %<latexrelease>
2387 %<latexrelease>\EndModuleRelease
2388 % \end{macrocode}
2389 %
2390 % \begin{macrocode}
2391 \ExplSyntaxOff
2392 % \end{macrocode}
2393 % \begin{macrocode}
2394 %</2ekernel|latexrelease>
2395 % \end{macrocode}
2396 %
2397 % Reset module prefix:
2398 % \begin{macrocode}
2399 %<@@=>
2400 % \end{macrocode}
2401 %
2402 %
2403 %
2404 %
2405 %
2406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2407 \endinput
2408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2409 %