Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %%
3 %% Copyright (C) 2020-2026
4 %% Frank Mittelbach, The LaTeX Project
5 %%
6 %
7 % This file is part of the LaTeX base system.
8 % -------------------------------------------
9 %
10 % It may be distributed and/or modified under the
11 % conditions of the LaTeX Project Public License, either version 1.3c
12 % of this license or (at your option) any later version.
13 % The latest version of this license is in
14 % https://www.latex-project.org/lppl.txt
15 % and version 1.3c or later is part of all distributions of LaTeX
16 % version 2008 or later.
17 %
18 % This file has the LPPL maintenance status "maintained".
19 %
20 % The list of all files belonging to the LaTeX base distribution is
21 % given in the file `manifest.txt'. See also `legal.txt' for additional
22 % information.
23 %
24 % The list of derived (unpacked) files belonging to the distribution
25 % and covered by LPPL is defined by the unpacking scripts (with
26 % extension .ins) which are part of the distribution.
27 %
28 % \fi
29 %
30 % \iffalse
31 %
32 %%% From File: ltshipout.dtx
33 %
34 %<*driver>
35 % \fi
36 \ProvidesFile{ltshipout.dtx}
37 [2026-03-12 v1.0q LaTeX Kernel (Shipout)]
38 % \iffalse
39 %
40 \documentclass{l3in2edoc}
41 \GetFileInfo{ltshipout.dtx}
42
43 \providecommand\InternalDetectionOff{}
44 \providecommand\InternalDetectionOn{}
45
46 \EnableCrossrefs
47 \CodelineIndex
48 \begin{document}
49 \DocInput{ltshipout.dtx}
50 \end{document}
51 %</driver>
52 %
53 % \fi
54 %
55 %
56 % \long\def\fmi#1{\begin{quote}\itshape Todo: #1\end{quote}}
57 %
58 % \providecommand\hook[1]{\texttt{#1}}
59 % \providecommand\pkg[1]{\texttt{#1}}
60 %
61 %
62 % \title{The shipout routine: hooks and interfaces\thanks{This file has version
63 % \fileversion\ dated \filedate, \copyright\ \LaTeX\
64 % Project.}}
65 %
66 % \author{Frank Mittelbach, \LaTeX{} Project Team}
67 %
68 % \maketitle
69 %
70 %
71 % \tableofcontents
72 %
73 % \section{Introduction}
74 %
75 % The code provides an interface to the \cs{shipout} primitive of
76 % \TeX{} which is called when a finished pages is finally
77 % \enquote{shipped out} to the target output file, e.g., the
78 % \texttt{.dvi} or \texttt{.pdf} file.
79 % A good portion of the code is based on ideas by Heiko Oberdiek
80 % implemented in his packages \pkg{atbegshi} and \pkg{atenddvi}
81 % even though the interfaces are somewhat
82 % different.\footnote{Heiko's interfaces are emulated by the kernel
83 % code, if a document requests his packages, so older documents
84 % will continue to work.}
85 %
86 % \subsection{Overloading the \cs{shipout} primitive}
87 %
88 %
89 % \begin{function}{\shipout}
90 % With this implementation \TeX's shipout primitive is no longer
91 % available for direct use. Instead \cs{shipout} is running some
92 % (complicated) code that picks up the box to be shipped out
93 % regardless of how that is done, i.e., as a constructed \cs{vbox}
94 % or \cs{hbox} or as a box register.
95 %
96 % It then stores it in a named box register. This box can then be
97 % manipulated through a set of hooks after which it is shipped out
98 % for real.
99 %
100 % Each shipout that actually happens (i.e., where the material is
101 % not discarded for one or the other reason) is recorded and the
102 % total number is available in a readonly variable and in a
103 % \LaTeX{} counter.
104 % \end{function}
105 %
106 %
107 % \begin{function}{\RawShipout}
108 % This command implements a simplified shipout that bypasses the
109 % foreground and background
110 % hooks, e.g., only \hook{shipout/firstpage} and
111 % \hook{shipout/lastpage} are executed and the total shipout
112 % counters are incremented.
113 %
114 % The command doesn't use \cs{ShipoutBox} but its own private box
115 % register so that it can be used inside of shipout hooks to do
116 % some additional shipouts while already in the output routine with
117 % the current page being stored in \cs{ShipoutBox}. It does have
118 % access to \cs{ShipoutBox} if it is used in \hook{shipout/before}
119 % (or \hook{shipout/after}) and can use its content.
120 %
121 % It is safe to use it in \hook{shipout/before} or
122 % \hook{shipout/after} but not necessarily in the other
123 % \hook{shipout/...} hooks as they are intended for special
124 % processing.
125 % \end{function}
126 %
127 % \begin{variable}{\ShipoutBox,\l_shipout_box}
128 % This box register is called \cs{ShipoutBox} (alternatively
129 % available via the L3 name \cs{l_shipout_box}).
130 %
131 % This box is a ``local'' box and assignments to it should be done
132 % only locally. Global assignments (as done by some packages with
133 % older code where this is box is known as 255) may work but they are
134 % conceptually wrong and may result in errors under certain
135 % circumstances.
136 %
137 % During the execution of \hook{shipout/before} this box contains
138 % the accumulated material for the page, but not yet any material
139 % added by other shipout hooks.
140 % During execution of \hook{shipout/after}, i.e., after the shipout
141 % has happened, the box also contains any background or foreground
142 % material.
143 %
144 % Material from the hooks \hook{shipout/firstpage} or
145 % \hook{shipout/lastpage} is not included (but only used during the
146 % actual shipout) to facilitate reuse of the box data (e.g.,
147 % \hook{shipout/firstpage} material should never be added to a
148 % later page of the output).
149 % \end{variable}
150 %
151 %
152 % \begin{variable}{\l_shipout_box_ht_dim,
153 % \l_shipout_box_dp_dim,\l_shipout_box_wd_dim,
154 % \l_shipout_box_ht_plus_dp_dim}
155 % The shipout box dimensions are available in the L3 registers
156 % \cs{l_shipout_box_ht_dim}, etc.\ (there are no \LaTeXe{}
157 % names).\footnotemark{} These variables can be used
158 % inside the hook code for \hook{shipout/before},
159 % \hook{shipout/foreground} and \hook{shipout/background} if needed.
160 % \end{variable}
161 % \footnotetext{Might need changing, but HO's version as strings
162 % is not really helpful I think).}
163 %
164 %
165 %
166 %
167 % \subsection{Provided hooks}
168 %
169 % \begin{variable}{shipout/before,shipout/after,
170 % shipout/foreground,shipout/background,
171 % shipout/firstpage,
172 % shipout/lastpage}
173 % The code for \cs{shipout} offers a number of hooks into which packages (or the
174 % user) can add code to support different use cases.
175 % These are:
176 % \begin{description}
177 % \item[\hook{shipout/before}]
178 %
179 % This hook is executed after the finished page has been stored in
180 % \cs{ShipoutBox} / \cs{l_shipout_box}).
181 % It can be used to alter that box content or to discard it
182 % completely (see \cs{DiscardShipoutBox} below).
183 %
184 % You can use \cs{RawShipout} inside this hook for special use
185 % cases. It can make use of \cs{ShipoutBox} (which doesn't yet
186 % include the background and foreground material).
187 %
188 % \textbf{Note:} It is not possible (or say advisable) to try
189 % and use this hook to typeset material with the intention to
190 % return it to main vertical list, it will go wrong and give
191 % unexpected results in many cases---for starters it will appear
192 % after the current page not before or it will vanish or the
193 % vertical spacing will be wrong!
194 % \end{description}
195 % \end{variable}
196 %
197 % \begin{description}
198 % \item[\hook{shipout/background}]
199 %
200 % This hook adds a picture environment into the background of
201 % the page with the \texttt{(0,0)} coordinate in the top-left
202 % corner using a \cs{unitlength} of \texttt{1pt}.
203 %
204 % It should therefore only receive \cs{put} commands or other
205 % commands suitable in a \texttt{picture} environment and the
206 % vertical coordinate values would normally be
207 % negative.
208 %
209 % Technically this is implemented by adding a zero-sized
210 % \cs{hbox} as the very first item into the \cs{ShipoutBox}
211 % containing that \texttt{picture} environment. Thus the rest of
212 % the box content will overprint what ever is typeset by that hook.
213 %
214 %
215 % \item[\hook{shipout/foreground}]
216 %
217 % This hook adds a picture environment into the foreground of
218 % the page with the \texttt{(0,0)} coordinate in the top-left
219 % corner using a \cs{unitlength} of \texttt{1pt}.
220 %
221 % Technically this is implemented by adding a zero-sized
222 % \cs{hbox} as the very last item into the \cs{ShipoutBox} and
223 % raising it up so that it still has its \texttt{(0,0)} point in
224 % the top-left corner.
225 % But being placed after the main box content it will be typeset
226 % later and thus overprints it (i.e., is in the foreground).
227 %
228 % \item[\hook{shipout}]
229 % This hook is executed after foreground and/or background
230 % material has been added, i.e., just in front of the actual
231 % shipout operation. Its purpose is to allow manipulation of the
232 % finalized box (stored in \cs{ShipoutBox}) with the extra
233 % material also in place (which is not yet the case in
234 % \hook{shipout/before}).
235 %
236 % It cannot be used to cancel the shipout operation via
237 % \cs{DiscardShipoutBox} (that has
238 % to happen in \hook{shipout/before}, if desired!
239 %
240 %
241 % \item[\hook{shipout/firstpage}]
242 %
243 % The material from this hook is executed only once at the very
244 % beginning of the first output page that is shipped out (i.e.,
245 % not discarded at the last minute). It should only contain
246 % \cs{special} or similar commands needed to direct post processors
247 % handling the \texttt{.dvi} or \texttt{.pdf} output.\footnotemark
248 % \footnotetext{In
249 % \LaTeXe{} that was already existing, but implemented using a box
250 % register with the name \cs{@begindvibox}.}
251 %
252 % ^^A \fmi{not sure it has to be that restrictive.}
253 %
254 % This hook is added to the very first page regardless of how it
255 % is shipped out (i.e., with \cs{shipout} or \cs{RawShipout}).
256 %
257 % \item[\hook{shipout/lastpage}]
258 %
259 % The corresponding hook to add \cs{special}s at the very end of
260 % the output file. It is only executed on the very last page of
261 % the output file ---
262 % or rather on the page that \LaTeX{} believes is the last one.
263 % Again it is executed regardless of the shipout method.
264 %
265 % It may not be possible for \LaTeX{} to correctly determine
266 % which page is the last one without several reruns. If this
267 % happens and the hook is non-empty then \LaTeX{} will add an
268 % extra page to place the material and also request a rerun to
269 % get the correct placement sorted out.
270 %
271 % \item[\hook{shipout/after}]
272 %
273 % This hook is executed after a shipout has happened. If the
274 % shipout box is discarded this hook is not looked at.
275 %
276 % You can use \cs{RawShipout} inside this hook for special use
277 % cases and the main \cs{ShipoutBox} is still available at this
278 % point (but in contrast to \hook{shipout/before} it now
279 % includes the background and foreground material).
280 %
281 % \textbf{Note:} Just like \hook{shipout/before} this hook is
282 % not meant to be used for adding typeset material back
283 % to the main vertical list---it might vanish or the
284 % vertical spacing will be wrong!
285 %
286 %
287 % \end{description}
288 %
289 % As mentioned above the hook \hook{shipout/before} is executed
290 % first and can manipulate the prepared shipout box stored in
291 % \cs{ShipoutBox} or set things up for use in \cs{write} during the
292 % actual shipout. It is even run if there was a
293 % \cs{DiscardShipoutBox} request in the document.
294 %
295 % The other hooks (except \hook{shipout} and \hook{shipout/after}) are added inside
296 % hboxes to the box being shipped out in the following order:
297 % \begin{center}
298 % \begin{tabular}{ll}
299 % \hook{shipout/firstpage} & only on the first page \\
300 % \hook{shipout/background} & \\
301 % \meta{boxed content of \cs{ShipoutBox}} & \\
302 % \hook{shipout/foreground} & \\
303 % \hook{shipout/lastpage} & only on the last page \\
304 % \end{tabular}
305 % \end{center}
306 % If any of the hooks has no code then the corresponding box is
307 % added at that point.
308 %
309 % Once the (page) box has got the above extra content it can
310 % again be manipulated using the \hook{shipout} hook and then
311 % is shipped out for real.
312 %
313 % Once the (page) box has been shipped out the \hook{shipout/after}
314 % hook is called (while you are still inside the output routine). It
315 % is not called if the shipout box was discarded.
316 %
317 % In a document that doesn't produce pages, e.g., only makes
318 % \cs{typeout}s, none of the hooks are ever executed (as there is no
319 % \cs{shipout}) not even the \hook{shipout/lastpage} hook.
320 %
321 % If \cs{RawShipout} is used instead of \cs{shipout} then only the
322 % hooks \hook{shipout/firstpage} and \hook{shipout/lastpage} are
323 % executed (on the first or last page), all others are bypassed.
324 %
325 %
326 %
327 % \subsection{Legacy \LaTeX{} commands}
328 %
329 % \begin{function}{\AtBeginDvi,\AtEndDvi}
330 % \begin{syntax}
331 % \cs{AtBeginDvi} \Arg{code}
332 % \end{syntax}
333 % \cs{AtBeginDvi} is the existing \LaTeXe{} interface to fill the
334 % \hook{shipout/firstpage} hook. This is not really a good name
335 % as it is not just supporting \texttt{.dvi} but also \texttt{.pdf}
336 % output or \texttt{.xdv}.
337 %
338 % \cs{AtEndDvi} is the counterpart that was not available in the
339 % kernel but only through the package \pkg{atenddvi}. It fills the
340 % \hook{shipout/lastpage} hook.
341 %
342 % Neither interface can set a code label but uses the current default label.
343 %
344 % \end{function}
345 %
346 % As these two wrappers have been available for a long time we
347 % continue offering them (but not enhancing them, e.g., by providing
348 % support for code labels).
349 %
350 % For new code we strongly suggest using the high-level hook
351 % management commands directly instead of ``randomly-named''
352 % wrappers. This will lead to code that is easier to understand
353 % and to maintain and it also allows you to set code labels if
354 % needed.
355 %
356 % For this reason we do not provide any other ``new'' wrapper
357 % commands for the above hooks in the kernel, but only keep
358 % the existing ones for backward compatibility.
359 %
360 %
361 % \subsection{Special commands for use inside the hooks}
362 %
363 % \begin{function}{\DiscardShipoutBox,\shipout_discard:}
364 % \begin{syntax}
365 % \cs{AddToHookNext} \texttt{\{shipout/before\} \{...\cs{DiscardShipoutBox}...\}}
366 % \end{syntax}
367 % The \cs{DiscardShipoutBox} declaration (L3 name
368 % \cs{shipout_discard:})
369 % requests that on the next
370 % shipout the page box is thrown away instead of being shipped to
371 % the \texttt{.dvi} or \texttt{.pdf} file.
372 %
373 % Typical applications wouldn't do this unconditionally, but have
374 % some processing logic that decides to use or not to use the page.
375 %
376 % Note that if this declaration is used directly in the document it
377 % may depend on the placement to which page it applies, given that
378 % \LaTeX{} output routine is called in an asynchronous manner!
379 % Thus normally one would use this only as part of the
380 % \hook{shipout/before} code.
381 %
382 % \fmi{Once we have a new mark mechanism available we can improve
383 % on that and make sure that the declaration applies to the page
384 % that contains it --- not done (yet)}
385 %
386 % \cs{DiscardShipoutBox} cannot be used in any of the \hook{shipout/...}
387 % hooks other than \hook{shipout/before}.
388 % \end{function}
389 %
390 % In the \pkg{atbegshi} package there are a number of additional
391 % commands for use inside the \hook{shipout/before} hook. They
392 % should normally not be needed any more as one can instead simply
393 % add code to the hooks \hook{shipout/before}, \hook{shipout},
394 % \hook{shipout/background} or
395 % \hook{shipout/foreground}.\footnote{If that assumption turns out to
396 % be wrong it would be trivial to change them to public functions
397 % (right now they are private).} If \pkg{atbegshi} gets loaded then
398 % those commands become available as public functions with their original
399 % names as given below.
400 %
401 %
402 %
403 % \subsection{Provided Lua\TeX\ callbacks}
404 %
405 % \begin{variable}{pre_shipout_filter}
406 % Under Lua\TeX{} the \texttt{pre\_shipout\_filter} Lua callback is
407 % provided which gets called directly after the
408 % \hook{shipout} hook, immediately before the shipout
409 % primitive gets invoked.
410 % The signature is
411 % \begin{verbatim}
412 % function(<node> head)
413 % return true
414 % end
415 % \end{verbatim}
416 % The \texttt{head} is the list node corresponding to the box to be shipped out.
417 % The return value should always be \texttt{true}.
418 % \end{variable}
419 %
420 % \subsection{Information counters}
421 %
422 %
423 % \begin{variable}{\ReadonlyShipoutCounter,\g_shipout_readonly_int}
424 % \begin{syntax}
425 % \cs{ifnum}\cs{ReadonlyShipoutCounter}\texttt{=...}
426 % \cs{int_use:N} \cs{g_shipout_readonly_int} \texttt{\% expl3 usage}
427 % \end{syntax}
428 % This integer holds the number of pages shipped out up to now
429 % (including the one to be shipped out when inside the output
430 % routine). More precisely, it is incremented only after it is
431 % clear that a page will be shipped out, i.e., after the
432 % \hook{shipout/before} hook (because that might discard the page)!
433 % In contrast \hook{shipout/after} sees the incremented value.
434 %
435 % Just like with the \texttt{page} counter its value is
436 % only accurate within the output routine. In the body of the
437 % document it may be off by one as the output routine is called
438 % asynchronously!
439 %
440 % Also important: it \emph{must not} be set, only read. There are
441 % no provisions to prevent that restriction, but if you manipulate
442 % it, chaos will be the result. To emphasize this fact it is not
443 % provided as a \LaTeX{} counter but as a \TeX{} counter (i.e., a
444 % command), so \cs{Alph}\verb={=\cs{ReadonlyShipoutCounter}\verb=}=
445 % etc, would not work.
446 % \end{variable}
447 %
448 % \begin{variable}{totalpages,\g_shipout_totalpages_int}
449 % \begin{syntax}
450 % \cs{arabic}\texttt{\{totalpages\}}
451 % \cs{int_use:N} \cs{g_shipout_totalpage_int} \texttt{\% expl3 usage}
452 % \end{syntax}
453 % In contrast to \cs{ReadonlyShipoutCounter}, the
454 % \texttt{totalpages} counter is a \LaTeX{} counter and incremented
455 % for each shipout attempt including those pages that are discarded
456 % for one or the other reason. Again \hook{shipout/before} sees
457 % the counter before it is incremented. In contrast
458 % \hook{shipout/after} sees the incremented value.
459 %
460 % Furthermore, while it is incremented for each page, its value is
461 % never used by \LaTeX. It can therefore be freely reset or changed by user
462 % code, for example, to additionally count a number of pages that
463 % are not build by \LaTeX\ but are added in a later part of the
464 % process, e.g., cover pages or picture pages made externally.
465 %
466 % Important: as this is a page-related counter its value is only
467 % reliable inside the output routine!
468 % \end{variable}
469 %
470 % \begin{variable}{\PreviousTotalPages}
471 % \begin{syntax}
472 % \cs{PreviousTotalPages}
473 % \end{syntax}
474 % Command that expands to the number of total pages from the
475 % previous run. If there was no previous run or if used in the
476 % preamble it expands to
477 % \texttt{0}. Note that this is a command and not a counter, so in order
478 % to display the number in, say, Roman numerals you have to assign
479 % its value to a counter and then use \cs{Roman} on that counter.
480 % \end{variable}
481 %
482 %
483 % \subsection{Debugging shipout code}
484 %
485 % \begin{function}{\DebugShipoutsOn,\DebugShipoutsOff,
486 % \shipout_debug_on:,\shipout_debug_off:}
487 % \begin{syntax}
488 % \cs{DebugShipoutsOn}
489 % \end{syntax}
490 % Turn the debugging of shipout code on or off. This displays
491 % changes made to the shipout data structures. \fmi{This needs
492 % some rationalizing and may not stay this way.}
493 % \end{function}
494 %
495 %
496 %
497 %
498 % \section{Emulating commands from other packages}
499 %
500 % The packages in this section are no longer necessary, but as they
501 % are used by other packages, they are emulated when they are
502 % explicitly loaded with \cs{usepackage} or \cs{RequirePackage}.
503 %
504 % Please note that the emulation only happens if the package is
505 % explicitly requested, i.e., the commands documented below are not
506 % automatically available in the \LaTeX{} kernel! If you write a
507 % new package we suggest to use the appropriate kernel hooks
508 % directly instead of loading the emulation.
509 %
510 %
511 % \subsection{Emulating \pkg{atbegshi}}
512 %
513 %
514 % \begin{function}{\AtBeginShipoutUpperLeft,\AtBeginShipoutUpperLeftForeground}
515 % \begin{syntax}
516 % \cs{AddToHook} \texttt{\{shipout/before\} \{...\cs{AtBeginShipoutUpperLeft}}\Arg{code}\texttt{...\}}
517 % \end{syntax}
518 % This adds a \texttt{picture} environment into the background of the shipout
519 % box expecting \meta{code} to contain \texttt{picture}
520 % commands. The same effect can be obtained by simply using kernel
521 % features as follows:
522 % \begin{quote}
523 % \cs{AddToHook}\texttt{\{shipout/background\}}\Arg{code}
524 % \end{quote}
525 % There is one technical difference: if
526 % \cs{AtBeginShipoutUpperLeft} is used several times each
527 % invocation is put into its own box inside the shipout box whereas
528 % all \meta{code} going into \hook{shipout/background} ends up
529 % all in the same box in the order it is added or sorted based on
530 % the rules for the hook chunks.
531 %
532 % \cs{AtBeginShipoutUpperLeftForeground} is similar with the
533 % difference that the \texttt{picture} environment is placed in the
534 % foreground. To model it with the kernel functions use the hook
535 % \hook{shipout/foreground} instead.
536 % \end{function}
537 %
538 %
539 % \begin{function}{\AtBeginShipoutAddToBox,\AtBeginShipoutAddToBoxForeground}
540 % \begin{syntax}
541 % \cs{AddToHook} \texttt{\{shipout/before\} \{...\cs{AtBeginShipoutAddToBox}}\Arg{code}\texttt{...\}}
542 % \end{syntax}
543 % These work like \cs{AtBeginShipoutUpperLeft} and
544 % \cs{AtBeginShipoutUpperLeftForeground} with the difference that
545 % \meta{code} is directly placed into an \cs{hbox} inside the
546 % shipout box and not surrounded by a \texttt{picture} environment.
547 %
548 % To emulate them using \hook{shipout/background} or
549 % \hook{shipout/foreground} you may have to wrap \meta{code} into
550 % a \cs{put} statement but if the code is not doing any typesetting
551 % just adding it to the hook should be sufficient.
552 % \end{function}
553 %
554 %
555 %
556 % \begin{function}{\AtBeginShipoutBox}
557 % This is the name of the shipout box as \pkg{atbegshi} knows it.
558 % \end{function}
559 %
560 %
561 % \begin{function}{\AtBeginShipoutOriginalShipout}
562 % This is the name of the \cs{shipout} primitive as \pkg{atbegshi}
563 % knows it. This bypasses all the mechanisms set up by the \LaTeX{}
564 % kernel and there are various scenarios in which it can therefore
565 % fail. It should only be used to run existing legacy
566 % \pkg{atbegshi} code but not in newly developed applications.
567 %
568 % The kernel alternative is \cs{RawShipout} which is integrated
569 % with the \LaTeX{} mechanisms and updates, for example, the
570 % \cs{ReadonlyShipoutCounter} counter. Please use \cs{RawShipout}
571 % for new code if you want to bypass the before, foreground and
572 % background hooks.
573 % \end{function}
574 %
575 % \begin{function}{\AtBeginShipoutInit}
576 % By default \pkg{atbegshi} delayed its action until
577 % \verb=\begin{document}=. This command was forcing it in an earlier
578 % place. With the new concept it does nothing.
579 % \end{function}
580 %
581 % \begin{function}{\AtBeginShipout,\AtBeginShipoutNext}
582 % \begin{syntax}
583 % \cs{AtBeginShipout}\Arg{code} $\equiv$ \cs{AddToHook}\texttt{\{shipout/before\}}\Arg{code}
584 % \cs{AtBeginShipoutNext}\Arg{code} $\equiv$ \cs{AddToHookNext}\texttt{\{shipout/before\}}\Arg{code}
585 % \end{syntax}
586 % This is equivalent to filling the \hook{shipout/before} hook
587 % by either using \cs{AddToHook} or \cs{AddToHookNext}, respectively.
588 % \end{function}
589 %
590 % \begin{function}{\AtBeginShipoutFirst,\AtBeginShipoutDiscard}
591 % The \pkg{atbegshi} names for \cs{AtBeginDvi} and \cs{DiscardShipoutBox}.
592 % \end{function}
593 %
594 %
595 %
596 %
597 % \subsection{Emulating \pkg{everyshi}}
598 %
599 % The \pkg{everyshi} package is providing commands to run arbitrary
600 % code just before the shipout starts.
601 % One point of difference: in the new shipout hooks the page is
602 % available as \cs{ShipoutBox} for inspection of change, one should
603 % not manipulate box 255 directly inside \hook{shipout/before}, so
604 % old code doing this would change to use \cs{ShipoutBox} instead
605 % of \texttt{255} or \cs{@cclv}.
606 %
607 % \begin{function}{\EveryShipout}
608 % \begin{syntax}
609 % \cs{EveryShipout}\Arg{code} $\equiv$ \cs{AddToHook}\texttt{\{shipout/before\}}\Arg{code}
610 % \end{syntax}
611 % \end{function}
612 %
613 % \begin{function}{\AtNextShipout}
614 % \begin{syntax}
615 % \cs{AtNextShipout}\Arg{code} $\equiv$ \cs{AddToHookNext}\texttt{\{shipout/before\}}\Arg{code}
616 % \end{syntax}
617 % \end{function}
618 %
619 % However, most use cases for \pkg{everyshi} are attempts to put
620 % some picture or text into the background or foreground of the page
621 % and that can be done today simply by using the
622 % \hook{shipout/background} and
623 % \hook{shipout/foreground} hooks without any need to coding.
624 %
625 %
626 % \subsection{Emulating \pkg{atenddvi}}
627 %
628 % The \pkg{atenddvi} package implemented only a single command:
629 % \cs{AtEndDvi} and that is now available out of the box so the
630 % emulation makes the package a no-op.
631 %
632 %
633 %
634 % \subsection{Emulating \pkg{everypage}}
635 %
636 % This package patched the original \cs{@begindvi} hook and replaced
637 % it with its own version.
638 % Its functionality is now covered by the hooks offered by the
639 % kernel so that there is no need for such patching any longer.
640 %
641 % \begin{function}{\AddEverypageHook}
642 % \begin{syntax}
643 % \cs{AddEverypageHook}\Arg{code} $\equiv$
644 % \qquad\cs{AddToHook}\texttt{\{shipout/background\}\{\cs{put}(1in,-1in)\Arg{code}\}}
645 % \end{syntax}
646 % \cs{AddEverypageHook} is adding something into the
647 % background of every page at a position of 1in to the right and
648 % 1in down from the top left corner of the page.
649 % By using the kernel hook directly you can put your material
650 % directly to the right place, i.e., use other coordinates in the
651 % \cs{put} statement above.
652 % \end{function}
653 %
654 % \begin{function}{\AddThispageHook}
655 % \begin{syntax}
656 % \cs{AddThispageHook}\Arg{code} $\equiv$
657 % \qquad\cs{AddToHookNext}\texttt{\{shipout/background\}\{\cs{put}(1in,-1in)\Arg{code}\}}
658 % \end{syntax}
659 % The \cs{AddThispageHook} wrapper is similar but uses
660 % \cs{AddToHookNext}.
661 % \end{function}
662 %
663 %
664 % \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
665 %
666 %
667 % \section{The Implementation}
668 % \begin{macrocode}
669 %<@@=shipout>
670 % \end{macrocode}
671 %
672 % At the moment the whole module rolls back in one go, but if we
673 % make any modifications in later releases this will then need
674 % splitting.
675 % \begin{macrocode}
676 %<*2ekernel|latexrelease>
677 %<latexrelease>\IncludeInRelease{2020/10/01}%
678 %<latexrelease> {\shipout}{Hook management (shipout)}%
679 % \end{macrocode}
680 %
681 %
682 % \begin{macrocode}
683 \ExplSyntaxOn
684 % \end{macrocode}
685 %
686 % \changes{v1.0q}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
687 % expansion in expl3 code}
688 %
689 % \subsection{Debugging}
690 %
691 % \begin{macro}{\g_@@_debug_bool}
692 % Holds the current debugging state.
693 % \begin{macrocode}
694 \bool_new:N \g_@@_debug_bool
695 % \end{macrocode}
696 % \end{macro}
697 %
698 % \begin{macro}{\shipout_debug_on:,\shipout_debug_off:}
699 % \begin{macro}{\@@_debug:n}
700 % \begin{macro}{\@@_debug_gset:}
701 % Turns debugging on and off by redefining \cs{@@_debug:n}.
702 % \begin{macrocode}
703 \cs_new_eq:NN \@@_debug:n \use_none:n
704 \cs_new_protected:Npn \shipout_debug_on:
705 {
706 \bool_gset_true:N \g_@@_debug_bool
707 \@@_debug_gset:
708 }
709 \cs_new_protected:Npn \shipout_debug_off:
710 {
711 \bool_gset_false:N \g_@@_debug_bool
712 \@@_debug_gset:
713 }
714 \cs_new_protected:Npn \@@_debug_gset:
715 {
716 \cs_gset_protected:Npe \@@_debug:n ##1
717 { \bool_if:NT \g_@@_debug_bool {##1} }
718 }
719 % \end{macrocode}
720 % \end{macro}
721 % \end{macro}
722 % \end{macro}
723 %
724 %
725 %
726 %
727 %
728 % \begin{macro}{\ShipoutBox,\l_shipout_box}
729 % The box filled with the page to be shipped out (both L3 and
730 % \LaTeXe{} name).
731 % \begin{macrocode}
732 \box_new:N \l_shipout_box
733 % \end{macrocode}
734 %
735 % \begin{macrocode}
736 \cs_set_eq:NN \ShipoutBox \l_shipout_box
737 % \end{macrocode}
738 % \end{macro}
739 %
740 %
741 % \begin{macro}{\l_@@_raw_box}
742 % The \cs{RawShipout} gets its own box but it is internal as there
743 % is no hook manipulation for it.
744 % \begin{macrocode}
745 \box_new:N \l_@@_raw_box
746 % \end{macrocode}
747 % \end{macro}
748 %
749 %
750 % \begin{macro}{\@@_finalize_box:}
751 % For Lua\TeX{} invoke the \texttt{pre\_shipout\_filter} callback.
752 % \changes{v1.0i}{2021/01/22}{Add \texttt{pre\_shipout\_filter} Lua callback}
753 % \begin{macrocode}
754 \sys_if_engine_luatex:TF
755 {
756 \newprotectedluacmd \@@_finalize_box:
757 \exp_args:Ne \everyjob {
758 \exp_not:V \everyjob
759 \exp_not:N \lua_now:n {
760 luatexbase.create_callback('pre_shipout_filter', 'list')
761 local~call, getbox, setbox = luatexbase.call_callback, tex.getbox, tex.setbox~
762 lua.get_functions_table()[\the \allocationnumber] = function()
763 local~head = getbox(\the \l_shipout_box)
764 local~result = call('pre_shipout_filter', head)
765 if~not (result == head) then~
766 setbox(\the \l_shipout_box, result~or~nil)
767 end~
768 end
769 }
770 }
771 } {
772 \cs_set_eq:NN \@@_finalize_box: \scan_stop:
773 }
774 % \end{macrocode}
775 % \end{macro}
776 %
777 %
778 % \begin{macro}{\@@_execute:}
779 % This is going to the be the code run by \cs{shipout}. The code
780 % follows closely the ideas from \pkg{atbegshi}, so not
781 % documenting that here for now.
782 % \begin{macrocode}
783 \cs_set_protected:Npn \@@_execute: {
784 \tl_set:Ne \l_@@_group_level_tl
785 { \int_value:w \tex_currentgrouplevel:D }
786 \tex_afterassignment:D \@@_execute_test_level:
787 \tex_setbox:D \l_shipout_box
788 }
789 % \end{macrocode}
790 % \end{macro}
791 %
792 %
793 % \begin{macro}{\shipout}
794 % Overloading the \cs{shipout} primitive:
795 % \begin{macrocode}
796 \cs_gset_eq:NN \shipout \@@_execute:
797 % \end{macrocode}
798 % \end{macro}
799 %
800 %
801 % \begin{macro}{\l_@@_group_level_tl}
802 % Helper token list to record the group level at which
803 % \cs{@@_execute:} is encountered.
804 % \begin{macrocode}
805 \tl_new:N \l_@@_group_level_tl
806 % \end{macrocode}
807 % \end{macro}
808 %
809 %
810 % \begin{macro}{\@@_execute_test_level:}
811 % If the group level has changed then we are still constructing
812 % \cs{l_shipout_box} and to continue we need to wait until the
813 % current group has finished, hence the \cs{tex_aftergroup:D}.
814 % \begin{macrocode}
815 \cs_new:Npn \@@_execute_test_level: {
816 \int_compare:nNnT
817 \l_@@_group_level_tl < \tex_currentgrouplevel:D
818 \tex_aftergroup:D \@@_execute_cont:
819 }
820 % \end{macrocode}
821 % \end{macro}
822 %
823 %
824 % \begin{macro}{\@@_execute_cont:}
825 % This does the actual shipout running several hooks as part of it.
826 % The code for them is passed as argument \verb=#2= to \verb=#4=
827 % to \cs{@@_execute_main_cont:Nnnn}; the first argument is the box
828 % to be shipped out.
829 % \begin{macrocode}
830 \cs_new:Npn \@@_execute_cont: {
831 \@@_execute_main_cont:Nnnn
832 \l_shipout_box
833 { \hook_use:n {shipout/before} }
834 { \hook_if_empty:nF {shipout/foreground}
835 { \@@_add_foreground_picture:n
836 { \hook_use:n {shipout/foreground} } }
837 % \end{macrocode}
838 % If the user hook for the background (\hook{shipout/background}) has
839 % no code, there might still code in the kernel hook so we need to
840 % test for this too.
841 % We only test for the \cs{@kernel@before@shipout@background}
842 % though. If the \cs{@kernel@after@shipout@background} needs
843 % executing even if the user hook is empty then we can add another
844 % test (or the kernel could put something into the before hook).
845 %
846 % \changes{v1.0d}{2020/11/23}{Check for both kernel and user hook (gh/431)}
847 % \changes{v1.0f}{2021/01/08}{Added another kernel hook for more
848 % flexibility (cf.\ \texttt{https://github.com/pgf-tikz/pgf/issues/960}}
849 % \begin{macrocode}
850 \bool_lazy_and:nnF
851 { \hook_if_empty_p:n {shipout/background} }
852 { \tl_if_empty_p:N \@kernel@before@shipout@background }
853 { \@@_add_background_picture:n
854 { \@kernel@before@shipout@background
855 \hook_use:n {shipout/background}
856 \@kernel@after@shipout@background }
857 }
858 }
859 { \hook_use:n {shipout/after} }
860 }
861 % \end{macrocode}
862 % \end{macro}
863 %
864 %
865 % \begin{macro}{\@@_execute_main_cont:Nnnn}
866 % When we have reached this point the shipout box has been
867 % processed and is available in \cs{l_shipout_box} and ready for
868 % real ship out (unless it gets discarded during the process).
869 %
870 % The three arguments hold hook code that is executed just before the
871 % actual shipout (\verb=#1=), within the shipout adding
872 % background and foreground material (\verb=#2=) and after the
873 % shipout has happened (\verb=#3=).
874 % These are passed as arguments because the same code without those
875 % hooks is also used when doing a ``raw'' shipout implemented by
876 % \cs{RawShipout}.
877 % The only hook that is always executed is that for the very last
878 % page, i.e., \hook{shipout/lastpage}.
879 %
880 % First we quickly check if it is void (can't happen in the
881 % standard \LaTeX{} output routine but \cs{shipout} might be called
882 % from a package that has some special processing logic). If it is
883 % void we aren't shipping anything out and processing ends.\footnote{In that
884 % case we don't reset the deadcycles, that would be up to the OR
885 % processing logic to do.}
886 % \begin{macrocode}
887 \cs_new:Npn \@@_execute_main_cont:Nnnn #1#2#3#4 {
888 \box_if_empty:NTF #1
889 { \@latex@warning@no@line{ Ignoring~ void~ shipout~ box } }
890 {
891 % \end{macrocode}
892 % Otherwise we assume that we will ship something and prepare for
893 % final adjustments (in particular setting the state of
894 % \cs{protect} while we are running the hook code).
895 % We also save the current \cs{protect} state to restore it later.
896 % \begin{macrocode}
897 % \bool_gset_false:N \g_@@_discard_bool % setting this would disable
898 % \DiscardShipoutBox on doc-level
899 \cs_set_eq:NN \@@_saved_protect: \protect
900 \set@typeset@protect
901 % \end{macrocode}
902 % We also store the current shipout box dimension in registers, so that
903 % they can be used in the hook code.\footnote{This is not really
904 % necessary as the code could access them via \cs{box_ht:N}, etc.,
905 % but it is perhaps convenient.}
906 % \begin{macrocode}
907 \@@_get_box_size:N #1
908 % \end{macrocode}
909 % Then we execute the \hook{shipout/before} hook (or nothing in
910 % case of \cs{RawShipout}).
911 % \begin{macrocode}
912 #2
913 % \end{macrocode}
914 % In \cs{g_shipout_totalpages_int} we count all shipout attempts so
915 % we increment that counter already here (the other one is
916 % incremented later when we know for sure that we do a
917 % \cs{shipout}.
918 %
919 % We increment it after running the above hook so that the values
920 % for \cs{g_shipout_totalpages_int} and \cs{g_shipout_readonly_int}
921 % are in sync while the
922 % hook is executed (in the case that totalpages isn't manually
923 % altered or through discarding pages that is).
924 % \begin{macrocode}
925 \int_gincr:N \g_shipout_totalpages_int
926 % \end{macrocode}
927 % The above hook might contain code that requests the page to be discarded so
928 % we now test for it.
929 % \begin{macrocode}
930 \bool_if:NTF \g_@@_discard_bool
931 { \@latex@info@no@line{Completed~ page~ discarded}
932 \bool_gset_false:N \g_@@_discard_bool
933 % \end{macrocode}
934 % As we are discarding the page box and not shipping anything out,
935 % we need to do some house cleaning and reset \TeX's deadcycles so
936 % that it doesn't complain about too many calls to the OR without
937 % any shipout.
938 % \begin{macrocode}
939 \tex_deadcycles:D \c_zero_int
940 % \end{macrocode}
941 % \fmi{In \pkg{atbegshi} the box was dropped but is that actually
942 % needed? Or the resetting of \cs{protect} to its kernel value?}
943 % \begin{macrocode}
944 % \group_begin:
945 % \box_set_eq_drop:NN #1 #1
946 % \group_end:
947 % \cs_set_eq:NN \protect \exp_not:N
948 }
949 % \end{macrocode}
950 % Even if there was no explicit request to discard the box it is
951 % possible that the code for the hook \hook{shipout/before} has
952 % voided the box (by mistake or deliberately). We therefore test
953 % once more but this time make it a warning, because the best
954 % practice way is to use the request mechanism.
955 % \begin{macrocode}
956 { \box_if_empty:NTF #1
957 { \@latex@warning@no@line { Ignoring~ void~ shipout~ box.
958 \MessageBreak The~ shipout~ box~ was~ voided~ by~ hook~ code }
959 }
960 % \end{macrocode}
961 % Finally, if the box is still non-empty we are nearly ready to
962 % ship it out.
963 % First we increment the total page counter so that we can later
964 % test if we have reached the final page according to our available
965 % information.\footnote{Doing that earlier would be wrong because we
966 % might end up with the last page counted but discard and then we
967 % have no place to add the final objects into the output file.}
968 % \begin{macrocode}
969 {
970 \int_gincr:N \g_shipout_readonly_int
971 \@@_debug:n {
972 \typeout{Absolute~ page~ =~ \int_use:N \g_shipout_readonly_int
973 \space (target:~ \@abspage@last)}
974 }
975 % \end{macrocode}
976 % Then we store the box sizes again (as they may have
977 % changed) and then look at the hooks \hook{shipout/foreground}
978 % and \hook{shipout/background}. If either or both are non-empty
979 % we add a \texttt{picture} environment to the box (in the
980 % foreground and/or in the background) and execute the hook code
981 % inside that environment.
982 %
983 % \begin{macrocode}
984 \@@_get_box_size:N #1
985 % \end{macrocode}
986 % The first hook we run is the \hook{shipout/firstpage} hook. This
987 % is only done once, then the \cs{@@_run_firstpage_hook:}
988 % command redefines itself to do nothing. If the hook contains
989 % \cs{special}s for integration at the top of the page they will be
990 % temporarily stored in a safe place and added later with
991 % \cs{@@_add_firstpage_specials:}.
992 % \begin{macrocode}
993 \@@_run_firstpage_hook:
994 % \end{macrocode}
995 % Run the hooks for background and foreground or, if this
996 % is called by \cs{RawShipout}, copy the box \cs{l_@@_raw_box} to
997 % \cs{l_shipout_box} so that firstpage and lastpage material gets
998 % added if necessary (that is always done to \cs{l_shipout_box}.
999 % \begin{macrocode}
1000 #3
1001 % \end{macrocode}
1002 % We then run \cs{@@_add_firstpage_specials:} that adds
1003 % the content of the hook \hook{shipout/firstpage} to the
1004 % start of the first page (if non-empty). It is then redefined to
1005 % do nothing on later pages.
1006 % \begin{macrocode}
1007 \@@_add_firstpage_specials:
1008 % \end{macrocode}
1009 % Then we check if we have to add the \hook{shipout/lastpage} hook
1010 % or the corresponding kernel hook
1011 % because we have reached the last page. This test will be false for
1012 % all but one (and hopefully the correct) page.
1013 % \changes{v1.0d}{2020/11/23}{Check for both kernel and user hook (gh/431)}
1014 % \begin{macrocode}
1015 \int_compare:nNnT \@abspage@last = \g_shipout_readonly_int
1016 { \bool_lazy_and:nnF
1017 { \hook_if_empty_p:n {shipout/lastpage} }
1018 { \tl_if_empty_p:N \@kernel@after@shipout@lastpage }
1019 { \@@_debug:n { \typeout{Executing~ lastpage~ hook~
1020 on~ page~ \int_use:N \g_shipout_readonly_int } }
1021 \_@@_add_foreground_box:n
1022 { \UseHook{shipout/lastpage}
1023 \@kernel@after@shipout@lastpage }
1024 % \end{macrocode}
1025 % We record that we have handled the \hook{shipout/lastpage} hook
1026 % but only if we really did.
1027 % \changes{v1.0m}{2022/05/08}{Handle case where shipout/lastpage is
1028 % run too early (gh/813)}
1029 % \begin{macrocode}
1030 \bool_gset_true:N \g_@@_lastpage_handled_bool
1031 }
1032 }
1033 % \end{macrocode}
1034 %
1035 % \changes{v1.0n}{2022/11/08}{Add shipout hook (gh/920)}
1036 % \begin{macrocode}
1037 \hook_use:n {shipout}
1038 \@@_finalize_box:
1039 % \end{macrocode}
1040 % Finally we run the actual \TeX{} primitive for shipout. As that will
1041 % expand delayed \cs{write} statements inside the page in which
1042 % protected commands should not expand we first change \cs{protect}
1043 % to the appropriate definition for that case.
1044 % \begin{macrocode}
1045 \cs_set_eq:NN \protect \exp_not:N
1046 \tex_shipout:D \box_use:N \l_shipout_box
1047 % \end{macrocode}
1048 % The \cs{l_shipout_box} may contain the firstpage material if this
1049 % was the very first shipout. That makes it unsuitable for reuse in
1050 % another shipout, so as a safety measure the next command resets
1051 % \cs{l_shipout_box} to its earlier state if that is necessary. On
1052 % later pages this is then a no-op.
1053 % \begin{macrocode}
1054 \@@_drop_firstpage_specials:
1055 % \end{macrocode}
1056 % The \hook{shipout/after} hook (if in \verb=#4=) needs to run with
1057 % \cs{protect}ed commands again being executed, because that hook
1058 % will ``typeset'' material added at the top of the next page.
1059 % \begin{macrocode}
1060 \set@typeset@protect
1061 #4
1062 }
1063 }
1064 % \end{macrocode}
1065 % Restore the value of \cs{protect} in case \cs{shipout} is called
1066 % outside of the output routine (where it is automatically restored
1067 % because of the implicit group).
1068 % \begin{macrocode}
1069 \cs_set_eq:NN \protect \@@_saved_protect:
1070 }
1071 }
1072 % \end{macrocode}
1073 % \end{macro}
1074 %
1075 %
1076 % \begin{macro}{\@@_execute_raw:,
1077 % \@@_execute_test_level_raw:}
1078 %
1079 % This implements the ``raw'' shipout which bypasses the before,
1080 % foreground, background and after hooks. It follows the same pattern than
1081 % \cs{_@@_execute_raw:} except that it finally calls
1082 % \cs{_@@_execute_main_cont:Nnnn} with three empty arguments.
1083 % instead of the hook code.
1084 % \begin{macrocode}
1085 \cs_set_protected:Npn \@@_execute_raw: {
1086 \tl_set:Ne \l_@@_group_level_tl
1087 { \int_value:w \tex_currentgrouplevel:D }
1088 \tex_afterassignment:D \@@_execute_test_level_raw:
1089 \tex_setbox:D \l_@@_raw_box
1090 }
1091 % \end{macrocode}
1092 %
1093 % \begin{macrocode}
1094 \cs_new:Npn \@@_execute_test_level_raw: {
1095 \int_compare:nNnT
1096 \l_@@_group_level_tl < \tex_currentgrouplevel:D
1097 \tex_aftergroup:D \@@_execute_nohooks_cont:
1098 }
1099 % \end{macrocode}
1100 % Well, not totally empty arguments, we add some debugging if we
1101 % are actually doing a shipout.
1102 % \begin{macrocode}
1103 \cs_new:Npn \@@_execute_nohooks_cont: {
1104 \@@_execute_main_cont:Nnnn \l_@@_raw_box
1105 {} { \@@_debug:n{ \typeout{Doing~ raw~ shipout~ ...} }
1106 \box_set_eq:NN \l_shipout_box \l_@@_raw_box } {}
1107 }
1108 % \end{macrocode}
1109 % \end{macro}
1110
1111 % \begin{macro}{\RawShipout}
1112 % The interface name for raw shipout.
1113 % \changes{v1.0g}{2021/01/10}{Macro added}
1114 % \begin{macrocode}
1115 \cs_gset_eq:NN \RawShipout \@@_execute_raw:
1116 % \end{macrocode}
1117 % \end{macro}
1118 %
1119 %
1120 %
1121 %
1122 % \begin{macro}{\@@_saved_protect:}
1123 % Remember the current \cs{protect} state.
1124 % \begin{macrocode}
1125 \cs_new_eq:NN \@@_saved_protect: \protect
1126 % \end{macrocode}
1127 % \end{macro}
1128 %
1129 % \begin{macro}{shipout/before,shipout,shipout/after,
1130 % shipout/foreground,shipout/background,
1131 % shipout/firstpage,shipout/lastpage}
1132 % Declaring all hooks for the shipout code.
1133 % \changes{v1.0n}{2022/11/08}{Add shipout hook (gh/920)}
1134 % \begin{macrocode}
1135 \hook_new:n{shipout/before}
1136 \hook_new:n{shipout}
1137 \hook_new:n{shipout/after}
1138 \hook_new:n{shipout/foreground}
1139 \hook_new:n{shipout/background}
1140 \hook_new:n{shipout/firstpage}
1141 \hook_new:n{shipout/lastpage}
1142 % \end{macrocode}
1143 % \end{macro}
1144 %
1145 %
1146 %
1147 % \begin{macro}[no-user-doc]{\@kernel@after@shipout@lastpage,
1148 % \@kernel@before@shipout@background,
1149 % \@kernel@after@shipout@background}
1150 % And here are the internal kernel hooks going before or after the
1151 % public ones where needed.
1152 % \changes{v1.0g}{2021/01/10}{Internal hook
1153 % \cs{@kernel@after@shipout@background} added}
1154 % \begin{macrocode}
1155 \let\@kernel@after@shipout@lastpage\@empty
1156 \let\@kernel@before@shipout@background\@empty
1157 \let\@kernel@after@shipout@background\@empty
1158 % \end{macrocode}
1159 % \end{macro}
1160 %
1161 %
1162 % \begin{macro}{\@@_run_firstpage_hook:}
1163 %
1164 % \changes{v1.0h}{2021/01/19}{Handling of firstpage hook altered}
1165 %
1166 % There are three commands to handle the \hook{shipout/firstpage}
1167 % hook:
1168 % \cs{@@_run_firstpage_hook:}, \cs{@@_add_firstpage_specials:} and
1169 % \cs{@@_drop_firstpage_specials:}.
1170 %
1171 % That hook is supposed to contain \cs{special}s and similar
1172 % material to be placed at the very beginning of the output page
1173 % and so it needs careful placing to avoid that anything else gets
1174 % in front of it. And this means we have to wait with this until
1175 % other hooks such as \hook{shipout/background} have added their
1176 % bits. It is also important that such \cs{special}s show up only
1177 % on the very first page, so if this page gets saved before
1178 % \cs{shipout} for later reuse, we have to make sure that they
1179 % aren't in the saved version.
1180 %
1181 % In addition the hook may also contain code to be executed ``first'', e.g.,
1182 % visible from code in \hook{shipout/background} and this conflicts
1183 % with adding the \cs{special}s late.
1184 %
1185 % Therefore the processing is split into different parts:
1186 % \cs{@@_run_firstpage_hook:} is done early and checks if there is
1187 % any material in the hook.
1188 % \begin{macrocode}
1189 \cs_new:Npn \@@_run_firstpage_hook: {
1190 \hook_if_empty:nTF {shipout/firstpage}
1191 % \end{macrocode}
1192 % If not then we define the other two commands to do nothing.
1193 % \begin{macrocode}
1194 {
1195 \cs_gset_eq:NN \@@_add_firstpage_specials: \prg_do_nothing:
1196 \cs_gset_eq:NN \@@_drop_firstpage_specials: \prg_do_nothing:
1197 }
1198 % \end{macrocode}
1199 % If there is material we execute inside a box, which means any
1200 % \cs{special} will end up in that box and any other code is
1201 % executed and can have side effects (as long as they are global).
1202 % \begin{macrocode}
1203 {
1204 \hbox_set:Nn \l_@@_firstpage_box { \UseHook{shipout/firstpage} }
1205 }
1206 % \end{macrocode}
1207 % Once we are here we change the definition to do nothing next time
1208 % and we also change the command used to implement \cs{AtBeginDvi}
1209 % to become a warning and not add further material to a hook that
1210 % is never used again.
1211 % \begin{macrocode}
1212 \cs_gset_eq:NN \@@_run_firstpage_hook: \prg_do_nothing:
1213 \cs_gset:Npn \@@_add_firstpage_material:Nn ##1 ##2 {
1214 \@latex@warning{ First~ page~ is~ already~ shipped~ out,~ ignoring
1215 \MessageBreak \string##1 }
1216 }
1217 }
1218 % \end{macrocode}
1219 % \end{macro}
1220 %
1221 %
1222 %
1223 % \begin{macro}{\@@_add_firstpage_specials:,\@@_drop_firstpage_specials:}
1224 % The \cs{@@_add_firstpage_specials:} then adds the \cs{special}s
1225 % stored in \cs{l_@@_firstpage_box} to the page to be shipped out
1226 % when the time is ready. Note that if there was no material in the
1227 % \hook{shipout/firstpage} hook then this command gets redefined to
1228 % do nothing. But for most documents there is something, e.g., some
1229 % PostScript header, or some metadata declaration, etc.\ so by
1230 % default we assume there is something to do.
1231 % \begin{macrocode}
1232 \cs_new:Npn \@@_add_firstpage_specials: {
1233 % \end{macrocode}
1234 % First we make a copy of the \cs{l_shipout_box} that we can
1235 % restore it later on.
1236 % \begin{macrocode}
1237 \box_set_eq:NN \l_@@_raw_box \l_shipout_box
1238 % \end{macrocode}
1239 % Adding something to the beginning means adding it to the
1240 % background as that layer is done first in the output.
1241 % \begin{macrocode}
1242 \@@_add_background_box:n { \hbox_unpack_drop:N \l_@@_firstpage_box }
1243 % \end{macrocode}
1244 % After the actual shipout \cs{@@_drop_firstpage_specials:} is
1245 % run to
1246 % restore the earlier content of \cs{l_shipout_box} and then
1247 % redefines itself again to do nothing.
1248 %
1249 % As a final act we change the definition to do nothing next time.
1250 % \begin{macrocode}
1251 \cs_gset_eq:NN \@@_add_firstpage_specials: \prg_do_nothing:
1252 }
1253 % \end{macrocode}
1254 %
1255 % The \cs{@@_drop_firstpage_specials:} is run after the shipout has
1256 % occurred but before the \hook{shipout/afterpage} hook is executed.
1257 % That is the point where we have to restore the \cs{ShipoutBox} to
1258 % its state without the \hook{shipout/firstpage} material.
1259 % \begin{macrocode}
1260 \cs_new:Npn \@@_drop_firstpage_specials: {
1261 \box_set_eq:NN \l_shipout_box \l_@@_raw_box
1262 % \end{macrocode}
1263 % If there was no such material then \cs{@@_run_firstpage_hook:}
1264 % will have changed the definition to a no-op already. Otherwise
1265 % this is what we do here.
1266 % \begin{macrocode}
1267 \cs_gset_eq:NN \@@_drop_firstpage_specials: \prg_do_nothing:
1268 }
1269 % \end{macrocode}
1270 % \end{macro}
1271 %
1272
1273 % \begin{macro}{\l_@@_firstpage_box}
1274 % The box to hold any firstpage \cs{special}s.
1275 % \begin{macrocode}
1276 \box_new:N \l_@@_firstpage_box
1277 % \end{macrocode}
1278 % \end{macro}
1279
1280 %
1281 % \begin{macro}{\g_@@_lastpage_handled_bool}
1282 % A boolean to signal if we have already handled the
1283 % \hook{shipout/lastpage} hook.
1284 % \begin{macrocode}
1285 \bool_new:N \g_@@_lastpage_handled_bool
1286 % \end{macrocode}
1287 % \end{macro}
1288 %
1289 %
1290 %
1291 % \begin{macro}{\@@_add_firstpage_material:Nn}
1292 % This command adds material to the
1293 % \hook{shipout/firstpage} hook. It is used in
1294 % \cs{AtBeginDvi}, etc. The first argument is the
1295 % command through which is it called. Initially this is ignored but
1296 % once we are passed the first page it can be used to generate a
1297 % warning message mentioning the right user command.
1298 % \begin{macrocode}
1299 \cs_new:Npn \@@_add_firstpage_material:Nn #1#2 {
1300 \AddToHook{shipout/firstpage}{#2}
1301 }
1302 % \end{macrocode}
1303 % \end{macro}
1304 %
1305 %
1306 %
1307 %
1308 %
1309 %
1310 %
1311 % \begin{macro}{\@@_get_box_size:N}
1312 % Store the box dimensions in dimen registers.
1313 % \fmi{This could/should perhaps be generalized to set height depth and
1314 % width given an arbitrary box.}
1315 % \begin{macrocode}
1316 \cs_new:Npn \@@_get_box_size:N #1 {
1317 \dim_set:Nn \l_shipout_box_ht_dim { \box_ht:N #1 }
1318 \dim_set:Nn \l_shipout_box_dp_dim { \box_dp:N #1 }
1319 \dim_set:Nn \l_shipout_box_wd_dim { \box_wd:N #1 }
1320 \dim_set:Nn \l_shipout_box_ht_plus_dp_dim
1321 { \l_shipout_box_ht_dim + \l_shipout_box_dp_dim }
1322 }
1323 % \end{macrocode}
1324 % \end{macro}
1325 %
1326 % \begin{macro}{\l_shipout_box_ht_dim,
1327 % \l_shipout_box_dp_dim,\l_shipout_box_wd_dim,
1328 % \l_shipout_box_ht_plus_dp_dim}
1329 % And here are the variables set by \cs{@@_get_box_size:N}.
1330 % \begin{macrocode}
1331 \dim_new:N \l_shipout_box_ht_dim
1332 \dim_new:N \l_shipout_box_dp_dim
1333 \dim_new:N \l_shipout_box_wd_dim
1334 \dim_new:N \l_shipout_box_ht_plus_dp_dim
1335 % \end{macrocode}
1336 % \end{macro}
1337 %
1338 %
1339 %
1340 %
1341 %
1342 % \begin{macro}{\g_@@_discard_bool}
1343 % Indicate whether or not the current page box should be discarded
1344 % \begin{macrocode}
1345 \bool_new:N \g_@@_discard_bool
1346 % \end{macrocode}
1347 % \end{macro}
1348 %
1349 %
1350 %
1351 % \begin{macro}{\l_@@_tmp_box,\l_@@_saved_badness_tl}
1352 % We need a box for the background and foreground material and a
1353 % token register to remember badness settings as we disable them
1354 % during the buildup below.
1355 % \begin{macrocode}
1356 \box_new:N \l_@@_tmp_box
1357 \tl_new:N \l_@@_saved_badness_tl
1358 % \end{macrocode}
1359 % \end{macro}
1360 %
1361 %
1362 % \begin{macro}{\@@_add_background_box:n}
1363 % In standard \LaTeX{} the shipout box is always a \cs{vbox} but
1364 % here we are allow for other usage as well, in case some package
1365 % has its own output routine.
1366 % \begin{macrocode}
1367 \cs_new:Npn \@@_add_background_box:n #1
1368 { \@@_get_box_size:N \l_shipout_box
1369 % \end{macrocode}
1370 % But we start testing for a vertical box as that should be the
1371 % normal case.
1372 % \begin{macrocode}
1373 \box_if_vertical:NTF \l_shipout_box
1374 {
1375 % \end{macrocode}
1376 % Save current values of \cs{vfuzz} and \cs{vbadness} then change
1377 % them to allow box manipulations without warnings.
1378 % \begin{macrocode}
1379 \tl_set:Ne \l_@@_saved_badness_tl
1380 { \vfuzz=\the\vfuzz\relax
1381 \vbadness=\the\vbadness\relax }
1382 \vfuzz=\c_max_dim
1383 \vbadness=\c_max_int
1384 % \end{macrocode}
1385 % Then we reconstruct \cs{l_shipout_box} \ldots
1386 % \begin{macrocode}
1387 \vbox_set_to_ht:Nnn \l_shipout_box \l_shipout_box_ht_plus_dp_dim
1388 {
1389 % \end{macrocode}
1390 % \ldots{} the material in \verb=#1= is placed into a horizontal
1391 % box with zero dimensions.
1392 % \begin{macrocode}
1393 \hbox_set:Nn \l_@@_tmp_box
1394 { \l_@@_saved_badness_tl #1 }
1395 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1396 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1397 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1398 % \end{macrocode}
1399 % The we typeset that box followed by whatever was in
1400 % \cs{l_shipout_box} before (unpacked).
1401 % \begin{macrocode}
1402 \skip_zero:N \baselineskip
1403 \skip_zero:N \lineskip
1404 \skip_zero:N \lineskiplimit
1405 \box_use:N \l_@@_tmp_box
1406 \vbox_unpack:N \l_shipout_box
1407 % \end{macrocode}
1408 % The \cs{kern} ensures that the box has no depth which is
1409 % afterwards explicitly corrected.
1410 % \begin{macrocode}
1411 \kern \c_zero_dim
1412 }
1413 \box_set_ht:Nn \l_shipout_box \l_shipout_box_ht_dim
1414 \box_set_dp:Nn \l_shipout_box \l_shipout_box_dp_dim
1415 % \end{macrocode}
1416 % \fmi{The whole boxing maneuver looks a bit like overkill to me, but for
1417 % the moment I leave.}
1418 % \begin{macrocode}
1419 \l_@@_saved_badness_tl
1420 }
1421 {
1422 % \end{macrocode}
1423 % A horizontal box is handled in a similar way. The last case would
1424 % be a void box in which case we do nothing hence the missing
1425 % \texttt{F} branch.
1426 % \begin{macrocode}
1427 \box_if_horizontal:NT \l_shipout_box
1428 {
1429 \tl_set:Ne \l_@@_saved_badness_tl
1430 { \hfuzz=\the\hfuzz\relax
1431 \hbadness=\the\hbadness\relax }
1432 \hfuzz=\c_max_dim
1433 \hbadness=\c_max_int
1434 \hbox_set_to_wd:Nnn \l_shipout_box \l_shipout_box_wd_dim
1435 {
1436 \hbox_set:Nn \l_@@_tmp_box
1437 { \l_@@_saved_badness_tl #1 }
1438 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1439 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1440 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1441 \box_move_up:nn
1442 \l_shipout_box_ht_dim
1443 { \box_use:N \l_@@_tmp_box }
1444 \hbox_unpack:N \l_shipout_box
1445 }
1446 \l_@@_saved_badness_tl
1447 }
1448 }
1449 }
1450 % \end{macrocode}
1451 % \end{macro}
1452 %
1453 %
1454 %
1455 %
1456 % \begin{macro}{\@@_add_foreground_box:n}
1457 % Foreground boxes are done in the same way, only the order and
1458 % placement of boxes has to be done differently.
1459 % \begin{macrocode}
1460 \cs_new:Npn \@@_add_foreground_box:n #1
1461 {
1462 \box_if_vertical:NTF \l_shipout_box
1463 {
1464 \tl_set:Ne \l_@@_saved_badness_tl
1465 { \vfuzz=\the\vfuzz\relax
1466 \vbadness=\the\vbadness\relax }
1467 \vfuzz=\c_max_dim
1468 \vbadness=\c_max_int
1469 \vbox_set_to_ht:Nnn \l_shipout_box \l_shipout_box_ht_plus_dp_dim
1470 {
1471 \hbox_set:Nn \l_@@_tmp_box
1472 { \l_@@_saved_badness_tl #1 }
1473 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1474 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1475 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1476 \skip_zero:N \baselineskip
1477 \skip_zero:N \lineskip
1478 \skip_zero:N \lineskiplimit
1479 \vbox_unpack:N \l_shipout_box
1480 \kern -\l_shipout_box_ht_plus_dp_dim
1481 \box_use:N \l_@@_tmp_box
1482 \kern \l_shipout_box_ht_plus_dp_dim
1483 }
1484 \l_@@_saved_badness_tl
1485 \box_set_ht:Nn \l_shipout_box \l_shipout_box_ht_dim
1486 \box_set_dp:Nn \l_shipout_box \l_shipout_box_dp_dim
1487 }
1488 {
1489 \box_if_horizontal:NT \l_shipout_box
1490 {
1491 \tl_set:Ne \l_@@_saved_badness_tl
1492 { \hfuzz=\the\hfuzz\relax
1493 \hbadness=\the\hbadness\relax }
1494 \hfuzz=\c_max_dim
1495 \hbadness=\c_max_int
1496 \hbox_set_to_wd:Nnn \l_shipout_box \l_shipout_box_wd_dim
1497 {
1498 \hbox_unpack:N \l_shipout_box
1499 \kern -\box_wd:N \l_shipout_box
1500 \hbox_set:Nn \l_@@_tmp_box
1501 { \l_@@_saved_badness_tl #1 }
1502 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1503 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1504 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1505 \box_move_up:nn { \box_ht:N \l_shipout_box }
1506 { \box_use:N \l_@@_tmp_box }
1507 \kern \box_wd:N \l_shipout_box
1508 }%
1509 \l_@@_saved_badness_tl
1510 }
1511 }
1512 }
1513 % \end{macrocode}
1514 % \end{macro}
1515 %
1516 %
1517 %
1518 %
1519 % \begin{macro}{\@@_init_page_origins:,\c_@@_horigin_tl,\c_@@_vorigin_tl}
1520 % Two constants holding the offset of the top-left with respect to
1521 % the media box.
1522 %
1523 % Setting the constants this way is courtesy of Bruno.
1524 %
1525 % We delay setting the constants to the last possible place as
1526 % there might be updates in the preamble or even in the
1527 % \hook{begindocument} hook that affects their setup.
1528 % \begin{macrocode}
1529 \cs_new:Npn \@@_init_page_origins: {
1530 \tl_const:Ne \c_@@_horigin_tl
1531 {
1532 \cs_if_exist_use:NTF \pdfvariable { horigin }
1533 { \cs_if_exist_use:NF \pdfhorigin { 1in } }
1534 }
1535 \tl_const:Ne \c_@@_vorigin_tl
1536 {
1537 \cs_if_exist_use:NTF \pdfvariable { vorigin }
1538 { \cs_if_exist_use:NF \pdfvorigin { 1in } }
1539 }
1540 % \end{macrocode}
1541 % After the constants have been set there is no need to execute
1542 % this command again, in fact it would raise an error, so we
1543 % redefine it to do nothing.
1544 % \begin{macrocode}
1545 \cs_gset_eq:NN \@@_init_page_origins: \prg_do_nothing:
1546 }
1547 % \end{macrocode}
1548 % \end{macro}
1549 %
1550 %
1551 % \begin{macro}{\@@_picture_overlay:n}
1552 % Put the argument into a \texttt{picture} environment that doesn't take up
1553 % any size and uses \texttt{1pt} for \cs{unitlength}.
1554 % \fmi{Could perhaps be generalized as it might be useful elsewhere. For
1555 % now it is not.}
1556 % \begin{macrocode}
1557 \cs_new:Npn \@@_picture_overlay:n #1 {
1558 % \end{macrocode}
1559 % The very first time this is executed we have to initialize (and
1560 % freeze) the origins.
1561 % \begin{macrocode}
1562 \@@_init_page_origins:
1563 % \end{macrocode}
1564 %
1565 % \begin{macrocode}
1566 \kern -\c_@@_horigin_tl \scan_stop:
1567 \vbox_to_zero:n {
1568 \kern -\c_@@_vorigin_tl \scan_stop:
1569 \unitlength 1pt \scan_stop:
1570 % \end{macrocode}
1571 % This mimics a simple zero-sized picture environment. The \cs{hss}
1572 % is need in case there is horizontal material (without using
1573 % \cs{put} with a positive width.
1574 % \changes{v1.0b}{2020/09/09}
1575 % {Prevent overfull box warnings (gh/387)}
1576 % \begin{macrocode}
1577 \hbox_set_to_wd:Nnn \l_@@_tmp_box \c_zero_dim
1578 { \ignorespaces #1 \hss }
1579 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1580 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1581 \box_use:N \l_@@_tmp_box
1582 \tex_vss:D
1583 }
1584 }
1585 % \end{macrocode}
1586 % \end{macro}
1587 %
1588 %
1589 % \begin{macro}{\@@_add_background_picture:n}
1590 % Put a \texttt{picture} env in the background of the shipout box
1591 % with its reference point in the top-left corner.
1592 %
1593 % \begin{macrocode}
1594 \cs_new:Npn \@@_add_background_picture:n #1 {
1595 \@@_add_background_box:n { \@@_picture_overlay:n {#1} }
1596 }
1597 % \end{macrocode}
1598 % \end{macro}
1599 %
1600 %
1601 %
1602 % \begin{macro}{\@@_add_foreground_picture:n}
1603 %
1604 % Put a \texttt{picture} env in the foreground of the shipout box
1605 % with its reference point in the top-left corner.
1606 % \begin{macrocode}
1607 \cs_new:Npn \@@_add_foreground_picture:n #1 {
1608 \@@_add_foreground_box:n { \@@_picture_overlay:n {#1} }
1609 }
1610 % \end{macrocode}
1611 % \end{macro}
1612 %
1613 %
1614 % \begin{macro}{\shipout_discard:}
1615 % Request that the next shipout box should be discarded. At the
1616 % moment this is just setting a boolean, but we may want to augment
1617 % this behavior that the position of the call is taken into account
1618 % (in case \LaTeX{} looks ahead and is not using the position for
1619 % on the next page).
1620 % \begin{macrocode}
1621 \cs_new_protected:Npn \shipout_discard: {
1622 \bool_gset_true:N \g_@@_discard_bool
1623 }
1624 % \end{macrocode}
1625 % \end{macro}
1626 %
1627 %
1628 %
1629 %
1630 % \subsection{Handling the end of job hook}
1631 %
1632 % At the moment this is partly solved by using the existing hooks.
1633 % But rather than putting the code into these hooks it should be
1634 % moved to the right place directly as we shouldn't prefill hooks
1635 % with material unless it needs to interact with other code.
1636 %
1637 %
1638 %
1639 % \begin{macro}{\g_shipout_readonly_int,\ReadonlyShipoutCounter}
1640 % We count every shipout activity that makes a page (but not those
1641 % that are discarded) in order to know how many pages got produced.
1642 % \begin{macrocode}
1643 \int_new:N \g_shipout_readonly_int
1644 % \end{macrocode}
1645 % For \LaTeXe{} it is available as a command (i.e., a \TeX{}
1646 % counter only.
1647 % \begin{macrocode}
1648 \cs_new_eq:NN \ReadonlyShipoutCounter \g_shipout_readonly_int
1649 % \end{macrocode}
1650 % \end{macro}
1651 %
1652 % \begin{macro}{\g_shipout_totalpages_int,\c@totalpages}
1653 % We count every shipout attempt (even those that are discarded) in
1654 % this counter. It is not used in the code but may get used in user
1655 % code.
1656 % \begin{macrocode}
1657 \int_new:N \g_shipout_totalpages_int
1658 % \end{macrocode}
1659 % For \LaTeXe{} this is offered as a \LaTeX{} counter so can be
1660 % easily typeset inside the output routine to display things like
1661 % \enquote{\cs{thepage}\texttt{/}\cs{thetotalpages}}, etc.
1662 % \begin{macrocode}
1663 \cs_new_eq:NN \c@totalpages \g_shipout_totalpages_int
1664 \cs_new:Npn \thetotalpages { \arabic{totalpages} }
1665 % \end{macrocode}
1666 % \end{macro}
1667 %
1668 %
1669 %
1670 %
1671 % \begin{macro}[int]{\@abspage@last}
1672 % In \cs{@abspage@last} record the number of pages from the last
1673 % run. This is written to the \texttt{.aux} and this way made
1674 % available to the next run. In case there is no \texttt{.aux} file
1675 % or the statement is missing from it we initialize it with the
1676 % largest possible number in \TeX{}. We use this as the default
1677 % because then we are inserting the \hook{shipout/lastpage} on
1678 % the last page (or after the last page) but not on page 1 for a
1679 % multipage document.
1680 % \begin{macrocode}
1681 \xdef\@abspage@last{\number\maxdimen}
1682 % \end{macrocode}
1683 % \end{macro}
1684 %
1685 %
1686 % \begin{macro}[no-user-doc]{\enddocument}
1687 %
1688 % Instead of using the hooks \hook{enddocument} and
1689 % \hook{enddocument/afterlastpage} we add this code to private
1690 % kernel hooks to be 100\% sure when it is executed and to avoid
1691 % cluttering the hooks with data that is always there.
1692 %
1693 % Inside \cs{enddocument} there is a \cs{clearpage}. Just before
1694 % that we execute this code here. There is a good chance that we
1695 % are on the last page. Therefore, if we don't know the value from
1696 % the last run, we assume that the current page is the right
1697 % one. So we set \cs{@abspage@last} and as a result the next
1698 % shipout will run the \hook{shipout/lastpage} code. Of course,
1699 % if there are floats that still need a placement this guess will
1700 % be wrong but then rerunning the document will give us the correct
1701 % value next time around.
1702 %
1703 % \begin{macro}[no-user-doc]{\@kernel@after@enddocument}
1704 % \begin{macrocode}
1705 \g@addto@macro \@kernel@after@enddocument {
1706 \int_compare:nNnT \@abspage@last = \maxdimen
1707 {
1708 % \end{macrocode}
1709 % We use \LaTeXe{} coding as \cs{@abspage@last} is not an L3 name.
1710 % \begin{macrocode}
1711 \xdef\@abspage@last{ \int_eval:n {\g_shipout_readonly_int + 1} }
1712 }
1713 }
1714 % \end{macrocode}
1715 % \end{macro}
1716 %
1717 % \begin{macro}[no-user-doc]{\@kernel@before@enddocument@afterlastpage}
1718 % After the last page has been shipped out, we force further
1719 % \cs{write} calls to be always \cs{immediate} because we aren't
1720 % going to ship out any more pages. This goes before the
1721 % \texttt{enddocument/afterlastpage} hook because that may contain
1722 % such \cs{write} commands.
1723 % \changes{v1.4p}{2025/03/05}{Make all \cs{write}s immediate after
1724 % the last page (gh/1689)}
1725 % \begin{macrocode}
1726 \g@addto@macro \@kernel@before@enddocument@afterlastpage {
1727 \@@_force_immediate_writes:
1728 % This is also the point where \__tag_lastpage_label: could be executed
1729 % instead of going into enddocument/afterlastpage
1730 }
1731 % \end{macrocode}
1732 % \end{macro}
1733 %
1734 %
1735 % \begin{macro}{\@kernel@after@enddocument@afterlastpage}
1736 % Once the \cs{clearpage} has done its work inside \cs{enddocument}
1737 % we know for sure how many pages this document has, so we record
1738 % that in the \texttt{.aux} file for the next run.
1739 % \begin{macrocode}
1740 \g@addto@macro \@kernel@after@enddocument@afterlastpage {
1741 % \end{macrocode}
1742 % There is one special case: If no output is produced then there is
1743 % no point in a) recording the number as 0 will never match the
1744 % page number of a real page and b) adding an extra page to ran the
1745 % \hook{shipout/lastpage} is pointless as well (as it would
1746 % remain forever). So we test for this and run the code only if
1747 % there have been pages.
1748 % \begin{macrocode}
1749 \int_compare:nNnF \g_shipout_readonly_int = 0
1750 {
1751 % \end{macrocode}
1752 % This ends up in the \texttt{.aux} so we use \LaTeXe{} names here.
1753 % \fmi{This needs an interface for \cs{nofiles} in expl3, doesn't at the moment!}
1754 % \begin{macrocode}
1755 \if@filesw
1756 \iow_now:Ne \@auxout {
1757 \gdef\string\@abspage@last {\int_use:N \g_shipout_readonly_int}}
1758 \fi
1759 % \end{macrocode}
1760 % But we may have guessed wrongly earlier and have run it too early
1761 % or we still have to run the
1762 % \hook{shipout/lastpage} even though there is no page to place
1763 % it into. If that is the case we make a trivial extra page and put
1764 % it there. This temporary page will then vanish again on the next
1765 % run but helps to keep pdf viewers happy.
1766 % In either case we should put out an appropriate ``rerun'' warning.
1767 % \changes{v1.0m}{2022/05/08}{Handle case where shipout/lastpage is
1768 % run too early (gh/813)}
1769 % \begin{macrocode}
1770 \bool_if:NTF \g_@@_lastpage_handled_bool
1771 {
1772 % \end{macrocode}
1773 % If the hook was already executed, we have to test if that total
1774 % shipouts match the shipouts from last run (because that
1775 % corresponds to the page it was executed). If not we output a warning.
1776 % \begin{macrocode}
1777 \int_compare:nNnF \@abspage@last = \g_shipout_readonly_int
1778 {
1779 \@latex@warning@no@line{Hook~ 'shipout/lastpage'~ executed~
1780 on~ wrong~ page~ (\@abspage@last\space not~
1781 \int_use:N\g_shipout_readonly_int).\MessageBreak
1782 Rerun~ to~ correct~ this}%
1783 }
1784 }
1785 {
1786 % \end{macrocode}
1787 % If the hook was not run, we need to add an extra page and place
1788 % it there.
1789 % However, making this extra page in case the hook is actually
1790 % empty would be forcing a rerun without any reason, so we check
1791 % that condition and also check if
1792 % \cs{@kernel@after@shipout@lastpage} contains any code. If both
1793 % are empty we omit the page generation.
1794 % \changes{v1.0p}{2025/04/27}{Increment page counter gh/1717}
1795 % \begin{macrocode}
1796 \bool_lazy_and:nnF
1797 { \hook_if_empty_p:n {shipout/lastpage} }
1798 { \tl_if_empty_p:N \@kernel@after@shipout@lastpage }
1799 {
1800 \global\advance\c@page\@ne
1801 \tex_shipout:D\vbox to\textheight
1802 {
1803 \hbox:n { \UseHook{shipout/lastpage}
1804 \@kernel@after@shipout@lastpage }
1805 % \end{macrocode}
1806 % This extra page could be totally empty except for the hook
1807 % content, but to help the user understanding why it is there we
1808 % put some text into it.
1809 % \begin{macrocode}
1810 \@@_excuse_extra_page:
1811 \null
1812 }
1813 % \end{macrocode}
1814 % At this point we also signal to \LaTeX{}'s endgame that a rerun is
1815 % necessary so that an appropriate message can be shown on the
1816 % terminal. We do this by simply defining a command used as a flag and
1817 % tested in \cs{enddocument}.
1818 % \begin{macrocode}
1819 \cs_gset_eq:NN \@extra@page@added \relax
1820 }
1821 }
1822 }
1823 }
1824 % \end{macrocode}
1825 % \end{macro}
1826 % \end{macro}
1827 %
1828 %
1829 %
1830 % \begin{macro}{\@@_excuse_extra_page:}
1831 % Say mea culpa \ldots
1832 % \begin{macrocode}
1833 \cs_new:Npn \@@_excuse_extra_page: {
1834 \vfil
1835 \begin{center}
1836 \bfseries Temporary~ page!
1837 \end{center}
1838 \LaTeX{}~ was~ unable~ to~ guess~ the~ total~ number~ of~ pages~
1839 correctly.~ ~ As~ there~ was~ some~ unprocessed~ data~ that~
1840 should~ have~ been~ added~ to~ the~ final~ page~ this~ extra~
1841 page~ has~ been~ added~ to~ receive~ it.
1842 \par
1843 If~ you~ rerun~ the~ document~ (without~ altering~ it)~ this~
1844 surplus~ page~ will~ go~ away,~ because~ \LaTeX{}~ now~ knows~
1845 how~ many~ pages~ to~ expect~ for~ this~ document.
1846 \vfil
1847 }
1848 % \end{macrocode}
1849 % \end{macro}
1850 %
1851 %
1852 % \begin{macro}{\PreviousTotalPages,\@kernel@before@begindocument}
1853 % In the preamble before the \texttt{aux} file was read
1854 % \cs{PreviousTotalPages} is always zero.
1855 % \begin{macrocode}
1856 \def\PreviousTotalPages{0}
1857 % \end{macrocode}
1858 % In the \texttt{aux} file there should be an update for
1859 % \cs{@abspage@last} recording the number of pages from the
1860 % previous run. If not that macro holds the value of
1861 % \cs{maxdimen}. So we test for it and update
1862 % \cs{PreviousTotalPages} if there was a real value. This should
1863 % happen just before the \hook{begindocument} hook is executed so
1864 % that the value can be used inside that hook.
1865 % \begin{macrocode}
1866 \g@addto@macro\@kernel@before@begindocument
1867 {\ifnum\@abspage@last<\maxdimen
1868 \xdef\PreviousTotalPages{\@abspage@last}\fi}
1869 % \end{macrocode}
1870 % \end{macro}
1871 %
1872 %
1873 % \section{Legacy \LaTeXe{} interfaces}
1874 %
1875 %
1876 %
1877 % \begin{macro}{\DiscardShipoutBox}
1878 % Request that the next shipout box is to be discarded.
1879 % \begin{macrocode}
1880 \cs_new_eq:NN \DiscardShipoutBox \shipout_discard:
1881 % \end{macrocode}
1882 % \end{macro}
1883 %
1884 %
1885 % \begin{macro}{\AtBeginDvi}
1886 % If we roll forward from an earlier kernel \cs{AtBeginDvi} is
1887 % defined so we better not use \cs{cs_new_protected:Npn} here.
1888 % \changes{v1.0d}{2020/11/24}{Support for roll forward (gh/434)}
1889 % \begin{macrocode}
1890 \cs_set_protected:Npn \AtBeginDvi
1891 {\@@_add_firstpage_material:Nn \AtBeginDvi}
1892 % \end{macrocode}
1893 % \end{macro}
1894 %
1895 %
1896 % \begin{macro}{\DebugShipoutsOn,\DebugShipoutsOff}
1897 %
1898 % \begin{macrocode}
1899 \cs_new_eq:NN \DebugShipoutsOn \shipout_debug_on:
1900 \cs_new_eq:NN \DebugShipoutsOff \shipout_debug_off:
1901 % \end{macrocode}
1902 % \end{macro}
1903 %
1904 %
1905 % \section{Internal commands needed elsewhere}
1906 %
1907 % These internal commands use double and triple \texttt{@} signs so
1908 % we need to stop getting them translated to the module name.
1909 % \begin{macrocode}
1910 %<@@=>
1911 % \end{macrocode}
1912 %
1913 % \begin{macro}[int]{\@expl@@@shipout@add@firstpage@material@@Nn,
1914 % \@expl@@@shipout@add@background@box@@n,
1915 % \@expl@@@shipout@add@foreground@box@@n,
1916 % \@expl@@@shipout@add@background@picture@@n,
1917 % \@expl@@@shipout@add@foreground@picture@@n}
1918 % Some internals needed elsewhere.
1919 %
1920 % \InternalDetectionOff
1921 % \begin{macrocode}
1922 \cs_set_eq:NN \@expl@@@shipout@add@firstpage@material@@Nn
1923 \__shipout_add_firstpage_material:Nn
1924 % \end{macrocode}
1925 %
1926 % \begin{macrocode}
1927 \cs_set_eq:NN \@expl@@@shipout@add@background@box@@n
1928 \__shipout_add_background_box:n
1929 % \end{macrocode}
1930 %
1931 % \begin{macrocode}
1932 \cs_set_eq:NN \@expl@@@shipout@add@foreground@box@@n
1933 \__shipout_add_foreground_box:n
1934 % \end{macrocode}
1935 %
1936 % \begin{macrocode}
1937 \cs_set_eq:NN \@expl@@@shipout@add@background@picture@@n
1938 \__shipout_add_background_picture:n
1939 % \end{macrocode}
1940 %
1941 % \begin{macrocode}
1942 \cs_set_eq:NN \@expl@@@shipout@add@foreground@picture@@n
1943 \__shipout_add_foreground_picture:n
1944 % \end{macrocode}
1945 % \InternalDetectionOn
1946 % \end{macro}
1947 %
1948 % \begin{macrocode}
1949 \ExplSyntaxOff
1950 %</2ekernel|latexrelease>
1951 %<latexrelease>\EndIncludeInRelease
1952 % \end{macrocode}
1953 %
1954 %
1955 % Rolling back here doesn't undefine the interface commands as they
1956 % may be used in packages without rollback functionality. So we
1957 % just make them do nothing which may or may not work depending on
1958 % the code usage.
1959 %
1960 % \begin{macrocode}
1961 %<latexrelease>\IncludeInRelease{0000/00/00}%
1962 %<latexrelease> {\shipout}{Hook management (shipout)}%
1963 %<latexrelease>
1964 % \end{macrocode}
1965 % If we roll forward then \cs{tex\_shipout:D} may not be defined in
1966 % which case \cs{shipout} does have it original definition and so
1967 % we must not \cs{let} it to something else which is \cs{relax}!
1968 % \changes{v1.0d}{2020/11/24}{Support for roll forward (gh/434)}
1969 % \begin{macrocode}
1970 %<latexrelease>\ifcsname tex_shipout:D\endcsname
1971 %<latexrelease>\expandafter\let\expandafter\shipout
1972 %<latexrelease> \csname tex_shipout:D\endcsname
1973 %<latexrelease>\fi
1974 %<latexrelease>
1975 %<latexrelease>\let \RawShipout\@undefined
1976 %<latexrelease>\let \ShipoutBox\@undefined
1977 %<latexrelease>\let \ReadonlyShipoutCounter \@undefined
1978 %<latexrelease>\let \c@totalpages \@undefined
1979 %<latexrelease>\let \thetotalpages \@undefined
1980 %<latexrelease>
1981 %<latexrelease>\let \DiscardShipoutBox \@undefined
1982 %<latexrelease>\let \DebugShipoutsOn \@undefined
1983 %<latexrelease>\let \DebugShipoutsOff \@undefined
1984 %<latexrelease>
1985 %<latexrelease>\DeclareRobustCommand \AtBeginDvi [1]{%
1986 %<latexrelease> \global \setbox \@begindvibox
1987 %<latexrelease> \vbox{\unvbox \@begindvibox #1}%
1988 %<latexrelease>}
1989 %<latexrelease>
1990 %<latexrelease>\let \AtBeginShipout \@undefined
1991 %<latexrelease>\let \AtBeginShipoutNext \@undefined
1992 %<latexrelease>
1993 %<latexrelease>\let \AtBeginShipoutFirst \@undefined
1994 %<latexrelease>
1995 %<latexrelease>\let \ShipoutBoxHeight \@undefined
1996 %<latexrelease>\let \ShipoutBoxDepth \@undefined
1997 %<latexrelease>\let \ShipoutBoxWidth \@undefined
1998 %<latexrelease>
1999 % \end{macrocode}
2000 % We do not undo a substitution when rolling back. As the file
2001 % support gets undone the underlying data is no longer used (and
2002 % sufficiently obscure that it should not interfere with existing
2003 % commands) and properly removing it would mean we need to make the
2004 % \cs{undeclare@...} and its support macros available in all earlier
2005 % kernel releases which is pointless (and actually worse).
2006 %
2007 % \begin{macrocode}
2008 %<latexrelease>
2009 %<latexrelease>\let \AtEndDvi \@undefined
2010 % \end{macrocode}
2011 % We do not reenable a disabled package load when rolling back. As the file
2012 % support gets undone the underlying data is no longer checked (and
2013 % sufficiently obscure that it should not interfere with existing
2014 % commands) and properly removing it would mean we need to make the
2015 % \cs{reenable@package@load} command available in all earlier
2016 % kernel releases which is pointless (and actually worse).
2017 % \begin{macrocode}
2018 %\reenable@package@load{atenddvi}
2019 % \end{macrocode}
2020 %
2021 % \begin{macrocode}
2022 %<latexrelease>
2023 %<latexrelease>\EndIncludeInRelease
2024 %<*2ekernel>
2025 % \end{macrocode}
2026 %
2027 %
2028 %
2029 % \begin{macro}{\__shipout_force_immediate_writes:}
2030 % Change all writes to be immediate. To be used after the final
2031 % page has been shipped out.
2032 % \changes{v1.0o}{2025/03/05}{Macro added}
2033 % \changes{v1.0p}{2025/04/27}{Decrement page counter gh/1717}
2034 % \begin{macrocode}
2035 %</2ekernel>
2036 %<*2ekernel|latexrelease>
2037 %<latexrelease>\IncludeInRelease{2025/06/01}%
2038 %<latexrelease> {\__shipout_force_immediate_writes:}{Force immediate writes}%
2039 \ExplSyntaxOn
2040 \cs_new:Npn \__shipout_force_immediate_writes: {
2041 % \end{macrocode}
2042 %
2043 % Need to check if any variants are manually defined and if so
2044 % adjust them too --- not done!
2045 % \begin{macrocode}
2046 \cs_gset_eq:NN \iow_shipout:Nn \iow_now:Nn
2047 \cs_gset_eq:NN \lua_shipout:n \lua_now:n
2048 % \end{macrocode}
2049 %
2050 % \begin{macrocode}
2051 \cs_gset:Npn \write {\tex_immediate:D \tex_write:D}
2052 % \end{macrocode}
2053 % As the writes will happen without a page break, reset the
2054 % page number so they reference the last page.
2055 % \begin{macrocode}
2056 \global\advance\c@page\m@ne
2057 % \end{macrocode}
2058 %
2059 % \begin{macrocode}
2060 }
2061 \ExplSyntaxOff
2062 %</2ekernel|latexrelease>
2063 %<latexrelease>\EndIncludeInRelease
2064 %<latexrelease>\IncludeInRelease{0000/00/00}%
2065 %<latexrelease> {\__shipout_force_immediate_writes:}{Force immediate writes}%
2066 % \end{macrocode}
2067 % We want a definition for this even when doing rollback so that it
2068 % can stay in the kernel hook without forcing a rollback there as well.
2069 % \begin{macrocode}
2070 %<latexrelease>\ExplSyntaxOn
2071 %<latexrelease> \cs_new_eq:NN \__shipout_force_immediate_writes: \prg_do_nothing:
2072 %<latexrelease>\ExplSyntaxOff
2073 %<latexrelease>\EndIncludeInRelease
2074 %<*2ekernel>
2075 % \end{macrocode}
2076 % \end{macro}
2077 %
2078 %
2079 %
2080 %
2081 % \section{Package emulation for compatibility}
2082 %
2083 %
2084 % \subsection{Package \pkg{atenddvi} emulation}
2085 %
2086 %
2087 % \begin{macro}{\AtEndDvi}
2088 % This package has only one public command, so simulating it is easy
2089 % and actually sensible to provide as part of the kernel.
2090 % \changes{v1.0l}{2022/01/06}{Correctly simulate \cs{AtEndDvi} without
2091 % extending the syntax}
2092 % \begin{macrocode}
2093 %</2ekernel>
2094 %<*2ekernel|latexrelease>
2095 %<latexrelease>\IncludeInRelease{2020/10/01}%
2096 %<latexrelease> {\AtEndDvi}{atenddvi emulation}%
2097 \ExplSyntaxOn
2098 \cs_new_protected:Npn \AtEndDvi #1 {\AddToHook{shipout/lastpage}{#1}}
2099 \ExplSyntaxOff
2100 % \end{macrocode}
2101 % As the package is integrate we prevent loading (no need to roll that back):
2102 % \begin{macrocode}
2103 \disable@package@load{atenddvi}
2104 {\PackageWarning{atenddvi}
2105 {Functionality of this package is already\MessageBreak
2106 provided by LaTeX.\MessageBreak\MessageBreak
2107 It is there no longer necessary to load it\MessageBreak
2108 and you can safely remove it.\MessageBreak
2109 Found on}}
2110 % \end{macrocode}
2111 %
2112 % \begin{macrocode}
2113 %</2ekernel|latexrelease>
2114 % \end{macrocode}
2115 %
2116 % \begin{macrocode}
2117 %<latexrelease>\EndIncludeInRelease
2118 %<latexrelease>\IncludeInRelease{0000/00/00}%
2119 %<latexrelease> {\AtEndDvi}{atenddvi emulation}%
2120 %<latexrelease>\let \AtEndDvi \@undefined
2121 %<latexrelease>\EndIncludeInRelease
2122 %<*2ekernel>
2123 % \end{macrocode}
2124 % \end{macro}
2125 %
2126 %
2127 %
2128 % \begin{macrocode}
2129 %</2ekernel>
2130 % \end{macrocode}
2131 %
2132 %
2133 % \subsection{Package \pkg{atbegshi} emulation}
2134 %
2135 %
2136 %
2137 %
2138 %
2139 % \begin{macrocode}
2140 %<*atbegshi-ltx>
2141 \ProvidesPackage{atbegshi-ltx}
2142 [2021/01/10 v1.0c
2143 Emulation of the original atbegshi^^Jpackage with kernel methods]
2144 % \end{macrocode}
2145 %
2146 %
2147 % \begin{macro}{\AtBeginShipoutBox}
2148 % \begin{macrocode}
2149 \let \AtBeginShipoutBox \ShipoutBox
2150 % \end{macrocode}
2151 % \end{macro}
2152 %
2153 %
2154 %
2155 % \begin{macro}{\AtBeginShipoutInit}
2156 % Compatibility only, we aren't delaying \ldots
2157 % \begin{macrocode}
2158 \let \AtBeginShipoutInit \@empty
2159 % \end{macrocode}
2160 % \end{macro}
2161 %
2162 %
2163 % \begin{macro}{\AtBeginShipout,\AtBeginShipoutNext}
2164 % Filling hooks
2165 % \changes{v1.0l}{2022/01/06}{Correctly simulate \cs{AtBeginShipout}
2166 % and \cs{AtBeginShipoutNext} without extending the syntax}
2167 % \begin{macrocode}
2168 \protected\long\def\AtBeginShipout #1{\AddToHook{shipout/before}{#1}}
2169 \protected\long\def\AtBeginShipoutNext #1{\AddToHookNext{shipout/before}{#1}}
2170 % \end{macrocode}
2171 % \end{macro}
2172 %
2173 %
2174 % \begin{macro}{\AtBeginShipoutFirst}
2175 % Slightly more complex as we need to know the name of the command under which the
2176 % \hook{shipout/firstpage} hook is filled.
2177 % \begin{macrocode}
2178 \protected \def \AtBeginShipoutFirst
2179 {\@expl@@@shipout@add@firstpage@material@@Nn \AtBeginShipoutFirst}
2180 % \end{macrocode}
2181 % \end{macro}
2182 %
2183 %
2184 % \begin{macro}{\AtBeginShipoutDiscard}
2185 % Just a different name.
2186 % \begin{macrocode}
2187 \let \AtBeginShipoutDiscard \DiscardShipoutBox
2188 % \end{macrocode}
2189 % \end{macro}
2190 %
2191 %
2192 % \begin{macro}{\AtBeginShipoutAddToBox,\AtBeginShipoutAddToBoxForeground,
2193 % \AtBeginShipoutUpperLeft,\AtBeginShipoutUpperLeftForeground}
2194 % We don't expose them.
2195 % \begin{macrocode}
2196 \let \AtBeginShipoutAddToBox
2197 \@expl@@@shipout@add@background@box@@n
2198 \let \AtBeginShipoutAddToBoxForeground
2199 \@expl@@@shipout@add@foreground@box@@n
2200 % \end{macrocode}
2201 %
2202 % \begin{macrocode}
2203 \let \AtBeginShipoutUpperLeft
2204 \@expl@@@shipout@add@background@picture@@n
2205 \let \AtBeginShipoutUpperLeftForeground
2206 \@expl@@@shipout@add@foreground@picture@@n
2207 % \end{macrocode}
2208 % \end{macro}
2209 %
2210 % \begin{macro}{\AtBeginShipoutOriginalShipout}
2211 % This offers the raw \cs{shipout} primitive of the engine. A page
2212 % shipped out with this is not counted by
2213 % \cs{ReadonlyShipoutCounter} counter and thus the mechanism to
2214 % place \cs{special}s at the very end of the output might fail,
2215 % etc. It should therefore not be used in new applications but is
2216 % only provided to allow running legacy code. For new code use the
2217 % commands provided by the kernel instead.
2218 % \begin{macrocode}
2219 \ExplSyntaxOn
2220 \cs_new_eq:NN \AtBeginShipoutOriginalShipout \tex_shipout:D
2221 % \end{macrocode}
2222 % \end{macro}
2223 %
2224 %
2225 % \begin{macro}[no-user-doc]{\ShipoutBoxHeight,\ShipoutBoxWidth,\ShipoutBoxDepth}
2226 % This is somewhat different from the original in \pkg{atbegshi}
2227 % where \cs{ShipoutBoxHeight} etc.\ only holds the
2228 % \verb=\the\ht<box>= value. This may has some implications in some
2229 % use cases and if that is a problem then it might need changing.
2230 % \begin{macrocode}
2231 \cs_new:Npn \ShipoutBoxHeight { \dim_use:N \l_shipout_box_ht_dim }
2232 \cs_new:Npn \ShipoutBoxDepth { \dim_use:N \l_shipout_box_dp_dim }
2233 \cs_new:Npn \ShipoutBoxWidth { \dim_use:N \l_shipout_box_wd_dim }
2234 \ExplSyntaxOff
2235 % \end{macrocode}
2236 % \end{macro}
2237 %
2238 %
2239 % \begin{macrocode}
2240 %</atbegshi-ltx>
2241 % \end{macrocode}
2242 %
2243 % If the package is requested we substitute the one above:
2244 % \begin{macrocode}
2245 %<*2ekernel>
2246 \declare@file@substitution{atbegshi.sty}{atbegshi-ltx.sty}
2247 %</2ekernel>
2248 % \end{macrocode}
2249 %
2250 %
2251 %
2252 %
2253 %
2254 %
2255 % \subsection{Package \pkg{everyshi} emulation}
2256 %
2257 % This is now directly handled in that package so emulation is not
2258 % necessary any more.
2259 %
2260 %
2261 % Rather important :-)
2262 % \begin{macrocode}
2263 %<@@=>
2264 % \end{macrocode}
2265 %
2266 % \Finale
2267 %
2268 %
2269 %
2270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2271 \endinput
2272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%