Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %%
3 %% Copyright (C) 2020-2025
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 [2025/09/23 v1.0p LaTeX Kernel (Shipout)]
38 % \iffalse
39 %
40 \documentclass{l3doc}
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 \texttt{ltshipout} documentation\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 %
687 % \subsection{Debugging}
688 %
689 % \begin{macro}{\g_@@_debug_bool}
690 % Holds the current debugging state.
691 % \begin{macrocode}
692 \bool_new:N \g_@@_debug_bool
693 % \end{macrocode}
694 % \end{macro}
695 %
696 % \begin{macro}{\shipout_debug_on:,\shipout_debug_off:}
697 % \begin{macro}{\@@_debug:n}
698 % \begin{macro}{\@@_debug_gset:}
699 % Turns debugging on and off by redefining \cs{@@_debug:n}.
700 % \begin{macrocode}
701 \cs_new_eq:NN \@@_debug:n \use_none:n
702 \cs_new_protected:Npn \shipout_debug_on:
703 {
704 \bool_gset_true:N \g_@@_debug_bool
705 \@@_debug_gset:
706 }
707 \cs_new_protected:Npn \shipout_debug_off:
708 {
709 \bool_gset_false:N \g_@@_debug_bool
710 \@@_debug_gset:
711 }
712 \cs_new_protected:Npn \@@_debug_gset:
713 {
714 \cs_gset_protected:Npx \@@_debug:n ##1
715 { \bool_if:NT \g_@@_debug_bool {##1} }
716 }
717 % \end{macrocode}
718 % \end{macro}
719 % \end{macro}
720 % \end{macro}
721 %
722 %
723 %
724 %
725 %
726 % \begin{macro}{\ShipoutBox,\l_shipout_box}
727 % The box filled with the page to be shipped out (both L3 and
728 % \LaTeXe{} name).
729 % \begin{macrocode}
730 \box_new:N \l_shipout_box
731 % \end{macrocode}
732 %
733 % \begin{macrocode}
734 \cs_set_eq:NN \ShipoutBox \l_shipout_box
735 % \end{macrocode}
736 % \end{macro}
737 %
738 %
739 % \begin{macro}{\l_@@_raw_box}
740 % The \cs{RawShipout} gets its own box but it is internal as there
741 % is no hook manipulation for it.
742 % \begin{macrocode}
743 \box_new:N \l_@@_raw_box
744 % \end{macrocode}
745 % \end{macro}
746 %
747 %
748 % \begin{macro}{\@@_finalize_box:}
749 % For Lua\TeX{} invoke the \texttt{pre\_shipout\_filter} callback.
750 % \changes{v1.0i}{2021/01/22}{Add \texttt{pre\_shipout\_filter} Lua callback}
751 % \begin{macrocode}
752 \sys_if_engine_luatex:TF
753 {
754 \newprotectedluacmd \@@_finalize_box:
755 \exp_args:Nx \everyjob {
756 \exp_not:V \everyjob
757 \exp_not:N \lua_now:n {
758 luatexbase.create_callback('pre_shipout_filter', 'list')
759 local~call, getbox, setbox = luatexbase.call_callback, tex.getbox, tex.setbox~
760 lua.get_functions_table()[\the \allocationnumber] = function()
761 local~head = getbox(\the \l_shipout_box)
762 local~result = call('pre_shipout_filter', head)
763 if~not (result == head) then~
764 setbox(\the \l_shipout_box, result~or~nil)
765 end~
766 end
767 }
768 }
769 } {
770 \cs_set_eq:NN \@@_finalize_box: \scan_stop:
771 }
772 % \end{macrocode}
773 % \end{macro}
774 %
775 %
776 % \begin{macro}{\@@_execute:}
777 % This is going to the be the code run by \cs{shipout}. The code
778 % follows closely the ideas from \pkg{atbegshi}, so not
779 % documenting that here for now.
780 % \begin{macrocode}
781 \cs_set_protected:Npn \@@_execute: {
782 \tl_set:Nx \l_@@_group_level_tl
783 { \int_value:w \tex_currentgrouplevel:D }
784 \tex_afterassignment:D \@@_execute_test_level:
785 \tex_setbox:D \l_shipout_box
786 }
787 % \end{macrocode}
788 % \end{macro}
789 %
790 %
791 % \begin{macro}{\shipout}
792 % Overloading the \cs{shipout} primitive:
793 % \begin{macrocode}
794 \cs_gset_eq:NN \shipout \@@_execute:
795 % \end{macrocode}
796 % \end{macro}
797 %
798 %
799 % \begin{macro}{\l_@@_group_level_tl}
800 % Helper token list to record the group level at which
801 % \cs{@@_execute:} is encountered.
802 % \begin{macrocode}
803 \tl_new:N \l_@@_group_level_tl
804 % \end{macrocode}
805 % \end{macro}
806 %
807 %
808 % \begin{macro}{\@@_execute_test_level:}
809 % If the group level has changed then we are still constructing
810 % \cs{l_shipout_box} and to continue we need to wait until the
811 % current group has finished, hence the \cs{tex_aftergroup:D}.
812 % \begin{macrocode}
813 \cs_new:Npn \@@_execute_test_level: {
814 \int_compare:nNnT
815 \l_@@_group_level_tl < \tex_currentgrouplevel:D
816 \tex_aftergroup:D \@@_execute_cont:
817 }
818 % \end{macrocode}
819 % \end{macro}
820 %
821 %
822 % \begin{macro}{\@@_execute_cont:}
823 % This does the actual shipout running several hooks as part of it.
824 % The code for them is passed as argument \verb=#2= to \verb=#4=
825 % to \cs{@@_execute_main_cont:Nnnn}; the first argument is the box
826 % to be shipped out.
827 % \begin{macrocode}
828 \cs_new:Npn \@@_execute_cont: {
829 \@@_execute_main_cont:Nnnn
830 \l_shipout_box
831 { \hook_use:n {shipout/before} }
832 { \hook_if_empty:nF {shipout/foreground}
833 { \@@_add_foreground_picture:n
834 { \hook_use:n {shipout/foreground} } }
835 % \end{macrocode}
836 % If the user hook for the background (\hook{shipout/background}) has
837 % no code, there might still code in the kernel hook so we need to
838 % test for this too.
839 % We only test for the \cs{@kernel@before@shipout@background}
840 % though. If the \cs{@kernel@after@shipout@background} needs
841 % executing even if the user hook is empty then we can add another
842 % test (or the kernel could put something into the before hook).
843 %
844 % \changes{v1.0d}{2020/11/23}{Check for both kernel and user hook (gh/431)}
845 % \changes{v1.0f}{2021/01/08}{Added another kernel hook for more
846 % flexibility (cf.\ \texttt{https://github.com/pgf-tikz/pgf/issues/960}}
847 % \begin{macrocode}
848 \bool_lazy_and:nnF
849 { \hook_if_empty_p:n {shipout/background} }
850 { \tl_if_empty_p:N \@kernel@before@shipout@background }
851 { \@@_add_background_picture:n
852 { \@kernel@before@shipout@background
853 \hook_use:n {shipout/background}
854 \@kernel@after@shipout@background }
855 }
856 }
857 { \hook_use:n {shipout/after} }
858 }
859 % \end{macrocode}
860 % \end{macro}
861 %
862 %
863 % \begin{macro}{\@@_execute_main_cont:Nnnn}
864 % When we have reached this point the shipout box has been
865 % processed and is available in \cs{l_shipout_box} and ready for
866 % real ship out (unless it gets discarded during the process).
867 %
868 % The three arguments hold hook code that is executed just before the
869 % actual shipout (\verb=#1=), within the shipout adding
870 % background and foreground material (\verb=#2=) and after the
871 % shipout has happened (\verb=#3=).
872 % These are passed as arguments because the same code without those
873 % hooks is also used when doing a ``raw'' shipout implemented by
874 % \cs{RawShipout}.
875 % The only hook that is always executed is that for the very last
876 % page, i.e., \hook{shipout/lastpage}.
877 %
878 % First we quickly check if it is void (can't happen in the
879 % standard \LaTeX{} output routine but \cs{shipout} might be called
880 % from a package that has some special processing logic). If it is
881 % void we aren't shipping anything out and processing ends.\footnote{In that
882 % case we don't reset the deadcycles, that would be up to the OR
883 % processing logic to do.}
884 % \begin{macrocode}
885 \cs_new:Npn \@@_execute_main_cont:Nnnn #1#2#3#4 {
886 \box_if_empty:NTF #1
887 { \@latex@warning@no@line{ Ignoring~ void~ shipout~ box } }
888 {
889 % \end{macrocode}
890 % Otherwise we assume that we will ship something and prepare for
891 % final adjustments (in particular setting the state of
892 % \cs{protect} while we are running the hook code).
893 % We also save the current \cs{protect} state to restore it later.
894 % \begin{macrocode}
895 % \bool_gset_false:N \g_@@_discard_bool % setting this would disable
896 % \DiscardShipoutBox on doc-level
897 \cs_set_eq:NN \@@_saved_protect: \protect
898 \set@typeset@protect
899 % \end{macrocode}
900 % We also store the current shipout box dimension in registers, so that
901 % they can be used in the hook code.\footnote{This is not really
902 % necessary as the code could access them via \cs{box_ht:N}, etc.,
903 % but it is perhaps convenient.}
904 % \begin{macrocode}
905 \@@_get_box_size:N #1
906 % \end{macrocode}
907 % Then we execute the \hook{shipout/before} hook (or nothing in
908 % case of \cs{RawShipout}).
909 % \begin{macrocode}
910 #2
911 % \end{macrocode}
912 % In \cs{g_shipout_totalpages_int} we count all shipout attempts so
913 % we increment that counter already here (the other one is
914 % incremented later when we know for sure that we do a
915 % \cs{shipout}.
916 %
917 % We increment it after running the above hook so that the values
918 % for \cs{g_shipout_totalpages_int} and \cs{g_shipout_readonly_int}
919 % are in sync while the
920 % hook is executed (in the case that totalpages isn't manually
921 % altered or through discarding pages that is).
922 % \begin{macrocode}
923 \int_gincr:N \g_shipout_totalpages_int
924 % \end{macrocode}
925 % The above hook might contain code that requests the page to be discarded so
926 % we now test for it.
927 % \begin{macrocode}
928 \bool_if:NTF \g_@@_discard_bool
929 { \@latex@info@no@line{Completed~ page~ discarded}
930 \bool_gset_false:N \g_@@_discard_bool
931 % \end{macrocode}
932 % As we are discarding the page box and not shipping anything out,
933 % we need to do some house cleaning and reset \TeX's deadcycles so
934 % that it doesn't complain about too many calls to the OR without
935 % any shipout.
936 % \begin{macrocode}
937 \tex_deadcycles:D \c_zero_int
938 % \end{macrocode}
939 % \fmi{In \pkg{atbegshi} the box was dropped but is that actually
940 % needed? Or the resetting of \cs{protect} to its kernel value?}
941 % \begin{macrocode}
942 % \group_begin:
943 % \box_set_eq_drop:NN #1 #1
944 % \group_end:
945 % \cs_set_eq:NN \protect \exp_not:N
946 }
947 % \end{macrocode}
948 % Even if there was no explicit request to discard the box it is
949 % possible that the code for the hook \hook{shipout/before} has
950 % voided the box (by mistake or deliberately). We therefore test
951 % once more but this time make it a warning, because the best
952 % practice way is to use the request mechanism.
953 % \begin{macrocode}
954 { \box_if_empty:NTF #1
955 { \@latex@warning@no@line { Ignoring~ void~ shipout~ box.
956 \MessageBreak The~ shipout~ box~ was~ voided~ by~ hook~ code }
957 }
958 % \end{macrocode}
959 % Finally, if the box is still non-empty we are nearly ready to
960 % ship it out.
961 % First we increment the total page counter so that we can later
962 % test if we have reached the final page according to our available
963 % information.\footnote{Doing that earlier would be wrong because we
964 % might end up with the last page counted but discard and then we
965 % have no place to add the final objects into the output file.}
966 % \begin{macrocode}
967 {
968 \int_gincr:N \g_shipout_readonly_int
969 \@@_debug:n {
970 \typeout{Absolute~ page~ =~ \int_use:N \g_shipout_readonly_int
971 \space (target:~ \@abspage@last)}
972 }
973 % \end{macrocode}
974 % Then we store the box sizes again (as they may have
975 % changed) and then look at the hooks \hook{shipout/foreground}
976 % and \hook{shipout/background}. If either or both are non-empty
977 % we add a \texttt{picture} environment to the box (in the
978 % foreground and/or in the background) and execute the hook code
979 % inside that environment.
980 %
981 % \begin{macrocode}
982 \@@_get_box_size:N #1
983 % \end{macrocode}
984 % The first hook we run is the \hook{shipout/firstpage} hook. This
985 % is only done once, then the \cs{@@_run_firstpage_hook:}
986 % command redefines itself to do nothing. If the hook contains
987 % \cs{special}s for integration at the top of the page they will be
988 % temporarily stored in a safe place and added later with
989 % \cs{@@_add_firstpage_specials:}.
990 % \begin{macrocode}
991 \@@_run_firstpage_hook:
992 % \end{macrocode}
993 % Run the hooks for background and foreground or, if this
994 % is called by \cs{RawShipout}, copy the box \cs{l_@@_raw_box} to
995 % \cs{l_shipout_box} so that firstpage and lastpage material gets
996 % added if necessary (that is always done to \cs{l_shipout_box}.
997 % \begin{macrocode}
998 #3
999 % \end{macrocode}
1000 % We then run \cs{@@_add_firstpage_specials:} that adds
1001 % the content of the hook \hook{shipout/firstpage} to the
1002 % start of the first page (if non-empty). It is then redefined to
1003 % do nothing on later pages.
1004 % \begin{macrocode}
1005 \@@_add_firstpage_specials:
1006 % \end{macrocode}
1007 % Then we check if we have to add the \hook{shipout/lastpage} hook
1008 % or the corresponding kernel hook
1009 % because we have reached the last page. This test will be false for
1010 % all but one (and hopefully the correct) page.
1011 % \changes{v1.0d}{2020/11/23}{Check for both kernel and user hook (gh/431)}
1012 % \begin{macrocode}
1013 \int_compare:nNnT \@abspage@last = \g_shipout_readonly_int
1014 { \bool_lazy_and:nnF
1015 { \hook_if_empty_p:n {shipout/lastpage} }
1016 { \tl_if_empty_p:N \@kernel@after@shipout@lastpage }
1017 { \@@_debug:n { \typeout{Executing~ lastpage~ hook~
1018 on~ page~ \int_use:N \g_shipout_readonly_int } }
1019 \_@@_add_foreground_box:n
1020 { \UseHook{shipout/lastpage}
1021 \@kernel@after@shipout@lastpage }
1022 % \end{macrocode}
1023 % We record that we have handled the \hook{shipout/lastpage} hook
1024 % but only if we really did.
1025 % \changes{v1.0m}{2022/05/08}{Handle case where shipout/lastpage is
1026 % run too early (gh/813)}
1027 % \begin{macrocode}
1028 \bool_gset_true:N \g_@@_lastpage_handled_bool
1029 }
1030 }
1031 % \end{macrocode}
1032 %
1033 % \changes{v1.0n}{2022/11/08}{Add shipout hook (gh/920)}
1034 % \begin{macrocode}
1035 \hook_use:n {shipout}
1036 \@@_finalize_box:
1037 % \end{macrocode}
1038 % Finally we run the actual \TeX{} primitive for shipout. As that will
1039 % expand delayed \cs{write} statements inside the page in which
1040 % protected commands should not expand we first change \cs{protect}
1041 % to the appropriate definition for that case.
1042 % \begin{macrocode}
1043 \cs_set_eq:NN \protect \exp_not:N
1044 \tex_shipout:D \box_use:N \l_shipout_box
1045 % \end{macrocode}
1046 % The \cs{l_shipout_box} may contain the firstpage material if this
1047 % was the very first shipout. That makes it unsuitable for reuse in
1048 % another shipout, so as a safety measure the next command resets
1049 % \cs{l_shipout_box} to its earlier state if that is necessary. On
1050 % later pages this is then a no-op.
1051 % \begin{macrocode}
1052 \@@_drop_firstpage_specials:
1053 % \end{macrocode}
1054 % The \hook{shipout/after} hook (if in \verb=#4=) needs to run with
1055 % \cs{protect}ed commands again being executed, because that hook
1056 % will ``typeset'' material added at the top of the next page.
1057 % \begin{macrocode}
1058 \set@typeset@protect
1059 #4
1060 }
1061 }
1062 % \end{macrocode}
1063 % Restore the value of \cs{protect} in case \cs{shipout} is called
1064 % outside of the output routine (where it is automatically restored
1065 % because of the implicit group).
1066 % \begin{macrocode}
1067 \cs_set_eq:NN \protect \@@_saved_protect:
1068 }
1069 }
1070 % \end{macrocode}
1071 % \end{macro}
1072 %
1073 %
1074 % \begin{macro}{\@@_execute_raw:,
1075 % \@@_execute_test_level_raw:}
1076 %
1077 % This implements the ``raw'' shipout which bypasses the before,
1078 % foreground, background and after hooks. It follows the same pattern than
1079 % \cs{_@@_execute_raw:} except that it finally calls
1080 % \cs{_@@_execute_main_cont:Nnnn} with three empty arguments.
1081 % instead of the hook code.
1082 % \begin{macrocode}
1083 \cs_set_protected:Npn \@@_execute_raw: {
1084 \tl_set:Nx \l_@@_group_level_tl
1085 { \int_value:w \tex_currentgrouplevel:D }
1086 \tex_afterassignment:D \@@_execute_test_level_raw:
1087 \tex_setbox:D \l_@@_raw_box
1088 }
1089 % \end{macrocode}
1090 %
1091 % \begin{macrocode}
1092 \cs_new:Npn \@@_execute_test_level_raw: {
1093 \int_compare:nNnT
1094 \l_@@_group_level_tl < \tex_currentgrouplevel:D
1095 \tex_aftergroup:D \@@_execute_nohooks_cont:
1096 }
1097 % \end{macrocode}
1098 % Well, not totally empty arguments, we add some debugging if we
1099 % are actually doing a shipout.
1100 % \begin{macrocode}
1101 \cs_new:Npn \@@_execute_nohooks_cont: {
1102 \@@_execute_main_cont:Nnnn \l_@@_raw_box
1103 {} { \@@_debug:n{ \typeout{Doing~ raw~ shipout~ ...} }
1104 \box_set_eq:NN \l_shipout_box \l_@@_raw_box } {}
1105 }
1106 % \end{macrocode}
1107 % \end{macro}
1108
1109 % \begin{macro}{\RawShipout}
1110 % The interface name for raw shipout.
1111 % \changes{v1.0g}{2021/01/10}{Macro added}
1112 % \begin{macrocode}
1113 \cs_gset_eq:NN \RawShipout \@@_execute_raw:
1114 % \end{macrocode}
1115 % \end{macro}
1116 %
1117 %
1118 %
1119 %
1120 % \begin{macro}{\@@_saved_protect:}
1121 % Remember the current \cs{protect} state.
1122 % \begin{macrocode}
1123 \cs_new_eq:NN \@@_saved_protect: \protect
1124 % \end{macrocode}
1125 % \end{macro}
1126 %
1127 % \begin{macro}{shipout/before,shipout,shipout/after,
1128 % shipout/foreground,shipout/background,
1129 % shipout/firstpage,shipout/lastpage}
1130 % Declaring all hooks for the shipout code.
1131 % \changes{v1.0n}{2022/11/08}{Add shipout hook (gh/920)}
1132 % \begin{macrocode}
1133 \hook_new:n{shipout/before}
1134 \hook_new:n{shipout}
1135 \hook_new:n{shipout/after}
1136 \hook_new:n{shipout/foreground}
1137 \hook_new:n{shipout/background}
1138 \hook_new:n{shipout/firstpage}
1139 \hook_new:n{shipout/lastpage}
1140 % \end{macrocode}
1141 % \end{macro}
1142 %
1143 %
1144 %
1145 % \begin{macro}{\@kernel@after@shipout@lastpage,
1146 % \@kernel@before@shipout@background,
1147 % \@kernel@after@shipout@background}
1148 % And here are the internal kernel hooks going before or after the
1149 % public ones where needed.
1150 % \changes{v1.0g}{2021/01/10}{Internal hook
1151 % \cs{@kernel@after@shipout@background} added}
1152 % \begin{macrocode}
1153 \let\@kernel@after@shipout@lastpage\@empty
1154 \let\@kernel@before@shipout@background\@empty
1155 \let\@kernel@after@shipout@background\@empty
1156 % \end{macrocode}
1157 % \end{macro}
1158 %
1159 %
1160 % \begin{macro}{\@@_run_firstpage_hook:}
1161 %
1162 % \changes{v1.0h}{2021/01/19}{Handling of firstpage hook altered}
1163 %
1164 % There are three commands to handle the \hook{shipout/firstpage}
1165 % hook:
1166 % \cs{@@_run_firstpage_hook:}, \cs{@@_add_firstpage_specials:} and
1167 % \cs{@@_drop_firstpage_specials:}.
1168 %
1169 % That hook is supposed to contain \cs{special}s and similar
1170 % material to be placed at the very beginning of the output page
1171 % and so it needs careful placing to avoid that anything else gets
1172 % in front of it. And this means we have to wait with this until
1173 % other hooks such as \hook{shipout/background} have added their
1174 % bits. It is also important that such \cs{special}s show up only
1175 % on the very first page, so if this page gets saved before
1176 % \cs{shipout} for later reuse, we have to make sure that they
1177 % aren't in the saved version.
1178 %
1179 % In addition the hook may also contain code to be executed ``first'', e.g.,
1180 % visible from code in \hook{shipout/background} and this conflicts
1181 % with adding the \cs{special}s late.
1182 %
1183 % Therefore the processing is split into different parts:
1184 % \cs{@@_run_firstpage_hook:} is done early and checks if there is
1185 % any material in the hook.
1186 % \begin{macrocode}
1187 \cs_new:Npn \@@_run_firstpage_hook: {
1188 \hook_if_empty:nTF {shipout/firstpage}
1189 % \end{macrocode}
1190 % If not then we define the other two commands to do nothing.
1191 % \begin{macrocode}
1192 {
1193 \cs_gset_eq:NN \@@_add_firstpage_specials: \prg_do_nothing:
1194 \cs_gset_eq:NN \@@_drop_firstpage_specials: \prg_do_nothing:
1195 }
1196 % \end{macrocode}
1197 % If there is material we execute inside a box, which means any
1198 % \cs{special} will end up in that box and any other code is
1199 % executed and can have side effects (as long as they are global).
1200 % \begin{macrocode}
1201 {
1202 \hbox_set:Nn \l_@@_firstpage_box { \UseHook{shipout/firstpage} }
1203 }
1204 % \end{macrocode}
1205 % Once we are here we change the definition to do nothing next time
1206 % and we also change the command used to implement \cs{AtBeginDvi}
1207 % to become a warning and not add further material to a hook that
1208 % is never used again.
1209 % \begin{macrocode}
1210 \cs_gset_eq:NN \@@_run_firstpage_hook: \prg_do_nothing:
1211 \cs_gset:Npn \@@_add_firstpage_material:Nn ##1 ##2 {
1212 \@latex@warning{ First~ page~ is~ already~ shipped~ out,~ ignoring
1213 \MessageBreak \string##1 }
1214 }
1215 }
1216 % \end{macrocode}
1217 % \end{macro}
1218 %
1219 %
1220 %
1221 % \begin{macro}{\@@_add_firstpage_specials:,\@@_drop_firstpage_specials:}
1222 % The \cs{@@_add_firstpage_specials:} then adds the \cs{special}s
1223 % stored in \cs{l_@@_firstpage_box} to the page to be shipped out
1224 % when the time is ready. Note that if there was no material in the
1225 % \hook{shipout/firstpage} hook then this command gets redefined to
1226 % do nothing. But for most documents there is something, e.g., some
1227 % PostScript header, or some metadata declaration, etc.\ so by
1228 % default we assume there is something to do.
1229 % \begin{macrocode}
1230 \cs_new:Npn \@@_add_firstpage_specials: {
1231 % \end{macrocode}
1232 % First we make a copy of the \cs{l_shipout_box} that we can
1233 % restore it later on.
1234 % \begin{macrocode}
1235 \box_set_eq:NN \l_@@_raw_box \l_shipout_box
1236 % \end{macrocode}
1237 % Adding something to the beginning means adding it to the
1238 % background as that layer is done first in the output.
1239 % \begin{macrocode}
1240 \@@_add_background_box:n { \hbox_unpack_drop:N \l_@@_firstpage_box }
1241 % \end{macrocode}
1242 % After the actual shipout \cs{@@_drop_firstpage_specials:} is
1243 % run to
1244 % restore the earlier content of \cs{l_shipout_box} and then
1245 % redefines itself again to do nothing.
1246 %
1247 % As a final act we change the definition to do nothing next time.
1248 % \begin{macrocode}
1249 \cs_gset_eq:NN \@@_add_firstpage_specials: \prg_do_nothing:
1250 }
1251 % \end{macrocode}
1252 %
1253 % The \cs{@@_drop_firstpage_specials:} is run after the shipout has
1254 % occurred but before the \hook{shipout/afterpage} hook is executed.
1255 % That is the point where we have to restore the \cs{ShipoutBox} to
1256 % its state without the \hook{shipout/firstpage} material.
1257 % \begin{macrocode}
1258 \cs_new:Npn \@@_drop_firstpage_specials: {
1259 \box_set_eq:NN \l_shipout_box \l_@@_raw_box
1260 % \end{macrocode}
1261 % If there was no such material then \cs{@@_run_firstpage_hook:}
1262 % will have changed the definition to a no-op already. Otherwise
1263 % this is what we do here.
1264 % \begin{macrocode}
1265 \cs_gset_eq:NN \@@_drop_firstpage_specials: \prg_do_nothing:
1266 }
1267 % \end{macrocode}
1268 % \end{macro}
1269 %
1270
1271 % \begin{macro}{\l_@@_firstpage_box}
1272 % The box to hold any firstpage \cs{special}s.
1273 % \begin{macrocode}
1274 \box_new:N \l_@@_firstpage_box
1275 % \end{macrocode}
1276 % \end{macro}
1277
1278 %
1279 % \begin{macro}{\g_@@_lastpage_handled_bool}
1280 % A boolean to signal if we have already handled the
1281 % \hook{shipout/lastpage} hook.
1282 % \begin{macrocode}
1283 \bool_new:N \g_@@_lastpage_handled_bool
1284 % \end{macrocode}
1285 % \end{macro}
1286 %
1287 %
1288 %
1289 % \begin{macro}{\@@_add_firstpage_material:Nn}
1290 % This command adds material to the
1291 % \hook{shipout/firstpage} hook. It is used in
1292 % \cs{AtBeginDvi}, etc. The first argument is the
1293 % command through which is it called. Initially this is ignored but
1294 % once we are passed the first page it can be used to generate a
1295 % warning message mentioning the right user command.
1296 % \begin{macrocode}
1297 \cs_new:Npn \@@_add_firstpage_material:Nn #1#2 {
1298 \AddToHook{shipout/firstpage}{#2}
1299 }
1300 % \end{macrocode}
1301 % \end{macro}
1302 %
1303 %
1304 %
1305 %
1306 %
1307 %
1308 %
1309 % \begin{macro}{\@@_get_box_size:N}
1310 % Store the box dimensions in dimen registers.
1311 % \fmi{This could/should perhaps be generalized to set height depth and
1312 % width given an arbitrary box.}
1313 % \begin{macrocode}
1314 \cs_new:Npn \@@_get_box_size:N #1 {
1315 \dim_set:Nn \l_shipout_box_ht_dim { \box_ht:N #1 }
1316 \dim_set:Nn \l_shipout_box_dp_dim { \box_dp:N #1 }
1317 \dim_set:Nn \l_shipout_box_wd_dim { \box_wd:N #1 }
1318 \dim_set:Nn \l_shipout_box_ht_plus_dp_dim
1319 { \l_shipout_box_ht_dim + \l_shipout_box_dp_dim }
1320 }
1321 % \end{macrocode}
1322 % \end{macro}
1323 %
1324 % \begin{macro}{\l_shipout_box_ht_dim,
1325 % \l_shipout_box_dp_dim,\l_shipout_box_wd_dim,
1326 % \l_shipout_box_ht_plus_dp_dim}
1327 % And here are the variables set by \cs{@@_get_box_size:N}.
1328 % \begin{macrocode}
1329 \dim_new:N \l_shipout_box_ht_dim
1330 \dim_new:N \l_shipout_box_dp_dim
1331 \dim_new:N \l_shipout_box_wd_dim
1332 \dim_new:N \l_shipout_box_ht_plus_dp_dim
1333 % \end{macrocode}
1334 % \end{macro}
1335 %
1336 %
1337 %
1338 %
1339 %
1340 % \begin{macro}{\g_@@_discard_bool}
1341 % Indicate whether or not the current page box should be discarded
1342 % \begin{macrocode}
1343 \bool_new:N \g_@@_discard_bool
1344 % \end{macrocode}
1345 % \end{macro}
1346 %
1347 %
1348 %
1349 % \begin{macro}{\l_@@_tmp_box,\l_@@_saved_badness_tl}
1350 % We need a box for the background and foreground material and a
1351 % token register to remember badness settings as we disable them
1352 % during the buildup below.
1353 % \begin{macrocode}
1354 \box_new:N \l_@@_tmp_box
1355 \tl_new:N \l_@@_saved_badness_tl
1356 % \end{macrocode}
1357 % \end{macro}
1358 %
1359 %
1360 % \begin{macro}{\@@_add_background_box:n}
1361 % In standard \LaTeX{} the shipout box is always a \cs{vbox} but
1362 % here we are allow for other usage as well, in case some package
1363 % has its own output routine.
1364 % \begin{macrocode}
1365 \cs_new:Npn \@@_add_background_box:n #1
1366 { \@@_get_box_size:N \l_shipout_box
1367 % \end{macrocode}
1368 % But we start testing for a vertical box as that should be the
1369 % normal case.
1370 % \begin{macrocode}
1371 \box_if_vertical:NTF \l_shipout_box
1372 {
1373 % \end{macrocode}
1374 % Save current values of \cs{vfuzz} and \cs{vbadness} then change
1375 % them to allow box manipulations without warnings.
1376 % \begin{macrocode}
1377 \tl_set:Nx \l_@@_saved_badness_tl
1378 { \vfuzz=\the\vfuzz\relax
1379 \vbadness=\the\vbadness\relax }
1380 \vfuzz=\c_max_dim
1381 \vbadness=\c_max_int
1382 % \end{macrocode}
1383 % Then we reconstruct \cs{l_shipout_box} \ldots
1384 % \begin{macrocode}
1385 \vbox_set_to_ht:Nnn \l_shipout_box \l_shipout_box_ht_plus_dp_dim
1386 {
1387 % \end{macrocode}
1388 % \ldots{} the material in \verb=#1= is placed into a horizontal
1389 % box with zero dimensions.
1390 % \begin{macrocode}
1391 \hbox_set:Nn \l_@@_tmp_box
1392 { \l_@@_saved_badness_tl #1 }
1393 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1394 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1395 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1396 % \end{macrocode}
1397 % The we typeset that box followed by whatever was in
1398 % \cs{l_shipout_box} before (unpacked).
1399 % \begin{macrocode}
1400 \skip_zero:N \baselineskip
1401 \skip_zero:N \lineskip
1402 \skip_zero:N \lineskiplimit
1403 \box_use:N \l_@@_tmp_box
1404 \vbox_unpack:N \l_shipout_box
1405 % \end{macrocode}
1406 % The \cs{kern} ensures that the box has no depth which is
1407 % afterwards explicitly corrected.
1408 % \begin{macrocode}
1409 \kern \c_zero_dim
1410 }
1411 \box_set_ht:Nn \l_shipout_box \l_shipout_box_ht_dim
1412 \box_set_dp:Nn \l_shipout_box \l_shipout_box_dp_dim
1413 % \end{macrocode}
1414 % \fmi{The whole boxing maneuver looks a bit like overkill to me, but for
1415 % the moment I leave.}
1416 % \begin{macrocode}
1417 \l_@@_saved_badness_tl
1418 }
1419 {
1420 % \end{macrocode}
1421 % A horizontal box is handled in a similar way. The last case would
1422 % be a void box in which case we do nothing hence the missing
1423 % \texttt{F} branch.
1424 % \begin{macrocode}
1425 \box_if_horizontal:NT \l_shipout_box
1426 {
1427 \tl_set:Nx \l_@@_saved_badness_tl
1428 { \hfuzz=\the\hfuzz\relax
1429 \hbadness=\the\hbadness\relax }
1430 \hfuzz=\c_max_dim
1431 \hbadness=\c_max_int
1432 \hbox_set_to_wd:Nnn \l_shipout_box \l_shipout_box_wd_dim
1433 {
1434 \hbox_set:Nn \l_@@_tmp_box
1435 { \l_@@_saved_badness_tl #1 }
1436 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1437 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1438 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1439 \box_move_up:nn
1440 \l_shipout_box_ht_dim
1441 { \box_use:N \l_@@_tmp_box }
1442 \hbox_unpack:N \l_shipout_box
1443 }
1444 \l_@@_saved_badness_tl
1445 }
1446 }
1447 }
1448 % \end{macrocode}
1449 % \end{macro}
1450 %
1451 %
1452 %
1453 %
1454 % \begin{macro}{\@@_add_foreground_box:n}
1455 % Foreground boxes are done in the same way, only the order and
1456 % placement of boxes has to be done differently.
1457 % \begin{macrocode}
1458 \cs_new:Npn \@@_add_foreground_box:n #1
1459 {
1460 \box_if_vertical:NTF \l_shipout_box
1461 {
1462 \tl_set:Nx \l_@@_saved_badness_tl
1463 { \vfuzz=\the\vfuzz\relax
1464 \vbadness=\the\vbadness\relax }
1465 \vfuzz=\c_max_dim
1466 \vbadness=\c_max_int
1467 \vbox_set_to_ht:Nnn \l_shipout_box \l_shipout_box_ht_plus_dp_dim
1468 {
1469 \hbox_set:Nn \l_@@_tmp_box
1470 { \l_@@_saved_badness_tl #1 }
1471 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1472 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1473 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1474 \skip_zero:N \baselineskip
1475 \skip_zero:N \lineskip
1476 \skip_zero:N \lineskiplimit
1477 \vbox_unpack:N \l_shipout_box
1478 \kern -\l_shipout_box_ht_plus_dp_dim
1479 \box_use:N \l_@@_tmp_box
1480 \kern \l_shipout_box_ht_plus_dp_dim
1481 }
1482 \l_@@_saved_badness_tl
1483 \box_set_ht:Nn \l_shipout_box \l_shipout_box_ht_dim
1484 \box_set_dp:Nn \l_shipout_box \l_shipout_box_dp_dim
1485 }
1486 {
1487 \box_if_horizontal:NT \l_shipout_box
1488 {
1489 \tl_set:Nx \l_@@_saved_badness_tl
1490 { \hfuzz=\the\hfuzz\relax
1491 \hbadness=\the\hbadness\relax }
1492 \hfuzz=\c_max_dim
1493 \hbadness=\c_max_int
1494 \hbox_set_to_wd:Nnn \l_shipout_box \l_shipout_box_wd_dim
1495 {
1496 \hbox_unpack:N \l_shipout_box
1497 \kern -\box_wd:N \l_shipout_box
1498 \hbox_set:Nn \l_@@_tmp_box
1499 { \l_@@_saved_badness_tl #1 }
1500 \box_set_wd:Nn \l_@@_tmp_box \c_zero_dim
1501 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1502 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1503 \box_move_up:nn { \box_ht:N \l_shipout_box }
1504 { \box_use:N \l_@@_tmp_box }
1505 \kern \box_wd:N \l_shipout_box
1506 }%
1507 \l_@@_saved_badness_tl
1508 }
1509 }
1510 }
1511 % \end{macrocode}
1512 % \end{macro}
1513 %
1514 %
1515 %
1516 %
1517 % \begin{macro}{\@@_init_page_origins:,\c_@@_horigin_tl,\c_@@_vorigin_tl}
1518 % Two constants holding the offset of the top-left with respect to
1519 % the media box.
1520 %
1521 % Setting the constants this way is courtesy of Bruno.
1522 %
1523 % We delay setting the constants to the last possible place as
1524 % there might be updates in the preamble or even in the
1525 % \hook{begindocument} hook that affects their setup.
1526 % \begin{macrocode}
1527 \cs_new:Npn \@@_init_page_origins: {
1528 \tl_const:Nx \c_@@_horigin_tl
1529 {
1530 \cs_if_exist_use:NTF \pdfvariable { horigin }
1531 { \cs_if_exist_use:NF \pdfhorigin { 1in } }
1532 }
1533 \tl_const:Nx \c_@@_vorigin_tl
1534 {
1535 \cs_if_exist_use:NTF \pdfvariable { vorigin }
1536 { \cs_if_exist_use:NF \pdfvorigin { 1in } }
1537 }
1538 % \end{macrocode}
1539 % After the constants have been set there is no need to execute
1540 % this command again, in fact it would raise an error, so we
1541 % redefine it to do nothing.
1542 % \begin{macrocode}
1543 \cs_gset_eq:NN \@@_init_page_origins: \prg_do_nothing:
1544 }
1545 % \end{macrocode}
1546 % \end{macro}
1547 %
1548 %
1549 % \begin{macro}{\@@_picture_overlay:n}
1550 % Put the argument into a \texttt{picture} environment that doesn't take up
1551 % any size and uses \texttt{1pt} for \cs{unitlength}.
1552 % \fmi{Could perhaps be generalized as it might be useful elsewhere. For
1553 % now it is not.}
1554 % \begin{macrocode}
1555 \cs_new:Npn \@@_picture_overlay:n #1 {
1556 % \end{macrocode}
1557 % The very first time this is executed we have to initialize (and
1558 % freeze) the origins.
1559 % \begin{macrocode}
1560 \@@_init_page_origins:
1561 % \end{macrocode}
1562 %
1563 % \begin{macrocode}
1564 \kern -\c_@@_horigin_tl \scan_stop:
1565 \vbox_to_zero:n {
1566 \kern -\c_@@_vorigin_tl \scan_stop:
1567 \unitlength 1pt \scan_stop:
1568 % \end{macrocode}
1569 % This mimics a simple zero-sized picture environment. The \cs{hss}
1570 % is need in case there is horizontal material (without using
1571 % \cs{put} with a positive width.
1572 % \changes{v1.0b}{2020/09/09}
1573 % {Prevent overfull box warnings (gh/387)}
1574 % \begin{macrocode}
1575 \hbox_set_to_wd:Nnn \l_@@_tmp_box \c_zero_dim
1576 { \ignorespaces #1 \hss }
1577 \box_set_ht:Nn \l_@@_tmp_box \c_zero_dim
1578 \box_set_dp:Nn \l_@@_tmp_box \c_zero_dim
1579 \box_use:N \l_@@_tmp_box
1580 \tex_vss:D
1581 }
1582 }
1583 % \end{macrocode}
1584 % \end{macro}
1585 %
1586 %
1587 % \begin{macro}{\@@_add_background_picture:n}
1588 % Put a \texttt{picture} env in the background of the shipout box
1589 % with its reference point in the top-left corner.
1590 %
1591 % \begin{macrocode}
1592 \cs_new:Npn \@@_add_background_picture:n #1 {
1593 \@@_add_background_box:n { \@@_picture_overlay:n {#1} }
1594 }
1595 % \end{macrocode}
1596 % \end{macro}
1597 %
1598 %
1599 %
1600 % \begin{macro}{\@@_add_foreground_picture:n}
1601 %
1602 % Put a \texttt{picture} env in the foreground of the shipout box
1603 % with its reference point in the top-left corner.
1604 % \begin{macrocode}
1605 \cs_new:Npn \@@_add_foreground_picture:n #1 {
1606 \@@_add_foreground_box:n { \@@_picture_overlay:n {#1} }
1607 }
1608 % \end{macrocode}
1609 % \end{macro}
1610 %
1611 %
1612 % \begin{macro}{\shipout_discard:}
1613 % Request that the next shipout box should be discarded. At the
1614 % moment this is just setting a boolean, but we may want to augment
1615 % this behavior that the position of the call is taken into account
1616 % (in case \LaTeX{} looks ahead and is not using the position for
1617 % on the next page).
1618 % \begin{macrocode}
1619 \cs_new_protected:Npn \shipout_discard: {
1620 \bool_gset_true:N \g_@@_discard_bool
1621 }
1622 % \end{macrocode}
1623 % \end{macro}
1624 %
1625 %
1626 %
1627 %
1628 % \subsection{Handling the end of job hook}
1629 %
1630 % At the moment this is partly solved by using the existing hooks.
1631 % But rather than putting the code into these hooks it should be
1632 % moved to the right place directly as we shouldn't prefill hooks
1633 % with material unless it needs to interact with other code.
1634 %
1635 %
1636 %
1637 % \begin{macro}{\g_shipout_readonly_int,\ReadonlyShipoutCounter}
1638 % We count every shipout activity that makes a page (but not those
1639 % that are discarded) in order to know how many pages got produced.
1640 % \begin{macrocode}
1641 \int_new:N \g_shipout_readonly_int
1642 % \end{macrocode}
1643 % For \LaTeXe{} it is available as a command (i.e., a \TeX{}
1644 % counter only.
1645 % \begin{macrocode}
1646 \cs_new_eq:NN \ReadonlyShipoutCounter \g_shipout_readonly_int
1647 % \end{macrocode}
1648 % \end{macro}
1649 %
1650 % \begin{macro}{\g_shipout_totalpages_int,\c@totalpages}
1651 % We count every shipout attempt (even those that are discarded) in
1652 % this counter. It is not used in the code but may get used in user
1653 % code.
1654 % \begin{macrocode}
1655 \int_new:N \g_shipout_totalpages_int
1656 % \end{macrocode}
1657 % For \LaTeXe{} this is offered as a \LaTeX{} counter so can be
1658 % easily typeset inside the output routine to display things like
1659 % \enquote{\cs{thepage}\texttt{/}\cs{thetotalpages}}, etc.
1660 % \begin{macrocode}
1661 \cs_new_eq:NN \c@totalpages \g_shipout_totalpages_int
1662 \cs_new:Npn \thetotalpages { \arabic{totalpages} }
1663 % \end{macrocode}
1664 % \end{macro}
1665 %
1666 %
1667 %
1668 %
1669 % \begin{macro}{\@abspage@last}
1670 % In \cs{@abspage@last} record the number of pages from the last
1671 % run. This is written to the \texttt{.aux} and this way made
1672 % available to the next run. In case there is no \texttt{.aux} file
1673 % or the statement is missing from it we initialize it with the
1674 % largest possible number in \TeX{}. We use this as the default
1675 % because then we are inserting the \hook{shipout/lastpage} on
1676 % the last page (or after the last page) but not on page 1 for a
1677 % multipage document.
1678 % \begin{macrocode}
1679 \xdef\@abspage@last{\number\maxdimen}
1680 % \end{macrocode}
1681 % \end{macro}
1682 %
1683 %
1684 % \begin{macro}{\enddocument}
1685 %
1686 % Instead of using the hooks \hook{enddocument} and
1687 % \hook{enddocument/afterlastpage} we add this code to private
1688 % kernel hooks to be 100\% sure when it is executed and to avoid
1689 % cluttering the hooks with data that is always there.
1690 %
1691 % Inside \cs{enddocument} there is a \cs{clearpage}. Just before
1692 % that we execute this code here. There is a good chance that we
1693 % are on the last page. Therefore, if we don't know the value from
1694 % the last run, we assume that the current page is the right
1695 % one. So we set \cs{@abspage@last} and as a result the next
1696 % shipout will run the \hook{shipout/lastpage} code. Of course,
1697 % if there are floats that still need a placement this guess will
1698 % be wrong but then rerunning the document will give us the correct
1699 % value next time around.
1700 %
1701 % \begin{macro}{\@kernel@after@enddocument}
1702 % \begin{macrocode}
1703 \g@addto@macro \@kernel@after@enddocument {
1704 \int_compare:nNnT \@abspage@last = \maxdimen
1705 {
1706 % \end{macrocode}
1707 % We use \LaTeXe{} coding as \cs{@abspage@last} is not an L3 name.
1708 % \begin{macrocode}
1709 \xdef\@abspage@last{ \int_eval:n {\g_shipout_readonly_int + 1} }
1710 }
1711 }
1712 % \end{macrocode}
1713 % \end{macro}
1714 %
1715 % \begin{macro}{\@kernel@before@enddocument@afterlastpage}
1716 % After the last page has been shipped out, we force further
1717 % \cs{write} calls to be always \cs{immediate} because we aren't
1718 % going to ship out any more pages. This goes before the
1719 % \texttt{enddocument/afterlastpage} hook because that may contain
1720 % such \cs{write} commands.
1721 % \changes{v1.4p}{2025/03/05}{Make all \cs{write}s immediate after
1722 % the last page (gh/1689)}
1723 % \begin{macrocode}
1724 \g@addto@macro \@kernel@before@enddocument@afterlastpage {
1725 \@@_force_immediate_writes:
1726 % This is also the point where \__tag_lastpage_label: could be executed
1727 % instead of going into enddocument/afterlastpage
1728 }
1729 % \end{macrocode}
1730 % \end{macro}
1731 %
1732 %
1733 % \begin{macro}{\@kernel@after@enddocument@afterlastpage}
1734 % Once the \cs{clearpage} has done its work inside \cs{enddocument}
1735 % we know for sure how many pages this document has, so we record
1736 % that in the \texttt{.aux} file for the next run.
1737 % \begin{macrocode}
1738 \g@addto@macro \@kernel@after@enddocument@afterlastpage {
1739 % \end{macrocode}
1740 % There is one special case: If no output is produced then there is
1741 % no point in a) recording the number as 0 will never match the
1742 % page number of a real page and b) adding an extra page to ran the
1743 % \hook{shipout/lastpage} is pointless as well (as it would
1744 % remain forever). So we test for this and run the code only if
1745 % there have been pages.
1746 % \begin{macrocode}
1747 \int_compare:nNnF \g_shipout_readonly_int = 0
1748 {
1749 % \end{macrocode}
1750 % This ends up in the \texttt{.aux} so we use \LaTeXe{} names here.
1751 % \fmi{This needs an interface for \cs{nofiles} in expl3, doesn't at the moment!}
1752 % \begin{macrocode}
1753 \if@filesw
1754 \iow_now:Nx \@auxout {
1755 \gdef\string\@abspage@last {\int_use:N \g_shipout_readonly_int}}
1756 \fi
1757 % \end{macrocode}
1758 % But we may have guessed wrongly earlier and have run it too early
1759 % or we still have to run the
1760 % \hook{shipout/lastpage} even though there is no page to place
1761 % it into. If that is the case we make a trivial extra page and put
1762 % it there. This temporary page will then vanish again on the next
1763 % run but helps to keep pdf viewers happy.
1764 % In either case we should put out an appropriate ``rerun'' warning.
1765 % \changes{v1.0m}{2022/05/08}{Handle case where shipout/lastpage is
1766 % run too early (gh/813)}
1767 % \begin{macrocode}
1768 \bool_if:NTF \g_@@_lastpage_handled_bool
1769 {
1770 % \end{macrocode}
1771 % If the hook was already executed, we have to test if that total
1772 % shipouts match the shipouts from last run (because that
1773 % corresponds to the page it was executed). If not we output a warning.
1774 % \begin{macrocode}
1775 \int_compare:nNnF \@abspage@last = \g_shipout_readonly_int
1776 {
1777 \@latex@warning@no@line{Hook~ 'shipout/lastpage'~ executed~
1778 on~ wrong~ page~ (\@abspage@last\space not~
1779 \int_use:N\g_shipout_readonly_int).\MessageBreak
1780 Rerun~ to~ correct~ this}%
1781 }
1782 }
1783 {
1784 % \end{macrocode}
1785 % If the hook was not run, we need to add an extra page and place
1786 % it there.
1787 % However, making this extra page in case the hook is actually
1788 % empty would be forcing a rerun without any reason, so we check
1789 % that condition and also check if
1790 % \cs{@kernel@after@shipout@lastpage} contains any code. If both
1791 % are empty we omit the page generation.
1792 % \changes{v1.0p}{2025/04/27}{Increment page counter gh/1717}
1793 % \begin{macrocode}
1794 \bool_lazy_and:nnF
1795 { \hook_if_empty_p:n {shipout/lastpage} }
1796 { \tl_if_empty_p:N \@kernel@after@shipout@lastpage }
1797 {
1798 \global\advance\c@page\@ne
1799 \tex_shipout:D\vbox to\textheight
1800 {
1801 \hbox:n { \UseHook{shipout/lastpage}
1802 \@kernel@after@shipout@lastpage }
1803 % \end{macrocode}
1804 % This extra page could be totally empty except for the hook
1805 % content, but to help the user understanding why it is there we
1806 % put some text into it.
1807 % \begin{macrocode}
1808 \@@_excuse_extra_page:
1809 \null
1810 }
1811 % \end{macrocode}
1812 % At this point we also signal to \LaTeX{}'s endgame that a rerun is
1813 % necessary so that an appropriate message can be shown on the
1814 % terminal. We do this by simply defining a command used as a flag and
1815 % tested in \cs{enddocument}.
1816 % \begin{macrocode}
1817 \cs_gset_eq:NN \@extra@page@added \relax
1818 }
1819 }
1820 }
1821 }
1822 % \end{macrocode}
1823 % \end{macro}
1824 % \end{macro}
1825 %
1826 %
1827 %
1828 % \begin{macro}{\@@_excuse_extra_page:}
1829 % Say mea culpa \ldots
1830 % \begin{macrocode}
1831 \cs_new:Npn \@@_excuse_extra_page: {
1832 \vfil
1833 \begin{center}
1834 \bfseries Temporary~ page!
1835 \end{center}
1836 \LaTeX{}~ was~ unable~ to~ guess~ the~ total~ number~ of~ pages~
1837 correctly.~ ~ As~ there~ was~ some~ unprocessed~ data~ that~
1838 should~ have~ been~ added~ to~ the~ final~ page~ this~ extra~
1839 page~ has~ been~ added~ to~ receive~ it.
1840 \par
1841 If~ you~ rerun~ the~ document~ (without~ altering~ it)~ this~
1842 surplus~ page~ will~ go~ away,~ because~ \LaTeX{}~ now~ knows~
1843 how~ many~ pages~ to~ expect~ for~ this~ document.
1844 \vfil
1845 }
1846 % \end{macrocode}
1847 % \end{macro}
1848 %
1849 %
1850 % \begin{macro}{\PreviousTotalPages,\@kernel@before@begindocument}
1851 % In the preamble before the \texttt{aux} file was read
1852 % \cs{PreviousTotalPages} is always zero.
1853 % \begin{macrocode}
1854 \def\PreviousTotalPages{0}
1855 % \end{macrocode}
1856 % In the \texttt{aux} file there should be an update for
1857 % \cs{@abspage@last} recording the number of pages from the
1858 % previous run. If not that macro holds the value of
1859 % \cs{maxdimen}. So we test for it and update
1860 % \cs{PreviousTotalPages} if there was a real value. This should
1861 % happen just before the \hook{begindocument} hook is executed so
1862 % that the value can be used inside that hook.
1863 % \begin{macrocode}
1864 \g@addto@macro\@kernel@before@begindocument
1865 {\ifnum\@abspage@last<\maxdimen
1866 \xdef\PreviousTotalPages{\@abspage@last}\fi}
1867 % \end{macrocode}
1868 % \end{macro}
1869 %
1870 %
1871 % \section{Legacy \LaTeXe{} interfaces}
1872 %
1873 %
1874 %
1875 % \begin{macro}{\DiscardShipoutBox}
1876 % Request that the next shipout box is to be discarded.
1877 % \begin{macrocode}
1878 \cs_new_eq:NN \DiscardShipoutBox \shipout_discard:
1879 % \end{macrocode}
1880 % \end{macro}
1881 %
1882 %
1883 % \begin{macro}{\AtBeginDvi}
1884 % If we roll forward from an earlier kernel \cs{AtBeginDvi} is
1885 % defined so we better not use \cs{cs_new_protected:Npn} here.
1886 % \changes{v1.0d}{2020/11/24}{Support for roll forward (gh/434)}
1887 % \begin{macrocode}
1888 \cs_set_protected:Npn \AtBeginDvi
1889 {\@@_add_firstpage_material:Nn \AtBeginDvi}
1890 % \end{macrocode}
1891 % \end{macro}
1892 %
1893 %
1894 % \begin{macro}{\DebugShipoutsOn,\DebugShipoutsOff}
1895 %
1896 % \begin{macrocode}
1897 \cs_new_eq:NN \DebugShipoutsOn \shipout_debug_on:
1898 \cs_new_eq:NN \DebugShipoutsOff \shipout_debug_off:
1899 % \end{macrocode}
1900 % \end{macro}
1901 %
1902 %
1903 % \section{Internal commands needed elsewhere}
1904 %
1905 % These internal commands use double and triple \texttt{@} signs so
1906 % we need to stop getting them translated to the module name.
1907 % \begin{macrocode}
1908 %<@@=>
1909 % \end{macrocode}
1910 %
1911 % \begin{macro}{\@expl@@@shipout@add@firstpage@material@@Nn,
1912 % \@expl@@@shipout@add@background@box@@n,
1913 % \@expl@@@shipout@add@foreground@box@@n,
1914 % \@expl@@@shipout@add@background@picture@@n,
1915 % \@expl@@@shipout@add@foreground@picture@@n}
1916 % Some internals needed elsewhere.
1917 %
1918 % \InternalDetectionOff
1919 % \begin{macrocode}
1920 \cs_set_eq:NN \@expl@@@shipout@add@firstpage@material@@Nn
1921 \__shipout_add_firstpage_material:Nn
1922 % \end{macrocode}
1923 %
1924 % \begin{macrocode}
1925 \cs_set_eq:NN \@expl@@@shipout@add@background@box@@n
1926 \__shipout_add_background_box:n
1927 % \end{macrocode}
1928 %
1929 % \begin{macrocode}
1930 \cs_set_eq:NN \@expl@@@shipout@add@foreground@box@@n
1931 \__shipout_add_foreground_box:n
1932 % \end{macrocode}
1933 %
1934 % \begin{macrocode}
1935 \cs_set_eq:NN \@expl@@@shipout@add@background@picture@@n
1936 \__shipout_add_background_picture:n
1937 % \end{macrocode}
1938 %
1939 % \begin{macrocode}
1940 \cs_set_eq:NN \@expl@@@shipout@add@foreground@picture@@n
1941 \__shipout_add_foreground_picture:n
1942 % \end{macrocode}
1943 % \InternalDetectionOn
1944 % \end{macro}
1945 %
1946 % \begin{macrocode}
1947 \ExplSyntaxOff
1948 %</2ekernel|latexrelease>
1949 %<latexrelease>\EndIncludeInRelease
1950 % \end{macrocode}
1951 %
1952 %
1953 % Rolling back here doesn't undefine the interface commands as they
1954 % may be used in packages without rollback functionality. So we
1955 % just make them do nothing which may or may not work depending on
1956 % the code usage.
1957 %
1958 % \begin{macrocode}
1959 %<latexrelease>\IncludeInRelease{0000/00/00}%
1960 %<latexrelease> {\shipout}{Hook management (shipout)}%
1961 %<latexrelease>
1962 % \end{macrocode}
1963 % If we roll forward then \cs{tex\_shipout:D} may not be defined in
1964 % which case \cs{shipout} does have it original definition and so
1965 % we must not \cs{let} it to something else which is \cs{relax}!
1966 % \changes{v1.0d}{2020/11/24}{Support for roll forward (gh/434)}
1967 % \begin{macrocode}
1968 %<latexrelease>\ifcsname tex_shipout:D\endcsname
1969 %<latexrelease>\expandafter\let\expandafter\shipout
1970 %<latexrelease> \csname tex_shipout:D\endcsname
1971 %<latexrelease>\fi
1972 %<latexrelease>
1973 %<latexrelease>\let \RawShipout\@undefined
1974 %<latexrelease>\let \ShipoutBox\@undefined
1975 %<latexrelease>\let \ReadonlyShipoutCounter \@undefined
1976 %<latexrelease>\let \c@totalpages \@undefined
1977 %<latexrelease>\let \thetotalpages \@undefined
1978 %<latexrelease>
1979 %<latexrelease>\let \DiscardShipoutBox \@undefined
1980 %<latexrelease>\let \DebugShipoutsOn \@undefined
1981 %<latexrelease>\let \DebugShipoutsOff \@undefined
1982 %<latexrelease>
1983 %<latexrelease>\DeclareRobustCommand \AtBeginDvi [1]{%
1984 %<latexrelease> \global \setbox \@begindvibox
1985 %<latexrelease> \vbox{\unvbox \@begindvibox #1}%
1986 %<latexrelease>}
1987 %<latexrelease>
1988 %<latexrelease>\let \AtBeginShipout \@undefined
1989 %<latexrelease>\let \AtBeginShipoutNext \@undefined
1990 %<latexrelease>
1991 %<latexrelease>\let \AtBeginShipoutFirst \@undefined
1992 %<latexrelease>
1993 %<latexrelease>\let \ShipoutBoxHeight \@undefined
1994 %<latexrelease>\let \ShipoutBoxDepth \@undefined
1995 %<latexrelease>\let \ShipoutBoxWidth \@undefined
1996 %<latexrelease>
1997 % \end{macrocode}
1998 % We do not undo a substitution when rolling back. As the file
1999 % support gets undone the underlying data is no longer used (and
2000 % sufficiently obscure that it should not interfere with existing
2001 % commands) and properly removing it would mean we need to make the
2002 % \cs{undeclare@...} and its support macros available in all earlier
2003 % kernel releases which is pointless (and actually worse).
2004 %
2005 % \begin{macrocode}
2006 %<latexrelease>
2007 %<latexrelease>\let \AtEndDvi \@undefined
2008 % \end{macrocode}
2009 % We do not reenable a disabled package load when rolling back. As the file
2010 % support gets undone the underlying data is no longer checked (and
2011 % sufficiently obscure that it should not interfere with existing
2012 % commands) and properly removing it would mean we need to make the
2013 % \cs{reenable@package@load} command available in all earlier
2014 % kernel releases which is pointless (and actually worse).
2015 % \begin{macrocode}
2016 %\reenable@package@load{atenddvi}
2017 % \end{macrocode}
2018 %
2019 % \begin{macrocode}
2020 %<latexrelease>
2021 %<latexrelease>\EndIncludeInRelease
2022 %<*2ekernel>
2023 % \end{macrocode}
2024 %
2025 %
2026 %
2027 % \begin{macro}{\__shipout_force_immediate_writes:}
2028 % Change all writes to be immediate. To be used after the final
2029 % page has been shipped out.
2030 % \changes{v1.0o}{2025/03/05}{Macro added}
2031 % \changes{v1.0p}{2025/04/27}{Decrement page counter gh/1717}
2032 % \begin{macrocode}
2033 %</2ekernel>
2034 %<*2ekernel|latexrelease>
2035 %<latexrelease>\IncludeInRelease{2025/06/01}%
2036 %<latexrelease> {\__shipout_force_immediate_writes:}{Force immediate writes}%
2037 \ExplSyntaxOn
2038 \cs_new:Npn \__shipout_force_immediate_writes: {
2039 % \end{macrocode}
2040 %
2041 % Need to check if any variants are manually defined and if so
2042 % adjust them too --- not done!
2043 % \begin{macrocode}
2044 \cs_gset_eq:NN \iow_shipout:Nn \iow_now:Nn
2045 \cs_gset_eq:NN \lua_shipout:n \lua_now:n
2046 % \end{macrocode}
2047 %
2048 % \begin{macrocode}
2049 \cs_gset:Npn \write {\tex_immediate:D \tex_write:D}
2050 % \end{macrocode}
2051 % As the writes will happen without a page break, reset the
2052 % page number so they reference the last page.
2053 % \begin{macrocode}
2054 \global\advance\c@page\m@ne
2055 % \end{macrocode}
2056 %
2057 % \begin{macrocode}
2058 }
2059 \ExplSyntaxOff
2060 %</2ekernel|latexrelease>
2061 %<latexrelease>\EndIncludeInRelease
2062 %<latexrelease>\IncludeInRelease{0000/00/00}%
2063 %<latexrelease> {\__shipout_force_immediate_writes:}{Force immediate writes}%
2064 % \end{macrocode}
2065 % We want a definition for this even when doing rollback so that it
2066 % can stay in the kernel hook without forcing a rollback there as well.
2067 % \begin{macrocode}
2068 %<latexrelease>\ExplSyntaxOn
2069 %<latexrelease> \cs_new_eq:NN \__shipout_force_immediate_writes: \prg_do_nothing:
2070 %<latexrelease>\ExplSyntaxOff
2071 %<latexrelease>\EndIncludeInRelease
2072 %<*2ekernel>
2073 % \end{macrocode}
2074 % \end{macro}
2075 %
2076 %
2077 %
2078 %
2079 % \section{Package emulation for compatibility}
2080 %
2081 %
2082 % \subsection{Package \pkg{atenddvi} emulation}
2083 %
2084 %
2085 % \begin{macro}{\AtEndDvi}
2086 % This package has only one public command, so simulating it is easy
2087 % and actually sensible to provide as part of the kernel.
2088 % \changes{v1.0l}{2022/01/06}{Correctly simulate \cs{AtEndDvi} without
2089 % extending the syntax}
2090 % \begin{macrocode}
2091 %</2ekernel>
2092 %<*2ekernel|latexrelease>
2093 %<latexrelease>\IncludeInRelease{2020/10/01}%
2094 %<latexrelease> {\AtEndDvi}{atenddvi emulation}%
2095 \ExplSyntaxOn
2096 \cs_new_protected:Npn \AtEndDvi #1 {\AddToHook{shipout/lastpage}{#1}}
2097 \ExplSyntaxOff
2098 % \end{macrocode}
2099 % As the package is integrate we prevent loading (no need to roll that back):
2100 % \begin{macrocode}
2101 \disable@package@load{atenddvi}
2102 {\PackageWarning{atenddvi}
2103 {Functionality of this package is already\MessageBreak
2104 provided by LaTeX.\MessageBreak\MessageBreak
2105 It is there no longer necessary to load it\MessageBreak
2106 and you can safely remove it.\MessageBreak
2107 Found on}}
2108 % \end{macrocode}
2109 %
2110 % \begin{macrocode}
2111 %</2ekernel|latexrelease>
2112 % \end{macrocode}
2113 %
2114 % \begin{macrocode}
2115 %<latexrelease>\EndIncludeInRelease
2116 %<latexrelease>\IncludeInRelease{0000/00/00}%
2117 %<latexrelease> {\AtEndDvi}{atenddvi emulation}%
2118 %<latexrelease>\let \AtEndDvi \@undefined
2119 %<latexrelease>\EndIncludeInRelease
2120 %<*2ekernel>
2121 % \end{macrocode}
2122 % \end{macro}
2123 %
2124 %
2125 %
2126 % \begin{macrocode}
2127 %</2ekernel>
2128 % \end{macrocode}
2129 %
2130 %
2131 % \subsection{Package \pkg{atbegshi} emulation}
2132 %
2133 %
2134 %
2135 %
2136 %
2137 % \begin{macrocode}
2138 %<*atbegshi-ltx>
2139 \ProvidesPackage{atbegshi-ltx}
2140 [2021/01/10 v1.0c
2141 Emulation of the original atbegshi^^Jpackage with kernel methods]
2142 % \end{macrocode}
2143 %
2144 %
2145 % \begin{macro}{\AtBeginShipoutBox}
2146 % \begin{macrocode}
2147 \let \AtBeginShipoutBox \ShipoutBox
2148 % \end{macrocode}
2149 % \end{macro}
2150 %
2151 %
2152 %
2153 % \begin{macro}{\AtBeginShipoutInit}
2154 % Compatibility only, we aren't delaying \ldots
2155 % \begin{macrocode}
2156 \let \AtBeginShipoutInit \@empty
2157 % \end{macrocode}
2158 % \end{macro}
2159 %
2160 %
2161 % \begin{macro}{\AtBeginShipout,\AtBeginShipoutNext}
2162 % Filling hooks
2163 % \changes{v1.0l}{2022/01/06}{Correctly simulate \cs{AtBeginShipout}
2164 % and \cs{AtBeginShipoutNext} without extending the syntax}
2165 % \begin{macrocode}
2166 \protected\long\def\AtBeginShipout #1{\AddToHook{shipout/before}{#1}}
2167 \protected\long\def\AtBeginShipoutNext #1{\AddToHookNext{shipout/before}{#1}}
2168 % \end{macrocode}
2169 % \end{macro}
2170 %
2171 %
2172 % \begin{macro}{\AtBeginShipoutFirst}
2173 % Slightly more complex as we need to know the name of the command under which the
2174 % \hook{shipout/firstpage} hook is filled.
2175 % \begin{macrocode}
2176 \protected \def \AtBeginShipoutFirst
2177 {\@expl@@@shipout@add@firstpage@material@@Nn \AtBeginShipoutFirst}
2178 % \end{macrocode}
2179 % \end{macro}
2180 %
2181 %
2182 % \begin{macro}{\AtBeginShipoutDiscard}
2183 % Just a different name.
2184 % \begin{macrocode}
2185 \let \AtBeginShipoutDiscard \DiscardShipoutBox
2186 % \end{macrocode}
2187 % \end{macro}
2188 %
2189 %
2190 % \begin{macro}{\AtBeginShipoutAddToBox,\AtBeginShipoutAddToBoxForeground,
2191 % \AtBeginShipoutUpperLeft,\AtBeginShipoutUpperLeftForeground}
2192 % We don't expose them.
2193 % \begin{macrocode}
2194 \let \AtBeginShipoutAddToBox
2195 \@expl@@@shipout@add@background@box@@n
2196 \let \AtBeginShipoutAddToBoxForeground
2197 \@expl@@@shipout@add@foreground@box@@n
2198 % \end{macrocode}
2199 %
2200 % \begin{macrocode}
2201 \let \AtBeginShipoutUpperLeft
2202 \@expl@@@shipout@add@background@picture@@n
2203 \let \AtBeginShipoutUpperLeftForeground
2204 \@expl@@@shipout@add@foreground@picture@@n
2205 % \end{macrocode}
2206 % \end{macro}
2207 %
2208 % \begin{macro}{\AtBeginShipoutOriginalShipout}
2209 % This offers the raw \cs{shipout} primitive of the engine. A page
2210 % shipped out with this is not counted by
2211 % \cs{ReadonlyShipoutCounter} counter and thus the mechanism to
2212 % place \cs{special}s at the very end of the output might fail,
2213 % etc. It should therefore not be used in new applications but is
2214 % only provided to allow running legacy code. For new code use the
2215 % commands provided by the kernel instead.
2216 % \begin{macrocode}
2217 \ExplSyntaxOn
2218 \cs_new_eq:NN \AtBeginShipoutOriginalShipout \tex_shipout:D
2219 % \end{macrocode}
2220 % \end{macro}
2221 %
2222 %
2223 % \begin{macro}{\ShipoutBoxHeight,\ShipoutBoxWidth,\ShipoutBoxDepth}
2224 % This is somewhat different from the original in \pkg{atbegshi}
2225 % where \cs{ShipoutBoxHeight} etc.\ only holds the
2226 % \verb=\the\ht<box>= value. This may has some implications in some
2227 % use cases and if that is a problem then it might need changing.
2228 % \begin{macrocode}
2229 \cs_new:Npn \ShipoutBoxHeight { \dim_use:N \l_shipout_box_ht_dim }
2230 \cs_new:Npn \ShipoutBoxDepth { \dim_use:N \l_shipout_box_dp_dim }
2231 \cs_new:Npn \ShipoutBoxWidth { \dim_use:N \l_shipout_box_wd_dim }
2232 \ExplSyntaxOff
2233 % \end{macrocode}
2234 % \end{macro}
2235 %
2236 %
2237 % \begin{macrocode}
2238 %</atbegshi-ltx>
2239 % \end{macrocode}
2240 %
2241 % If the package is requested we substitute the one above:
2242 % \begin{macrocode}
2243 %<*2ekernel>
2244 \declare@file@substitution{atbegshi.sty}{atbegshi-ltx.sty}
2245 %</2ekernel>
2246 % \end{macrocode}
2247 %
2248 %
2249 %
2250 %
2251 %
2252 %
2253 % \subsection{Package \pkg{everyshi} emulation}
2254 %
2255 % This is now directly handled in that package so emulation is not
2256 % necessary any more.
2257 %
2258 %
2259 % Rather important :-)
2260 % \begin{macrocode}
2261 %<@@=>
2262 % \end{macrocode}
2263 %
2264 % \Finale
2265 %
2266 %
2267 %
2268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2269 \endinput
2270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%