Debian Codegraph @

texmf-dist/source/latex-dev/firstaid/latex2e-first-aid-for-external-files.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex2e-first-aid-for-external-files.dtx (C) Copyright 2020-2026
4 %%
5 %% The LaTeX Project and any individual authors listed elsewhere
6 %% in this file.
7 %
8 % It may be distributed and/or modified under the
9 % conditions of the LaTeX Project Public License, either version 1.3c
10 % of this license or (at your option) any later version.
11 % The latest version of this license is in
12 % https://www.latex-project.org/lppl.txt
13 % and version 1.3c or later is part of all distributions of LaTeX
14 % version 2008 or later.
15 %
16 % This file has the LPPL maintenance status "maintained".
17 %
18 %
19 %
20 %<*driver>
21 \documentclass{ltxdoc}
22 \EnableCrossrefs
23 \CodelineIndex
24 \begin{document}
25 \DocInput{latex2e-first-aid-for-external-files.dtx}
26 \end{document}
27 %</driver>
28 %
29 % \fi
30 %
31 %
32 % \providecommand\pkg[1]{\texttt{#1}}
33 %
34 % \title{First aid for external files and packages that need updating}
35 % \author{Frank Mittelbach, \LaTeX{} Project}
36 % \date{\LaTeXFirstAidDate}
37 %
38 % \maketitle
39 %
40 %
41 % \begin{abstract}
42 % This file contains some first aid for packages or classes that
43 % require updates because of internal changes to \LaTeX{} but that
44 % aren't yet reflected in the package/class code.
45 % \end{abstract}
46 %
47 % \tableofcontents
48 %
49 % \section{Introduction}
50 %
51 % Over the years package writers have hooked into
52 % various parts of internal \LaTeX{} commands (largely because
53 % proper interfaces were missing in important places) and if
54 % we are now gradually adding such interfaces these internal
55 % commands do change and as a result patching into them stops
56 % working.
57 %
58 % As part of making such internal changes the \LaTeX{} Project team
59 % attempts to check for such usage in packages, alert the package
60 % maintainers and ensures that the packages get updated alongside
61 % the core \LaTeX{} system. However it is not always possible to
62 % get packages that will fail with a new kernel updated in time and
63 % if that is the case we try to provide a temporary fix in this
64 % file for them.
65 % Once the package gets updated the fix will then be removed again.
66 %
67 % For that reason, it is put into a separate bundle so that we can
68 % update it easily without requiring the CTAN maintainers to
69 % install a new full \LaTeX{} system just because we take out (or add)
70 % a fix for a package here.
71 %
72 % In the best case scenario the file documented here should be
73 % empty. In practice it will probably always contain one or the
74 % other fix while we are waiting for the package to get updated.
75 %
76 % \begin{quote} \textbf{Important notice:} The fixes provided here
77 % are not meant to be a permanent solution, but are only provided
78 % to support the transition period. They are (usually) neither
79 % complete nor necessarily the best solution! Furthermore, as they
80 % are done from the ``outside'', they usually add some burden and
81 % slow down \LaTeX{} processing, even if the package/class is not
82 % used in the document.
83 %
84 % We will therefore remove such code as soon as possible
85 % again. In practice this means that if some package never gets
86 % updated/corrected, then it will eventually fail to work, because
87 % after one or at most two \LaTeX{} releases we will take out the
88 % transition code to ensure that this ``first aid patching''
89 % doesn't get out of bounds.
90 % \end{quote}
91 %
92 % \subsection{Minor kernel fixes}
93 %
94 % If we encounter issues with the kernel code that should get fixed
95 % before the next main release we normally generate a patch release
96 % for \LaTeX{}. However, depending on the complexity of the fix we
97 % might first add the fix here and generate a full patch release
98 % only when a number of such issues have accumulated. This way we
99 % lessen the impact on CTAN maintainers because for each tach
100 % release we have to make and distribute also a matching
101 % development release.
102 %
103 % \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
104 %
105 %
106 % \section{The Implementation}
107 %
108 % This file is meant to be loaded during format generation which is
109 % why we give it the extension \texttt{.ltx}.
110 % \begin{macrocode}
111 %<*kernel>
112 % \end{macrocode}
113 %
114 % \begin{macrocode}
115 \def\LaTeXFirstAidDate{2026-03-12}
116 \def\LaTeXFirstAidVersion{v1.1x}
117 % \end{macrocode}
118 %
119 % \begin{macrocode}
120 \ProvidesFile{latex2e-first-aid-for-external-files.ltx}
121 [\LaTeXFirstAidDate\space \LaTeXFirstAidVersion\space
122 LaTeX kernel fixes to external files and packages]
123 % \end{macrocode}
124 %
125 % \changes{v1.1x}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
126 % expansion in expl3 code}
127 %
128 % \begin{macro}{\FirstAidNeededT}
129 % This is a very simple help to ensure that we only apply first aid
130 % to an unmodified package or class. It only works in the case the
131 % file has already been loaded and the csname \cs{ver@\#1.\#2} got
132 % defined (holding the current date, version, and short description
133 % info). We then compare its content to a frozen string and make
134 % the modification \verb=#3= only if both agree. If they differ we
135 % assume that the package/class in question got updated by its
136 % maintainer.
137 % \begin{macrocode}
138 \ExplSyntaxOn
139 \cs_new:Npn\FirstAidNeededT#1#2#3{
140 \exp_args:Nce\str_if_eq:onTF{ver@#1.#2}{#3}
141 { \typeout{==>~ First~ Aid~ for~ #1.#2~ applied! } }
142 { \typeout{==>~ First~ Aid~ for~ #1.#2~ no~ longer~ applied!^^J
143 \@spaces Expected:^^J
144 \@spaces\@spaces #3^^J
145 \@spaces but~ found:^^J
146 \@spaces\@spaces \use:c{ver@#1.#2}^^J
147 \@spaces so~ I'm~ assuming~ it~ got~ fixed.
148 } }
149 \exp_args:Nce\str_if_eq:onT{ver@#1.#2}{#3}
150 }
151 \ExplSyntaxOff
152 % \end{macrocode}
153 % \end{macro}
154 %
155 % \begin{macrocode}
156 %</kernel>
157 % \end{macrocode}
158 %
159 %
160 % \subsection{The \pkg{filehook} package first aid}
161 %
162 % The \pkg{filehook} package implements hooks into file loading
163 % commands. These days this is already provided by the kernel
164 % albeit not with the same user interface. Until that package gets
165 % updated (to use the kernel interfaces) we provide a
166 % substitution. This does not offer all hooks from \pkg{filehook}
167 % but all that have been used in packages available in \TeX{} Live.
168 %
169 % Note that this doesn't fix \pkg{currfile} because that package
170 % uses \pkg{filehook} but relies on the internals of the old
171 % implementation.
172 %
173 % The package has now got an update so we aren't activating the
174 % first aid. However, at the moment it basically bypasses the new
175 % hook mechanism and puts the old hooks in thereby disabling, for
176 % example, the possibility to re-order code through rules.
177 %
178 % We therefore keep \texttt{filehook-ltx.sty} around as a guideline
179 % for further updates.
180 %
181 % Replacing \pkg{filehook} with a leaner version would then work
182 % like this:
183 % \begin{macrocode}
184 %<*kernel>
185 %\declare@file@substitution{filehook.sty}{filehook-ltx.sty}
186 %</kernel>
187 % \end{macrocode}
188 %
189 % What follows is a simplified (partial) implementation of the \pkg{filehook}
190 % interfaces.
191 % Not implemented are:
192 %\begin{verbatim}
193 % \AtBeginOfFiles \AtEndOfFiles
194 % \AtBeginOfInputs \AtEndOfInputs
195 % \AtBeginOfInputFile \AtEndOfInputFile
196 %\end{verbatim}
197 %
198 % \begin{macrocode}
199 %<*filehook-ltx>
200 % \end{macrocode}
201 %
202 % \begin{macrocode}
203 \newcommand\AtBeginOfEveryFile [1]
204 {\AddToHook{file/before}{#1}}
205 \newcommand\AtEndOfEveryFile [1]
206 {\AddToHook{file/after}{#1}}
207 % \end{macrocode}
208 %
209 % \begin{macrocode}
210 \newcommand\AtBeginOfIncludes [1]
211 {\AddToHook{include/before}{#1}}
212 \newcommand\AtEndOfIncludes [1]
213 {\AddToHook{include/end}{#1}}
214 \newcommand\AfterIncludes [1]
215 {\AddToHook{include/after}{#1}}
216 % \end{macrocode}
217 %
218 % \begin{macrocode}
219 \newcommand\AtBeginOfPackages [1]
220 {\AddToHook{package/before}{#1}}
221 \newcommand\AtEndOfPackages [1]
222 {\AddToHook{package/after}{#1}}
223 % \end{macrocode}
224 %
225 % \begin{macrocode}
226 \newcommand\AtBeginOfClasses [1]
227 {\AddToHook{class/before}{#1}}
228 \newcommand\AtEndOfClasses [1]
229 {\AddToHook{class/after}{#1}}
230 % \end{macrocode}
231 %
232 % \begin{macrocode}
233 \newcommand\AtBeginOfFile [2]
234 {\AddToHook{file/#1/before}{#2}}
235 \newcommand\AtEndOfFile [2]
236 {\AddToHook{file/#1/after}{#2}}
237 % \end{macrocode}
238 %
239 % Some commands offered a starred form
240 % \begin{macrocode}
241 \DeclareDocumentCommand \AtBeginOfPackageFile {smm}
242 {\IfBooleanTF{#1}%
243 {\@ifpackageloaded{#2}%
244 {#3}%
245 {\AddToHook{package/#2/before}{#3}}}%
246 {\AddToHook{package/#2/before}{#3}}%
247 }
248 % \end{macrocode}
249 %
250 % \begin{macrocode}
251 \DeclareDocumentCommand \AtEndOfPackageFile {smm}
252 {\IfBooleanTF{#1}%
253 {\@ifpackageloaded{#2}%
254 {#3}%
255 {\AddToHook{package/#2/after}{#3}}}%
256 {\AddToHook{package/#2/after}{#3}}%
257 }
258 % \end{macrocode}
259 %
260 % Are the * forms here of any use? I know they are use 3--4 times
261 % on CTAN but I wonder if those are real or mistaken usages.
262 % \begin{macrocode}
263 \DeclareDocumentCommand \AtBeginOfClassFile {smm}
264 {\IfBooleanTF{#1}%
265 {\@ifclassloaded{#2}%
266 {#3}%
267 {\AddToHook{class/#2/before}{#3}}}%
268 {\AddToHook{class/#2/before}{#3}}%
269 }
270 \DeclareDocumentCommand \AtEndOfClassFile {smm}
271 {\IfBooleanTF{#1}%
272 {\@ifclassloaded{#2}%
273 {#3}%
274 {\AddToHook{class/#2/after}{#3}}}%
275 {\AddToHook{class/#2/after}{#3}}%
276 }
277 % \end{macrocode}
278 %
279 % \begin{macrocode}
280 \newcommand\AtBeginOfIncludeFile [2]
281 {\AddToHook{include/#1/before}{#2}}
282 \newcommand\AtEndOfIncludeFile [2]
283 {\AddToHook{include/#1/end}{#2}}
284 \newcommand\AfterIncludeFile [2]
285 {\AddToHook{include/#1/after}{#2}}
286 % \end{macrocode}
287 %
288 % \begin{macrocode}
289 %</filehook-ltx>
290 % \end{macrocode}
291 %
292 % \begin{macrocode}
293 %<*kernel>
294 % \end{macrocode}
295 %
296 % \subsection{The \pkg{dinbrief} class first aid}
297 %
298 % Again a case of a no longer correct \cs{endgroup} in document.
299 % Here the fix is simply though.
300 % \begin{macrocode}
301 \AddToHook{file/dinbrief.cls/after}[firstaid]{%
302 \FirstAidNeededT{dinbrief}{cls}{2000/03/02 LaTeX2e class}%
303 {\AddToHook{env/document/begin}{\begingroup}}%
304 }
305 % \end{macrocode}
306 %
307 %
308 % \subsection{The \pkg{unicode-math} package first aid}
309 %
310 % If \pkg{unicode-math} is used together with \pkg{doc} there is a
311 % problem because it changes the mathcodes without adjusting the
312 % use in \pkg{doc} that assume standard settings. Could be fixed on
313 % either side, but as \pkg{unicode-math} is derivating from the
314 % standard, the right place is probably a fix in this package. For
315 % now we do it here. See github/820.
316 % \begin{macrocode}
317 \AddToHook{package/unicode-math/after}{%
318 \AddToHook{cmd/mod@math@codes/after}{\mathcode`\|=28796 }}
319 % \end{macrocode}
320 %
321 %
322 %
323 %
324 % \subsection{The \pkg{pgfpages} and \pkg{pgfmorepages} first aid}
325 %
326 % \pkg{pgfpages} alters the \cs{shipout} primitive to support
327 % multiple page up scenarios. If used together with \pkg{atbegshi}
328 % that worked because the alterations done by \pkg{atbegshi} came
329 % later and so used the new definition provide by
330 % \pkg{pgfpages}. Now that the code from \pkg{atbegshi} is already
331 % in the kernel this further redefinition doesn't happen with the
332 % result that the change to \cs{shipout} comes to late and
333 % breaks the kernel processes.
334 %
335 % \begin{macrocode}
336 \ExplSyntaxOn
337 \AddToHook{file/pgfpages.sty/after}[firstaid]{
338 % \end{macrocode}
339 % Undo overwriting \cs{shipout}:
340 % \begin{macrocode}
341 \cs_gset_eq:NN \shipout \pgfpages@originalshipout
342 % \end{macrocode}
343 % Instead overwrite the L3 programming layer name of the
344 % primitive. This is really an absolute no-go, but for now the
345 % simplest solution to keep the original code running.
346 %
347 % It will be replaced when the ``configuration points'' interface
348 % for \LaTeX{} becomes available. At that point the package will be
349 % able to set up a different strategy for doing shipouts and
350 % without the need to overwrite a primitive (which it did in the
351 % past and which we do below) and then this code here can be taken
352 % out again.
353 % \begin{macrocode}
354 \cs_set_eq:NN \pgfpages@originalshipout \tex_shipout:D
355 \cs_set_eq:NN \tex_shipout:D \pgfpages@interceptshipout
356 }
357 \ExplSyntaxOff
358 % \end{macrocode}
359 %
360 % Same issue with \pkg{pgfmorepages} but slightly different implementation (sigh).
361 %
362 % \begin{macrocode}
363 \ExplSyntaxOn
364 \AddToHook{file/pgfmorepages.sty/after}[firstaid]{
365 \cs_set_nopar:Npn \pgfhookintoshipout {
366 \cs_set_eq:NN \pgfpages@originalshipout \tex_shipout:D
367 \cs_set_eq:NN \tex_shipout:D \pgfpages@interceptshipout
368 }
369 }
370 \ExplSyntaxOff
371 % \end{macrocode}
372 %
373 % \subsection{The \pkg{babel} package}
374 %
375 % Turn off the \pkg{babel} hack.
376 % \begin{macrocode}
377 \AddToHook{file/babel.sty/before}[firstaid]{\def\BabelCaseHack{}}
378 % \end{macrocode}
379 %
380 %
381 % \subsection{The \pkg{songs} package first aid}
382 %
383 % The songs package uses \cs{obeylines} and redefines \cs{par} for special effect.
384 % this no longer works in \LaTeX{} 2022-06-01 (gh issue 367). The following
385 % fixes at least one failure.
386 % \begin{macrocode}
387 \AddToHook{file/songs.sty/after}[firstaid]{%
388 \FirstAidNeededT{songs}{sty}{2018/09/12 v3.1 Songs package}%
389 {%
390 \renewcommand\SB@obeylines{%
391 \let\obeyedline\SB@par%
392 \obeylines%
393 \let\@par\SB@@par%
394 }}%
395 }
396 % \end{macrocode}
397 %
398 % \subsection{The \pkg{crop} package first aid}
399 %
400 % The crop packages fails currently due to two \LaTeX{} changes:
401 % It doesn't know that \cs{stockheight} and \cs{stockwidth}
402 % are now defined, and doesn't take into account that \cs{rlap}
403 % is robust (https://github.com/rrthomas/crop/issues/2).
404 % The first is addressed by setting the dimension if they are zero or
405 % negative. For the second we locally change the meaning of \cs{protect}
406 %
407 % \begin{macrocode}
408 \AddToHook{file/crop.sty/after}[firstaid]{%
409 \FirstAidNeededT{crop}{sty}{2017/11/19 1.10 crop marks (mf)}%
410 {%
411 \ifdim\stockwidth > 0pt \else \stockwidth\paperwidth \fi
412 \ifdim\stockheight > 0pt \else \stockheight\paperheight \fi
413 \renewcommand*\CROP@genreflect[1]{%
414 \leavevmode
415 \dimen0\CROP@horigin
416 \kern2\dimen0
417 \begingroup
418 \set@typeset@protect %change protect
419 \reflectbox{%
420 \hb@xt@\paperwidth{%
421 \vbox to\paperheight{%
422 #1%
423 \vss
424 }%
425 \hss
426 }%
427 }%
428 \endgroup
429 }
430 }%
431 }
432 % \end{macrocode}
433 %
434 %
435 %
436 % \subsection{The \pkg{bigfoot} first aid}
437 %
438 % The \pkg{bigfoot} packages makes the assumption that two
439 % \cs{newinsert} allocations have a recognisable order in their
440 % numbers, the second one has a lower number. This was correct in
441 % the classic \TeX{} implementation but with the extended
442 % allocation possibilities of all modern engines is no longer the
443 % case and there is a point where the allocations take a ``jump''
444 % breaking the ordering assumption. These days we are fairly close
445 % to that point and depending on how many packages are loaded
446 % before \pkg{bigfoot} the package breaks.
447 %
448 % This firstaid therefore jumps over the problematical point by
449 % pushing the count allocation to a safe value if necessary.
450 % \begin{macrocode}
451 \AddToHook{file/bigfoot.sty/after}{%
452 \ifnum\count10<\insc@unt
453 \global\count10=\insc@unt
454 \fi
455 % \end{macrocode}
456 % We also correct a bug that \pkg{bigfoot} tries to shift mark
457 % registers, but in \LaTeX{} (at least since 2015) the allocation
458 % number is not 266, so it does that to a random number of mark
459 % registers (which sometimes blows up depending on the value in 266).
460 % \begin{macrocode}
461 \def\FN@allmarks#1{\@elt{#1}%
462 \ifnum#1<\count256 %<--- problem: 266 isn't the counter for marks
463 \expandafter\FN@allmarks\expandafter{\number\numexpr#1+\@ne}%
464 \fi}%
465 }
466 % \end{macrocode}
467 %
468 %
469 %
470 %
471 % \subsection{\pkg{marginfix} first aid}
472 %
473 % The \pkg{marginfix} package tries to patch \cs{@combinefloats} but
474 % with 2025-06 the kernel doesn't use this any longer but uses
475 % \cs{@outputbox@attachfloats} instead.
476 % \begin{macrocode}
477 \AddToHook{file/marginfix.sty/after}[firstaid]{%
478 \FirstAidNeededT{marginfix}{sty}%
479 {2020/05/06 v1.2 Fix Margin Paragraphs}%
480 {\let \@outputbox@attachfloats \@combinefloats}%
481 }
482 % \end{macrocode}
483 %
484 %
485 %
486 %
487 % \subsection{\pkg{ulem} first aid}
488 %
489 % In 2020 we fixed various kernel commands to accept \pkg{calc}
490 % syntax. The \pkg{ulem} package redefines some internals and that
491 % now conflicts with the new definitions as they involve an extra
492 % group. So we alter the definition of \cs{@hspace} if \pkg{ulem}
493 % was loaded. This is not perfect, obviously, so it will go out the
494 % moment \pkg{ulem} gets adjusted.
495 %
496 % \begin{macrocode}
497 \AddToHook{file/ulem.sty/after}[firstaid]{%
498 \def\@hspace#1{\begingroup\setlength\skip@{#1}%
499 \edef\x{\endgroup\hskip\the\skip@\relax}\x}%
500 }
501 % \end{macrocode}
502 %
503 %
504 % \subsection{\pkg{varwidth} first aid}
505 %
506 % The \pkg{varwidth} package does a lot of low-level paragraph
507 % manipulation assuming traditional \TeX{} paragraphs. However, with
508 % the paragraph hooks we end up with one extra glue 0pt item on the
509 % vertical list and if that isn't removed then the package doesn't
510 % find its penalties.
511 %
512 % So this needs to be removed as well by adding an additional
513 % \cs{unskip}.
514 %
515 % \begin{macrocode}
516 \AddToHook{file/varwidth.sty/after}[firstaid]{%
517 \FirstAidNeededT{varwidth}{sty}%
518 {2009/03/30 ver 0.92; \space Variable-width minipages}%
519 {%
520 \def\@vwid@sift{%
521 \skip@\lastskip\unskip
522 \ifdim\lastskip=\z@\unskip\fi % <---- the first aid here (not just unskip)
523 \dimen@\lastkern\unkern
524 \count@\lastpenalty\unpenalty
525 \setbox\z@\lastbox
526 \ifvoid\z@ \advance\sift@deathcycles\@ne \else \sift@deathcycles\z@ \fi
527 \ifnum\sift@deathcycles>33
528 \let\@vwid@sift\relax
529 \PackageWarning{varwidth}{Failed to reprocess entire contents}%
530 \fi
531 \ifnum\count@=\@vwid@preeqp \@vwid@eqmodefalse\fi
532 \ifnum\count@=\@vwid@posteqp \@vwid@eqmodetrue\fi
533 \ifnum\count@=\@vwid@toppen % finished
534 \let\@vwid@sift\relax
535 \else\ifnum\count@=\@vwid@offsets
536 \@vwid@setoffsets
537 \else
538 \ifnum\count@=\@vwid@postw
539 \else
540 \@vwid@resetb % reset box \z@ or measure it
541 \fi
542 \@vwid@append
543 \fi\fi
544 \@vwid@sift}%
545 }%
546 }
547 % \end{macrocode}
548 %
549 % \subsection{The \pkg{german} class first aid}
550 %
551 % Handling of \cs{protected} UTF-8
552 % \begin{macrocode}
553 \AddToHook{file/german.sty/after}[firstaid]{%
554 \FirstAidNeededT{german}{sty}{1998/07/08 v2.5e Support for writing german texts (br)}
555 {%
556 \let\grmn@active@dq@\@active@dq
557 \def\@active@dq{\protect\grmn@active@dq@}%
558 \germanTeX
559 }%
560 }
561 % \end{macrocode}
562 %
563 % \begin{macrocode}
564 \AddToHook{file/ngerman.sty/after}[firstaid]{%
565 \FirstAidNeededT{ngerman}{sty}{1998/07/08 v2.5e Support for writing german texts (br)}
566 {%
567 \let\grmn@active@dq@\@active@dq
568 \def\@active@dq{\protect\grmn@active@dq@}%
569 \ngermanTeX
570 }%
571 }
572 % \end{macrocode}
573 %
574 % \begin{macrocode}
575 %</kernel>
576 % \end{macrocode}
577 % \subsection{The \pkg{underscore} first aid}
578 %
579 % The \pkg{underscore} package makes the underscore active. This means that the
580 % underscore can not be used in label and references unless the package option \texttt{strings} is used
581 % (which patches a selection of problematic commands like \cs{label} and \cs{ref})
582 % or \pkg{babel} is used which redefines\footnote{unless the recommended option \texttt{safe=none} is used}
583 % a selection of problematic commands like \cs{@testdef} or \cs{@newl@bel}.
584 %
585 % With the new property commands the work-around do not work. We therefore make the
586 % underscore protected and use \cs{ifincsname} to allow its use in csnames.
587 %
588 % \begin{macrocode}
589 %<*underscore-ltx>
590 \ProvidesPackage{underscore-ltx}[2023/09/20 LaTeX firstaid to make underscore protected ]
591 \begingroup
592 \catcode`\_=\active
593 \protected\gdef _{%
594 \ifincsname %
595 \string_%
596 \else
597 \ifx\protect\@typeset@protect
598 \ifmmode \sb \else \BreakableUnderscore \fi
599 \else
600 \ifx\protect\@unexpandable@protect \noexpand_%
601 \else \protect_%
602 \fi\fi
603 \fi
604 }
605 \global\let\ActiveUnderscore=_
606 \endgroup
607 %</underscore-ltx>
608 % \end{macrocode}
609 % \begin{macrocode}
610 %<*kernel>
611 \AddToHook{file/underscore.sty/after}[firstaid]{%
612 \FirstAidNeededT{underscore}{sty}{2006/09/13}{\RequirePackage{underscore-ltx}}}
613 %</kernel>
614 % \end{macrocode}
615 %
616 % \begin{macrocode}
617 %<*kernel>
618 % \end{macrocode}
619 %
620 % \subsection{The \pkg{acro} package first aid}
621 %
622 % The package does not declare a \texttt{prop}, which causes an issue with newer
623 % routines in \pkg{expl3}.
624 % \begin{macrocode}
625 \AddToHook{package/acro/after}[firstaid]{%
626 \FirstAidNeededT{acro}{sty}{2022/04/01 v3.8 typeset acronyms
627 and other abbreviations (CN)}
628 {\UseName{prop_new:c}{l__acro_tmpa_prop}%
629 % \end{macrocode}
630 % With the 2024 June release of \LaTeX{} it will also fail to patch
631 % \cs{endlongtable} and therefore errors when loading. However, the
632 % patch it tries never worked (because it was setting a local
633 % boolean at a point where it was more or less immediately
634 % reset). Thus, rather than fixing the patch approach (which
635 % requires to surround the patch with \cs{ExplSyntaxOn}
636 % \verb*/\catcode`\ =10 / and \cs{ExplSyntaxOff}) we simply disable
637 % the patch for now.
638 % \begin{macrocode}
639 \acsetup{patch/longtable=false}%
640 }%
641 }
642 % \end{macrocode}
643 %
644 % \subsection{The \pkg{chemformula} package first aid}
645 %
646 % Package \pkg{chemformula} uses \pkg{l3keys2e} for option processing.
647 % This used to be made available as \pkg{chemformula} also loads
648 % \pkg{xfrac}, which loaded \pkg{l3keys2e}. However, \pkg{xfrac} has now
649 % been updated to use the newer kernel method if available, so loading
650 % \pkg{chemformula} fails.
651 % \begin{macrocode}
652 \AddToHook{package/chemformula/before}[firstaid]{%
653 \RequirePackage{l3keys2e}%
654 }
655 % \end{macrocode}
656 %
657 % \subsection{The \pkg{chemnum} package first aid}
658 %
659 % The package does not declare a \texttt{prop}, which causes an issue with newer
660 % routines in \pkg{expl3}.
661 % \begin{macrocode}
662 \AddToHook{package/chemnum/after}[firstaid]{%
663 \FirstAidNeededT{chemnum}{sty}{2021/01/21 v1.3a a comprehensive
664 approach for the numbering of chemical compounds (CN)}
665 {\UseName{prop_new:c}{l__chemnum_tmpa_prop}}%
666 }
667 % \end{macrocode}
668 %
669 % \subsection{The \pkg{cleveref} package first aid}
670 %
671 % The \pkg{cleveref} package expects only two data containers
672 % for its internal \cs{newlabel} command. This fails if
673 % \pkg{xr-hyper} is used which expands every \cs{newlabel} to
674 % five data container and puts the file name into the last one.
675 % \begin{macrocode}
676 \AddToHook{package/cleveref/after}[firstaid]{%
677 \FirstAidNeededT{cleveref}{sty}{2018/03/27 v0.21.4 Intelligent cross-referencing}
678 {%
679 % \end{macrocode}
680 % This are the two commands which retrieve the data from the label info.
681 % We change them to expect five arguments.
682 % \begin{macrocode}
683 \def\cref@getref#1#2{%
684 \expandafter\let\expandafter#2\csname r@#1@cref\endcsname%
685 \expandafter\expandafter\expandafter\def%
686 \expandafter\expandafter\expandafter#2%
687 \expandafter\expandafter\expandafter{%
688 \expandafter\@firstoffive#2}}% <-------- five
689 \def\cpageref@getref#1#2{%
690 \expandafter\let\expandafter#2\csname r@#1@cref\endcsname%
691 \expandafter\expandafter\expandafter\def%
692 \expandafter\expandafter\expandafter#2%
693 \expandafter\expandafter\expandafter{%
694 \expandafter\@secondoffive#2}}% <----------- five
695 % \end{macrocode}
696 % We also need to redefine the internal label commands of cleveref.
697 % This must be done after \pkg{cleveref} has made its changes
698 % in \texttt{begindocument} so we add it to the same hook using
699 % the hook label used by \pkg{cleveref}.
700 % This way it is guaranteed to overwrite the definitions.
701 % \begin{macrocode}
702 \AddToHook{begindocument}[cleveref]{%
703 \def\label@noarg#1{%
704 \cref@old@label{#1}%
705 \@bsphack%
706 \protected@edef\@tempa{{page}{\the\c@page}}% <--- should be protected
707 \setcounter{page}{1}%
708 \protected@edef\@tempb{\thepage}% <--- should be protected
709 \expandafter\setcounter\@tempa%
710 \cref@constructprefix{page}{\cref@result}%
711 \protected@write\@auxout{}%
712 {\string\newlabel{#1@cref}{{\cref@currentlabel}%
713 {[\@tempb][\arabic{page}][\cref@result]\thepage}{}{}{}}}% <----- five
714 \@esphack}%
715 \def\label@optarg[#1]#2{%
716 \cref@old@label{#2}%
717 \@bsphack%
718 \protected@edef\@tempa{{page}{\the\c@page}}% <--- should be protected
719 \setcounter{page}{1}%
720 \protected@edef\@tempb{\thepage}% <--- should be protected
721 \expandafter\setcounter\@tempa%
722 \cref@constructprefix{page}{\cref@result}%
723 \protected@edef\cref@currentlabel{%
724 \expandafter\cref@override@label@type%
725 \cref@currentlabel\@nil{#1}}%
726 \protected@write\@auxout{}%
727 {\string\newlabel{#2@cref}{{\cref@currentlabel}%
728 {[\@tempb][\arabic{page}][\cref@result]\thepage}{}{}{}}}% <------- five
729 \@esphack}%
730 }
731 % \end{macrocode}
732 % \changes{v1.1g}{2024/10/16}{update cleveref data in label hook}
733 % \changes{v1.1i}{2024/11/08}{add expandafter, issue \#1544}
734 % cleveref patches and redefines \cs{refstepcounter} so that a call updates its data.
735 % This fails if like e.g. in \pkg{longtable} the counter is stepped with
736 % \cs{@kernel@refstepcounter}. We therefore move the data setup into the label hook.
737 % As the hook is in a group we have to smuggle the data out of it.
738 % \url{https://tex.stackexchange.com/a/722909/2388} and issue \#1393
739 % \begin{macrocode}
740 \newcommand\firstaid@cref@smugglelabel{\let\cref@currentlabel\cref@gcurrentlabel@temp}
741 \newcommand\firstaid@cref@updatelabeldata[1]{%
742 \cref@constructprefix{#1}{\cref@result}%
743 \@ifundefined{cref@#1@alias}%
744 {\def\@tempa{#1}}%
745 {\def\@tempa{\csname cref@#1@alias\endcsname}}%
746 \protected@xdef\cref@gcurrentlabel@temp{%
747 [\@tempa][\arabic{#1}][\cref@result]%
748 \csname p@#1\expandafter\endcsname\csname the#1\endcsname}%
749 \aftergroup\firstaid@cref@smugglelabel
750 }
751 % \end{macrocode}
752 % we test if \cs{@currentcounter} is empty for unnumbered sections
753 % \changes{v1.1h}{2024/11/01}{fix spurious space}
754 % \changes{v1.1j}{2024/11/24}{add \cs{iftag@} test, issue \#1560}
755 % \begin{macrocode}
756 \newif\iftag@
757 \AddToHook{label}[firstaid/cleveref]
758 {\ifx
759 \@currentcounter\@empty
760 \else
761 \iftag@\else
762 \firstaid@cref@updatelabeldata{\@currentcounter}%
763 \fi
764 \fi}
765 % \end{macrocode}
766 % Due to a bug correction in amsmath a patch in cleveref fails in LaTeX 2025-06-01,
767 % see gh 1684
768 % \changes{v1.1p}{2025/06/05}{adapt to new amsmath \#1560}
769 % \begin{macrocode}
770 \AddToHook{begindocument}[cleveref]
771 {
772 \IfPackageAtLeastT{amsmath}{2025-05-18}
773 {
774 \def\ltx@label#1{\cref@label{#1}}
775 \def\label@in@display@noarg#1{\cref@old@label@in@display{#1}}
776 \def\label@in@mmeasure@noarg#1{%
777 \begingroup
778 \measuring@false%
779 \cref@old@label@in@display{#1}%
780 \endgroup}%
781 }
782 }
783 }%
784 }
785 % \end{macrocode}
786 %
787 % \subsection{The \pkg{arydshln} package first aid}
788 %
789 % Making two internal commands robust to avoid expansion while constructing the array preamble.
790 % \begin{macrocode}
791 \AddToHook{package/arydshln/after}[firstaid]{%
792 \FirstAidNeededT{arydshln}{sty}{2019/02/21 v1.76 }
793 {%
794 % add \protected
795 \protected\def\adl@@vlineL#1#2#3#4{\adl@ivline#4\@nil{#1}{#2}%
796 \xdef\adl@colsL{\adl@colsL
797 \@elt{#3}{\number\@tempcnta}{\number\@tempcntb}%
798 {\adl@dashcolor}{\adl@gapcolor}}}%
799 \protected\def\adl@@vlineR#1#2#3#4{\adl@ivline#4\@nil{#1}{#2}%
800 \xdef\adl@colsR{%
801 \@elt{#3}{\number\@tempcnta}{\number\@tempcntb}%
802 {\adl@dashcolor}{\adl@gapcolor}%
803 \adl@colsR}}%
804 \let\adl@act@@vlineL\adl@@vlineL
805 \let\adl@act@@vlineR\adl@@vlineR
806 \def\endtabular{\endarray \egroup}
807 }%
808 }
809 % \end{macrocode}
810 %
811 %
812 % \subsection{The \pkg{calc} package first aid}
813 %
814 % The \pkg{calc} is not an external package, but for now we are
815 % removing a newly introduced incompatibility with \pkg{zref-perpage}
816 % here. At some point \pkg{amstext}, \pkg{zref-perpage}, \pkg{calc},
817 % \ldots{} all need to be synced so that they do not overwrite the
818 % \cs{stepcounter} definition but safely hook into the kernel
819 % code. But that requires suitable hooks there, so has to wait.
820 %
821 % \pkg{zref-perpage} already handles \pkg{amstext}'s
822 % \cs{iffirstchoice@} in \cs{stepcounter} so the fix in \pkg{calc}
823 % should not be applied at \texttt{begindocument}. We therefore
824 % void it.
825 % \changes{v1.1u}{2026/01/16}{improve zref-perpage firstaid}
826 % \begin{macrocode}
827 \AddToHook{begindocument}[calc-noop]{}
828 \AddToHook{package/zref-perpage/after}{%
829 \DeclareHookRule{begindocument}{calc-noop}{voids}{calc}}
830 % \end{macrocode}
831 %
832 %
833 %
834 % \subsection{First aid after retiring the legacy mark mechanism}
835 %
836 % \changes{v1.1o}{2025/05/14}{First aid to help with retiring legacy
837 % mark mechanism (gh/1724)}
838 %
839 % With the June 2025 release of \LaTeX{} the legacy mark mechanism
840 % using \cs{@themark} with exactly two mark components has been removed
841 % and replaced by a more general mechanism. This affects a few
842 % packages and classes that had patched into the legacy code to extend
843 % it or to completely replace it. For now we do our best to keep that
844 % working, but mid-term all packages have to update to the new
845 % mechanism (and avoid low-level patching) in order to remain
846 % compatible with future adjustments.
847 %
848 % For an analysis of the current situation see the github issues 1724
849 % at \url{https://github.com/latex3/latex2e/issues/1724}.
850 %
851 % We provide a dummy definition for \cs{@themark} because some classes
852 % expect it to be around. This prevents low-level errors but only
853 % helps if the class in question otherwise fully implement its own
854 % mark interface (as for example the AMS classes do).
855 % \begin{macrocode}
856 \def\@themark{{}{}}
857 % \end{macrocode}
858 %
859 % \begin{macrocode}
860 \AddToHook{class/smfart/after}[firstaid]{%
861 \FirstAidNeededT{smfart}{cls}{2021/03/28 v1.6
862 Classe LaTeX pour les articles publies par la SMF}
863 {%
864 \def\leftmark{\expandafter\@leftmark\botmark\@empty\@empty}%
865 \def\rightmark{\expandafter\@rightmark\firstmark\@empty\@empty}%
866 }}
867 % \end{macrocode}
868 %
869 % \begin{macrocode}
870 \AddToHook{class/smfbook/after}[firstaid]{%
871 \FirstAidNeededT{smfbook}{cls}{2021/03/28 v1.6
872 Classe LaTeX pour les monographies editees par la SMF}
873 {%
874 \def\leftmark{\expandafter\@leftmark\botmark\@empty\@empty}%
875 \def\rightmark{\expandafter\@rightmark\firstmark\@empty\@empty}%
876 }}
877 % \end{macrocode}
878 %
879 %
880 %
881 % \subsection{First aid for \pkg{morewrites}}
882 %
883 % \changes{v1.1o}{2025/05/14}{First aid to avoid breaking the
884 % morewrites package}
885 %
886 % A recent change in \LaTeX{} breaks the \pkg{morewrites}
887 % package. Below is a possible fix for this. This may not be the final
888 % version which is why it is currently placed here (largely to avoid
889 % to distribute another full \texttt{dev} release for no good reason).
890 % \begin{macrocode}
891 \ExplSyntaxOn
892 \cs_set:Npn \__shipout_force_immediate_writes: {
893 \cs_gset_eq:NN \iow_shipout:Nn \iow_now:Nn
894 \cs_gset_eq:NN \lua_shipout:n \lua_now:n
895 \cs_gset_eq:NN \__kernel_write_saved: \write
896 \cs_gset:Npn \write {\immediate \__kernel_write_saved:}
897 \global\advance\c@page\m@ne
898 }
899 \ExplSyntaxOff
900 % \end{macrocode}
901 %
902 %
903 %
904 % \subsection{First aid for AMS classes}
905 %
906 % Some time ago \LaTeX{} fully switched to the new mark
907 % mechanism. However, the AMS classes still overwrite some, but not
908 % all, of the mark commands, i.e., \cs{leftmark}, \cs{rightmark}, and
909 % \cs{markboth}, assuming the old mechanism, and this way leaving it in an
910 % inconsistent state. We therefore switch the definitions back. The
911 % classes should simply stop making these redefinitions (which for a
912 % long time serve no purpose).
913 %
914 % To support alias counter we must adapt \cs{@ynthm} in \pkg{amsthm}
915 % and for the three classes \cls{amsart}, \cls{amsbook} and \cls{amsproc}.
916 % This has to be done
917 % early enough so that \cs{newtheorem} calls in the preamble work correctly.
918 % As \pkg{cleveref} undoes the definition we have to restore them after
919 % the package again.
920 % \changes{v1.1q}{2025/10/21}{Fix AMS classes with respect to marks (gh/1887)}
921 % \changes{v1.1t}{2025/31/12}{Add support for new alias counter}
922 % \begin{macrocode}
923 \ExplSyntaxOn
924 \AddToHook{class/amsart/after}{
925 \DeclareRobustCommand*\markboth[2]{%
926 \mark_insert:nn{2e-left}{#1}
927 \mark_insert:nn{2e-right}{#2}
928 \tl_if_empty:nF{#2}{ \mark_insert:nn{2e-right-nonempty}{#2} }
929 }
930 \cs_set:Npn \leftmark {\mark_use_last:nn{page}{2e-left}}
931 \cs_set:Npn \rightmark {\mark_use_first:nn{page}{2e-right}}
932 }
933 \AddToHook{class/amsproc/after}{
934 \DeclareRobustCommand*\markboth[2]{%
935 \mark_insert:nn{2e-left}{#1}
936 \mark_insert:nn{2e-right}{#2}
937 \tl_if_empty:nF{#2}{ \mark_insert:nn{2e-right-nonempty}{#2} }
938 }
939 \cs_set:Npn \leftmark {\mark_use_last:nn{page}{2e-left}}
940 \cs_set:Npn \rightmark {\mark_use_first:nn{page}{2e-right}}
941 }
942 % \end{macrocode}
943 % The \texttt{amsbook} class would require different adjustments
944 % (\cs{@secmark} would need to use the new mechanism) but with this
945 % class the overwriting still more or less works so we leave it
946 % alone for now.
947 %
948 % \begin{macrocode}
949 \newcommand\@amsthm@reset@ynthm
950 {
951 \def\@ynthm##1[##2]##3{%
952 \ifx\relax##2\relax
953 \def\@tempa{\@oparg{\@xthm{##1}{##3}}[]}%
954 \else
955 \@ifundefined{c@##2}{%
956 \def\@tempa{\@nocounterr{##2}}%
957 }{%
958 \newcounteralias{##1}{##2}% UF
959 \toks@{##3}%
960 \@xp\xdef\csname##1\endcsname{%
961 \@nx\@thm{%
962 \let\@nx\thm@swap
963 \if S\thm@swap\@nx\@firstoftwo\else\@nx\@gobble\fi
964 \@xp\@nx\csname th@\the\thm@style\endcsname}%
965 {##1}{\the\toks@}}% UF changed
966 \let\@tempa\relax
967 }%
968 \fi
969 \@tempa}
970 \let\@amsthm@saved@ynthm\@ynthm
971 }
972 \AddToHook{package/amsthm/after}[firstaid/aliascounter]
973 {
974 \@amsthm@reset@ynthm
975 \AddToHook{package/cleveref/after}[firstaid/aliascounter]{\let\@ynthm\@amsthm@saved@ynthm}
976 }
977 \AddToHook{class/amsart/after}[firstaid/aliascounter]
978 {
979 \@amsthm@reset@ynthm
980 \AddToHook{package/cleveref/after}[firstaid/aliascounter]{\let\@ynthm\@amsthm@saved@ynthm}
981 }
982 \AddToHook{class/amsbook/after}[firstaid/aliascounter]
983 {
984 \@amsthm@reset@ynthm
985 \AddToHook{package/cleveref/after}{\let\@ynthm\@amsthm@saved@ynthm}
986 }
987 \AddToHook{class/amsproc/after}[firstaid/aliascounter]
988 {
989 \@amsthm@reset@ynthm
990 \AddToHook{package/cleveref/after}[firstaid/aliascounter]{\let\@ynthm\@amsthm@saved@ynthm}
991 }
992 \ExplSyntaxOff
993 % \end{macrocode}
994 %
995 %
996 %
997 %
998 % \subsection{The \pkg{cellspace} package first aid}
999 %
1000 % Since this package doesn't seem to see updates we add a firstaid
1001 % for now, so that it becomes usable again.
1002 % \begin{macrocode}
1003 \AddToHook{package/cellspace/after}[firstaid]{%
1004 \FirstAidNeededT{cellspace}{sty}{2022/01/04 v1.9.0 Spacing table cells (JN)}%
1005 {\let\@startpbox@action\@startpbox}%
1006 }
1007 % \end{macrocode}
1008 %
1009 % \begin{macrocode}
1010 %</kernel>
1011 % \end{macrocode}
1012 %
1013 % \Finale
1014 %