summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/koma-script/source/scrlfile-patcholdlatex.dtx
blob: 5f5956dc7132a397be515d4f995bcb6c8d8274d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
% \iffalse^^A meta-comment
% ======================================================================
% scrlfile-patcholdlatex.dtx
% Copyright (c) Markus Kohm, 2002-2021
%
% This file is part of the work `scrlfile' which is part of the LaTeX2e
% KOMA-Script bundle.
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c of the license.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX 
% version 2005/12/01 or later and of this work.
%
% This work has the LPPL maintenance status "author-maintained".
%
% The Current Maintainer and author of this work is Markus Kohm.
%
% This work consists of all files listed in manifest.txt.
% ----------------------------------------------------------------------
% scrlfile-patcholdlatex.dtx
% Copyright (c) Markus Kohm, 2002-2021
%
% Diese Datei ist Teil des Werks `scrlfile', das wiederum Teil des
% LaTeX2e KOMA-Script Pakets ist.
%
% Dieses Werk darf nach den Bedingungen der LaTeX Project Public Lizenz,
% Version 1.3c, verteilt und/oder veraendert werden.
% Die neuste Version dieser Lizenz ist
%   http://www.latex-project.org/lppl.txt
% und Version 1.3c ist Teil aller Verteilungen von LaTeX
% Version 2005/12/01 oder spaeter und dieses Werks.
%
% Dieses Werk hat den LPPL-Verwaltungs-Status "author-maintained"
% (allein durch den Autor verwaltet).
%
% Der Aktuelle Verwalter und Autor dieses Werkes ist Markus Kohm.
% 
% Dieses Werk besteht aus den in manifest.txt aufgefuehrten Dateien.
% ======================================================================
% \fi^^A meta-comment
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \iffalse
%%% From File: $Id: scrlfile-patcholdlatex.dtx 3626 2021-11-01 10:57:06Z kohm $
%<*dtx>
\ifx\ProvidesFile\undefined\def\ProvidesFile#1[#2]{}\fi
\begingroup
  \def\filedate$#1: #2-#3-#4 #5${\gdef\filedate{#2/#3/#4}}
  \filedate$Date: 2021-11-01 11:57:06 +0100 (Mon, 01 Nov 2021) $
  \def\filerevision$#1: #2 ${\gdef\filerevision{r#2}}
  \filerevision$Revision: 3626 $
  \edef\reserved@a{%
    \noexpand\endgroup
    \noexpand\ProvidesFile{scrlfile-patcholdlatex.dtx}%
                          [\filedate\space\filerevision\space
                           KOMA-Script package source
  }%
\reserved@a
%</dtx>
%<package>\ProvidesPackage{scrlfile-patcholdlatex}[%
%!KOMAScriptVersion
%<package>  package 
  (patching old LaTeX kernels)]
%<*dtx>
\ifx\documentclass\undefined
  \input scrdocstrip.tex
  \@@input scrkernel-version.dtx
  \@@input scrstrip.inc
  \KOMAdefVariable{COPYRIGHTFROM}{2002}
  \generate{\usepreamble\defaultpreamble
    \file{scrlfile-patcholdlatex.sty}{%
      \from{scrlfile-patcholdlatex.dtx}{package}%
    }%
  }
  \@@input scrstrop.inc
\else
  \let\endbatchfile\relax
\fi
\endbatchfile
\documentclass[parskip=half-]{scrdoc}
\usepackage[main=english]{babel}
\CodelineIndex
\RecordChanges
\GetFileInfo{scrlfile-patcholdlatex.dtx}
\title{\KOMAScript{} \partname\ \texttt{\filename}%
  \thanks{This file is revision \fileversion\ of file \texttt{\filename}.}}  
\date{\filedate}
\author{Markus Kohm\thanks{mailto:komascript@gmx.info}}

\begin{document}
  \maketitle
  \begin{abstract}
    This package provides hooks before and after loading files, packages or
    classes. It also provides a hook after the last \cs{clearpage} of the
    document. It allowes to replace files, packages and classes by other
    files, packages and classes. It is inteded to be used by package and class
    authors but may also be used by \LaTeX{} users.
  \end{abstract}

  \DocInput{\filename}
\end{document}
%</dtx>
% \fi^^A  meta-comment
%
% \changes{v2.95}{2002/06/11}{First version split from hugh scrclass.dtx}
% \changes{v3.24}{2017/05/06}{standalone manual removed}
% \changes{v3.32}{2020/09/02}{new (sub)-package}
%
% \section{The User Manual of \textsf{scrlfile-patcholdlatex}}
%
% \textsf{scrlfile-patcholdlatex} implements the part of \textsf{scrlfile}
% that patches old \LaTeX{} kernels.
%
% There isn't any user manual for the user level \LaTeXe{} commands in this
% file. Please see the manual of \textsf{scrlfile} for more information about
% \textsf{scrlfile-patcholdlatex}.
%
% \StopEventually{\PrintIndex\PrintChanges}
%
% \section{Implementation of \textsf{scrlfile-patcholdlatex}}
%
%
% \subsection{Patching \LaTeX{} Kernel Macros}
%
% Test whether the used \LaTeX{} is old enough.
% \changes{v3.34}{2021/04/21}{make it more robust agains not recommended
%   direct usage}
% \changes{3.35}{2021/10/31}{\LaTeX{} kernel change results in new variant of
%   \Package{scrfile-hook}}
%    \begin{macrocode}
\@ifundefined{IfFormatAtLeastTF}{}{%
  \IfFormatAtLeastTF{2020/10/01}{%
    \PackageError{scrlfile-hook}{LaTeX too young for this package}{%
      It seems this package has been loaded directly using a LaTeX
      version\MessageBreak
      2020-10-01 or newer. This is not recommended. Please always load
      package\MessageBreak
      scrlfile instead of scrlfile-patcholdlatex.\MessageBreak
      If you would continue, I will try to load scrlfile-hook%
      \IfFormatAtLeast{2021/11/15}{}{-3.34}%
    }%
    \IfFormatAtLeastTF{2021/11/15}{%
      \RequirePackage{scrlfile-hook}%
    }{%
      \RequirePackage{scrlfile-hook-3.34}%
    }%
    \endinput
  }{}%
}
%    \end{macrocode}
%
% \begin{macro}{\InputIfFileExists}
% \changes{v2.96}{2006/07/19}{replacement code added}%^^A
% \LaTeX{} uses this macro for loading files with
% \cs{input}\marg{filename}, \cs{include}, \cs{LoadClass},
% \cs{LoadClassWithOptions}, \cs{documentclass}, \cs{documentstyle},
% \cs{RequirePackage}, \cs{usepackage}. Only
% \cs{input}~\meta{filename} loads \meta{filename} without using
% \cs{InputIfFileExists}.
%
% Because of this, it's very easy to add stuff before and after
% loading a file. We simply change this macro and add to
% macros\,---\,one before loading the file and one after loading the
% file. But before we redefine the macro, we test, if the macro was
% defined like we expected. \LaTeX{} will warn, if not.
% \changes{v3.27}{2019/10/06}{\LaTeX{} 2019-10-01 changed the definition}%^^A
%
% \changes{v3.28}{2019/11/21}{support for \textsf{memoir}}%^^A
% With \textsf{memoir} their is a special case. It also redefines
% \cs{InputIfFileExists}. As shown in the source of the class it does so
% because \textsf{scrlfile} does it. However it does it not compatible with
% \textsf{scrlfile} and it does not emulate \textsf{scrlfile} as it do for
% other packages. So if you try to use \textsf{scrlfile} with memoir,
% \textsf{scrbase} has to take care to \textsf{memoir}. And in oposit to
% \textsf{scrlfile} \textsf{memoir} does not always fix the argument order bug
% of \LaTeX{} but do so only for the implementation of \LaTeX{} 2019-10-01.
% So in case of \textsf{memoir} we just patch every version of it without
% testing.
%    \begin{macrocode}
\@ifclassloaded{memoir}{%
  \PackageInfo{scrlfile}{class `memoir' detected.\MessageBreak
    Using a compatible redefinition of \string\InputIfFileExists,\MessageBreak
    but always robust and with the fix of the\MessageBreak
    argument order bug even for old versions of the\MessageBreak
    LaTeX kernel%
  }%
}{%
  \long\def\reserved@a#1#2{%
    \IfFileExists{#1}%
      {#2\@addtofilelist{#1}\@@input \@filef@und}}%
  \ifx\reserved@a\InputIfFileExists\else
    \expandafter\CheckCommand\expandafter*\expandafter\InputIfFileExists
    \expandafter{%
      \expandafter\protect\csname InputIfFileExists \endcsname
    }%
    \expandafter\CheckCommand \csname InputIfFileExists \endcsname[2]{%
      \IfFileExists {#1}{%
        \expandafter \@swaptwoargs \expandafter {\@filef@und }%
        {#2\@addtofilelist {#1}\@@input }%
      }%
    }%
  \fi
}
%    \end{macrocode}
% Now we can redefine the macro like described above:
%    \begin{macrocode}
\expandafter\renewcommand\expandafter*\expandafter\InputIfFileExists
\expandafter{\expandafter
  \protect\csname InputIfFileExists \endcsname}%
\expandafter\long\expandafter\def\csname InputIfFileExists \endcsname#1#2{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1-@alias\endcsname\relax
    \expandafter\@secondoftwo
  \else
    \scr@replacefile@msg{\csname #1-@alias\endcsname}{#1}%
    \expandafter\@firstoftwo
  \fi
  {%
    \expandafter\InputIfFileExists\expandafter{\csname
      #1-@alias\endcsname}{#2}%
  }%
  {\IfFileExists{#1}{%
      \expandafter\scr@input@withhook\expandafter{\@filef@und}{#1}{#2}%
    }}%
}
%    \end{macrocode}
% \begin{macro}{\scr@input@withhook}
% \changes{v3.27}{2019/03/28}{new (internal)}%^^A
% \changes{v3.28}{2019/11/21}{support for \textsf{memoir}}%^^A
% We need a new internal, because of expanding \cs{@file@und} before inputting
% the file.
%    \begin{macrocode}
\@ifclassloaded{memoir}{%
  \newcommand{\scr@input@withhook}[3]{%
    \scr@load@hook{before}{#2}%
    #3\@addtofilelist{#2}%
    \@ifundefined{m@matbeginf}{}{\m@matbeginf{#2}}%
    \@@input #1%
    \@ifundefined{m@matendf}{}{\m@matendf{#2}}%
    \@ifundefined{killm@matf}{}{\killm@matf{#2}}%
    \scr@load@hook{after}{#2}%
  }%
}{%
  \newcommand{\scr@input@withhook}[3]{%
    \scr@load@hook{before}{#2}%
    #3\@addtofilelist{#2}%
    \@@input #1%
    \scr@load@hook{after}{#2}%
  }%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@loadwithoptions}
% \changes{v2.96}{2006/07/19}{new redefinition}
% \begin{macro}{\scr@loadwithoptions}
% \changes{v2.96}{2006/07/19}{new helper macro}
% \begin{macro}{\scr@@loadwithoptions}
% \changes{v2.97a}{2007/03/07}{new helper macro}
% \LaTeX{} uses this macro for loading classes and packages using
% \cs{LoadClassWithOptions} or \cs{RequirePackageWithOptions}. We simply say
% now: if the class or package should be replaced by another, try it again
% with the replacement name. The stack is used. This will result in an
% error if the replacements are nested too deep. This should be, because it
% will also result in an error using ring replacements.
%    \begin{macrocode}
\newcommand*{\scr@@loadwithoptions}{}
\newcommand*{\scr@loadwithoptions}{}
\let\scr@loadwithoptions\@loadwithoptions
\renewcommand*{\@loadwithoptions}[3]{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #3.#1-@aliasname\endcsname\relax
    \def\scr@@loadwithoptions{\scr@loadwithoptions{#1}{#2}{#3}}%
  \else
    \scr@replacefile@msg{\csname #3.#1-@aliasname\endcsname.#1}{#3.#1}% 
    \def\scr@@loadwithoptions{%
      \@loadwithoptions{#1}{#2}{\csname #3.#1-@aliasname\endcsname}%
    }%
  \fi
  \scr@@loadwithoptions
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@onefilewithoption}
% \changes{v2.96}{2006/07/19}{new redefinition}
% \begin{macro}{\scr@onefilewithoption}
% \changes{v2.96}{2006/07/19}{new helper macro}
% \begin{macro}{\scr@@onefilewithoption}
% \changes{v2.98}{2007/12/18}{new helper macro}
% \LaTeX{} uses this macro for loading classes and packages using
% \cs{LoadClass}, \cs{documentclass}, \cs{RequirePackage}, and
% \cs{usepackage}. We simply say now: if the class or package should be
% replaced by another, try it again with the replacement name. The stack is
% used. This will result in an error if the replacements are nested too
% deep. This should be, because it will also result in an error using ring
% replacements.
%    \begin{macrocode}
\newcommand*{\scr@onefilewithoptions}{}
\let\scr@onefilewithoptions\@onefilewithoptions
\def\@onefilewithoptions#1[#2][#3]#4{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1.#4-@aliasname\endcsname\relax
%    \end{macrocode}
% Do this ridiculous definition and call outside of \cs{ifx}\dots\cs{fi}
% because of some errornous packages like some versions of
% \textsf{breakurl}. This does not avoid an error, but the message about
% unclosed \cs{if} will be outside of \textsf{scrlfile}.
%    \begin{macrocode}
    \def\scr@@onefilewithoptions{\scr@onefilewithoptions{#1}}%
  \else
    \scr@replacefile@msg{\csname #1.#4-@aliasname\endcsname.#4}{#1.#4}%
    \edef\scr@@onefilewithoptions{%
      \noexpand\@onefilewithoptions{\csname #1.#4-@aliasname\endcsname}}%
  \fi 
  \scr@@onefilewithoptions[{#2}][{#3}]{#4}%
  \scr@load@hook{lateafter}{#1.#4}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%  \begin{macro}{\scr@replacefile@msg}
% \changes{v2.96}{2006/07/19}{new helper command}
% \changes{v2.98}{2007/12/18}{spellfix}
% Last but not least \textsf{scrlfile} will write a message to the log file at
% every replacement.
%    \begin{macrocode}
\newcommand*{\scr@replacefile@msg}[2]{%
  \PackageInfo{scrlfile}{inputing `#1' instead of `#2'}%
}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\scr@load@hook}
% \changes{v3.27a}{2019/10/16}{workaround for strange handling of
%   \cs{AtEndOfPackage} hook in \cs{@onefilewithoptions}}%^^A
% This macro simply calls a load-file-hook, either the \emph{before}
% or the \emph{after} one, if the hook exists. After calling the hook
% it will be deleted. First argument is the type of hook
% (\meta{before} or \meta{after}), second argument is a filename. 
%    \begin{macrocode}
\newcommand*{\scr@load@hook}[2]{%
%    \end{macrocode}
% \begin{macro}{\scrlfile@cmp@hook}
% \changes{v3.27a}{2019/10/23}{local macros renamed}%^^A
% \begin{macro}{\scrlfile@hookname}
% \changes{v3.27a}{2019/10/23}{local macros renamed}%^^A
% \changes{v3.29}{2020/06/01}{do the class and package load hooks only in the
%   document preamble}%^^A --> https://komascript.de/node/2274
% Workaround for strange handling of the \cs{AtEndOfPackage} hook in
% \cs{@onefilewithoptions} (becomes \cs{@empty}, if the package already has
% been loaded and is not loaded again). This workaround is not nice but
% smart. Before loading a file it defines a marker macro and undefines it
% later. To decide whether to define or remove the marker, two local macros
% are used \cs{scrlfile@hookname} and \cs{scrlfile@cmp@hook}. The first has
% the name of the current hook from the first argument. The other is a compare
% string. The first one is also valid while processing the hook, but only in
% the document preamble. The names are very unique, so we do not reserve them
% using \cs{newcommand} outside. The name of the marker is less
% unique. Nevertheless it is not reserved.
% Note: For recursive loading of files this does not work. But this also does
% not matter, because it is needed only for packages and classes, which are
% never loaded recursive. For the same reason from \KOMAScript{} 3.29 this is
% only used inside the document preamble. After the document preamble it can
% be dangerous, e.g., inside a |tabular| or |array|.
%    \begin{macrocode}
  \ifx\@begindocumenthook\@undefined\else
    \edef\scrlfile@hookname{#1}%
    \def\scrlfile@cmp@hook{before}%
    \ifx\scrlfile@hookname\scrlfile@cmp@hook
      \expandafter\def\csname #2-l@ading\endcsname{#2}%
    \fi
    \let\scrlfile@cmp@hook\@undefined
  \fi
  \@ifundefined{#2-@#1}{}{%
    \@nameuse{#2-@#1}%
    \expandafter\let\csname #2-@#1\endcsname\relax
  }%
%    \end{macrocode}
% The hook could destroy \cs{scrlfile@hookname} so define it again:
%    \begin{macrocode}
  \ifx\@begindocumenthook\@undefined\else
    \edef\scrlfile@hookname{#1}%
    \def\scrlfile@cmp@hook{after}%
    \ifx\scrlfile@hookname\scrlfile@cmp@hook
      \expandafter\let\csname #2-l@ading\endcsname\@undefined
    \fi
    \let\scrlfile@cmp@hook\@undefined
    \let\scrlfile@hookname\@undefined
  \fi
}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%
% \begin{macro}{\scr@set@load@hook}
% This command adds stuff to a load-file-hook, either the
% \emph{before} or the \emph{after} one. First argument is the type of
% hook (\meta{before} or \meta{after}), second is a filename. There's
% a third argument not parsed by the macro itself but
% \cs{g@addto@macro}. This third argument is the stuff, which will be
% added to the hook. Because of using \cs{g@addto@macro} the hook must
% be defined. So we define it, if it isn't.
% TODO: Handling/ignoring an optional argument to make it more compatible with
% \LaTeX's new \cs{AddToHook}.
%    \begin{macrocode}
\newcommand*{\scr@set@load@hook}[2]{%
  \@ifundefined{#2-@#1}{\@namedef{#2-@#1}{}}{}%
  \expandafter\g@addto@macro\csname #2-@#1\endcsname
}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Before and After Commands}
%
% \begin{macro}{\BeforeFile}
% \begin{macro}{\AfterFile}
% Just add code to the \emph{before} or \emph{after} hook of a file. The
% arguments are:
% \begin{description}
% \item[\meta{file name}:] mandatory argument with the file name as given by
%   \cs{InputIfFileExists}.
% \item[\meta{code}:] this argument is not processed by \cs{BeforeFile} or
%   \cs{AfterFile} but the internal \cs{scr@set@load@hook}. It is the code
%   that should be added to either the \emph{before} or the \emph{after} hook.
% \end{description}
%    \begin{macrocode}
\newcommand*{\BeforeFile}{\scr@set@load@hook{before}}
\newcommand*{\AfterFile}{\scr@set@load@hook{after}}
%    \end{macrocode}
% \end{macro}%^^A \AfterFile
% \end{macro}%^^A \BeforeFile
%
% \begin{macro}{\BeforeClass}
% \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
%   \cs{kernel@ifnextchar}}%^^A
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} durch \cs{kernel@ifstar}
%   ersetzt}%^^A
% This is quite simple. It only has to add code to the \emph{before} hook
% already known by \cs{BeforeFile}. The arguments are:
% \begin{description}
% \item[\meta{package name}:] mandatory argument.
% \item[\meta{code}:] this argument is processed not by \cs{BeforeClass} but
%   by the internal \cs{scr@set@load@hook} (see above), that will add the code
%   to the \emph{before} hook of file \meta{code}\texttt{.cls} (or whatever the
%   package extension is).
% \end{description}
%    \begin{macrocode}
\newcommand*{\BeforeClass}[1]{%
  \scr@set@load@hook{before}{#1.\@clsextension}%
}
%    \end{macrocode}
% \end{macro}%^^A \BeforeClass
%
% \begin{macro}{\AfterClass}
% \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
%   \cs{kernel@ifnextchar}}
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
% \changes{v3.32}{2020/09/02}{plus and exclamation mark variants removed}
% \begin{macro}{\AfterAtEndOfClass}
% \changes{v3.32}{2020/09/02}{star variant replaces the exclamation mark
%   variant of \cs{AfterClass}}
% These are more complicated. There are four forms:
% \begin{description}
% \item[\cs{AfterClass}] corresponds to \cs{AfterFile}. So the code will be
%   added to the usual \emph{after} hook and the code is exexuted before code
%   added by the class itself using \cs{AtEndOfClass}. Argument procession
%   will be done by \cs{@AfterClass}.
% \item[\cs{AfterAtEndOfClass}] This uses the \emph{lateafter} hook, which
%   is executed after the class has been loaded completely inclusive the
%   execution of \cs{AtEndOfClass} code. However, the code will never be
%   executed, if the class will not be loaded (again). But it does nothing,
%   if the class has already been loaded. The arguments are:
%   \begin{description}
%   \item[\meta{class name}] mandatory argument
%   \item[\meta{code}:] this argument is processed not by \cs{BeforeClass}
%     but by the internal \cs{scr@set@load@hook} (see above), that will add
%     the code to the \emph{lateafter} hook of file \meta{code}\texttt{.cls}
%     (or whatever the class extension is), or by \cs{@gobble}.
%   \end{description}
% \item[\cs{AfterClass*}] like \cs{AfterClass}, but if loading of the
%   class has already began, the code will be executed immediately. So, if
%   the command is used inside the class it is simply
%   \cs{@firstofone}. Argument procession will be done by \cs{@sAfterClass}.
% \item[\cs{AfterAtEndOfClass*}] like \cs{AfterClass*}, but the code will be
%   executed immediately only if the class already has been loaded
%   completely. Inside \cs{AtEndOfClass} the code is executed after every all
%   the \cs{AtEndOfClass} code. Argument procession will be done by
%   \cs{@xAfterClass}.
% \end{description}
%    \begin{macrocode}
\newcommand*{\scrlfile@AfterClass}{%
  \kernel@ifstar \@sAfterClass\@AfterClass
}
\newcommand*{\AfterClass}{\scrlfile@AfterClass}
%    \end{macrocode}
% \begin{macro}{\@AfterClass}
% \cs{AfterClass}\marg{class name}\marg{code}.
% It uses \cs{scr@set@load@hook} to add \meta{code} to the
% \emph{after} hook. The second argument will be processed by
% \cs{scr@set@load@hook}.
%    \begin{macrocode}
\newcommand*{\@AfterClass}[1]{%
  \scr@set@load@hook{after}{#1.\@clsextension}%
}
%    \end{macrocode}
% \end{macro}%^^A \@AfterClass
% \begin{macro}{\@sAfterClass}
% \changes{v3.27a}{2019/10/16}{group removed}%^^A
% This is the internal implementation of:
% \cs{AfterClass*}\marg{class name}\marg{code}.
% It tests, whether or not \LaTeX{} thinks that the class
% has already been loaded, but this only means whether or not the class
% loading has already been started. If so, it immediately executes the next
% argument. If not, is uses \cs{@AfterClass} to execute the \meta{code}
% after loading the class.
%    \begin{macrocode}
\newcommand*{\@sAfterClass}[1]{%
  \@ifclassloaded{#1}{%
    \@secondoftwo
  }{%
    \@AfterClass
  }%
  {#1}%
}
%    \end{macrocode}
% \end{macro}%^^A \@sAfterClass
%    \begin{macrocode}
\newcommand*{\AfterAtEndOfClass}{%
  \kernel@ifstar \@sAfterAtEndOfClass\@AfterAtEndOfClass
}
%    \end{macrocode}
% \begin{macro}{\@AfterAtEndOfClass}
% \changes{v3.32}{2020/09/02}{new (internal)}
% Note that \cs{AfterAtEndOfClass} executed the \meta{code} after each try
% to load a class, even if the class has already been loaded and you try
% to load it again.
%    \begin{macrocode}
\newcommand*{\@AfterAtEndOfClass}[1]{%
  \scr@set@load@hook{lateafter}{#1.\@clsextension}%
}
%    \end{macrocode}
% \begin{macro}{\@sAfterAtEndOfClass}
% \changes{v3.09}{2011/03/09}{new (internal)}%^^A
% \changes{v3.27}{2019/08/26}{using inside class fixed}%^^A
% \changes{v3.27a}{2019/10/15}{renamed from \cs{@@@AfterClass}}%^^A
% \changes{v3.27a}{2019/10/16}{usage of \emph{l@ading} marker}%^^A
% \changes{v3.27a}{2019/10/16}{group removed}%^^A
% \changes{v3.32}{2020/09/02}{renamed from \cs{@xAfterClass}}
% This is the internal implementation of:
% \cs{AfterClass!}\marg{class name}\marg{code}. Note, that the exclamation
% mark has not already read. So the arguments are:
% \begin{description}
% \item[\texttt{!}:] static exclamation mark, to be ignored.
% \item[\meta{class name}] mandatory argument
% \item[\meta{code}:] this argument is processed not by \cs{BeforeClass} but
%   by the internal \cs{@sAfterClass} or \cs{@AfterClass} (see above).
% \end{description}
% There are two differences to \cs{@pAfterClass}:
% \begin{enumerate}
% \item In case of \cs{\meta{class name}.cls-l@ading} is defined (and not
%   \cs{relax}), while loading a class, it used \cs{AfterAtEndOfClass}
%   instead of \cs{AfterClass} to add \meta{code} to the \emph{lateafter}
%   hook instead of the \emph{after} hook.
% \item In case of \cs{\meta{class name}.cls-l@ading} is undefined (or
%   \cs{relax}) it also has to test, whether or not the class has already
%   been loaded. If so, it uses \cs{@sAfterClass} to execute the \meta{code}
%   immediately, otherwise it uses \cs{AfterAtEndOfClass} to add the
%   \meta{code} to the \emph{lateafter} hook.
% \end{enumerate}
% So it is more like (the not existing) \cs{AfterAtEndOfClass+} than
% \cs{AfterAtEndOfClass*}.
%    \begin{macrocode}
\newcommand*{\@sAfterAtEndOfClass}[1]{%
  \@ifundefined{#1.\@clsextension-l@ading}{%
    \@ifclassloaded{#1}{%
      \@secondoftwo
    }{%
      \@AfterAtEndOfClass
    }%
  }{%
    \@AfterAtEndOfClass
  }%
  {#1}%
}
%    \end{macrocode}
% \end{macro}%^^A \@sAfterAtEndOfClass
% \end{macro}%^^A \AfterAtEndOfClass
% \end{macro}%^^A \AfterClass
%
% \begin{macro}{\BeforePackage}
% This is quite simple. It only has to add code to the \emph{before} hook
% already known by \cs{BeforeFile}. The arguments are:
% \begin{description}
% \item[\meta{package name}:] mandatory argument.
% \item[\meta{code}:] this argument is processed not by \cs{BeforePackage} but
%   by the internal \cs{scr@set@load@hook} (see above), that will add the code
%   to the \emph{before} hook of file \meta{code}\texttt{.sty} (or whatever the
%   package extension is).
% \end{description}
%    \begin{macrocode}
\newcommand*{\BeforePackage}[1]{%
  \scr@set@load@hook{before}{#1.\@pkgextension}%
}
%    \end{macrocode}
% \end{macro}%^^A \BeforePackage
%
% \begin{macro}{\AfterPackage}
% \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
%   \cs{kernel@ifnextchar}}%^^A
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} durch \cs{kernel@ifstar}
%   ersetzt}%^^A
% \begin{macro}{\AfterAtEndOfPackage}
% These are more complicated. There are four forms:
% \begin{description}
% \item[\cs{AfterPackage}] corresponds to \cs{AfterFile}. So the code will be
%   added to the usual \emph{after} hook and the code is exexuted before code
%   added by the package itself using \cs{AtEndOfPackage}. Argument procession
%   will be done by \cs{@AfterPackage}.
% \item[\cs{AfterAtEndOfPackage}] This uses the \emph{lateafter} hook, which
%   is executed after the package has been loaded completely inclusive the
%   execution of \cs{AtEndOfPackage} code. However, the code will never be
%   executed, if the package will not be loaded (again). But it does nothing,
%   if the package has already been loaded. The arguments are:
%   \begin{description}
%   \item[\meta{package name}] mandatory argument
%   \item[\meta{code}:] this argument is processed not by \cs{BeforePackage}
%     but by the internal \cs{scr@set@loaf@hook} (see above), that will add
%     the code to the \emph{lateafter} hook of file \meta{code}\texttt{.sty}
%     (or whatever the package extension is), or by \cs{@gobble}.
%   \end{description}
% \item[\cs{AfterPackage*}] like \cs{AfterPackage}, but if loading of the
%   package has already began, the code will be executed immediately. So, if
%   the command is used inside the package it is simply
%   \cs{@firstofone}..
% \item[\cs{AfterAtEndOfPackage*}] like \cs{AfterPackage*}, but the code will
%   be executed immediately only if the package already has been loaded
%   completely. Inside \cs{AtEndOfPackage} the code is executed after every
%   all the \cs{AtEndOfPackage} code.
% \end{description}
%    \begin{macrocode}
\newcommand*{\scrlfile@AfterPackage}{%
  \kernel@ifstar \@sAfterPackage\@AfterPackage
}
\newcommand*{\AfterPackage}{\scrlfile@AfterPackage}
%    \end{macrocode}
% \begin{macro}{\@AfterPackage}
% This is the internal implementation of:
% \cs{AfterPackage}\marg{package name}\marg{code}.
% It uses \cs{scr@set@load@hook} to add \meta{code} to the
% \emph{after} hook. The second argument will be processed by
% \cs{scr@set@load@hook}.
%    \begin{macrocode}
\newcommand*{\@AfterPackage}[1]{%
  \scr@set@load@hook{after}{#1.\@pkgextension}%
}
%    \end{macrocode}
% \end{macro}%^^A \@AfterPackage
% \begin{macro}{\@sAfterPackage}
% \changes{v3.27a}{2019/10/16}{group removed}%^^A
% This is the internal implementation of:
% \cs{AfterPackage*}\marg{package name}\marg{code}.
% It tests, whether or not \LaTeX{} thinks that the package
% has already been loaded, but this only means whether or not the package
% loading has already been started. If so, it immediately executes the next
% argument. If not, is uses \cs{@AfterPackage} to execute the \meta{code}
% after loading the package.
%    \begin{macrocode}
\newcommand*{\@sAfterPackage}[1]{%
  \@ifpackageloaded{#1}{%
    \@secondoftwo
  }{%
    \@AfterPackage
  }%
  {#1}%
}
%    \end{macrocode}
% \end{macro}%^^A \@sAfterPackage
% \begin{macrocode}
\newcommand*{\AfterAtEndOfPackage}{%
  \kernel@ifstar \@sAfterAtEndOfPackage\@AfterAtEndOfPackage
}
% \end{macrocode}
% \begin{macro}{\@sAfterAtEndOfPackage}
% \changes{v3.09}{2011/03/09}{new (internal)
% \changes{v3.27}{2019/08/26}{using inside package fixed}
% \changes{v3.27a}{2019/10/15}{renamed from \cs{@@@AfterPackage}
% \changes{v3.27a}{2019/10/16}{usage of \emph{l@ading} marker}
% \changes{v3.27a}{2019/10/16}{group removed}
% \changes{v3.32}{2020/09/02}{renamed from \@xAfterPackage}
% This is the internal implementation of:
% \cs{AfterAtEndOfPackage*}\marg{package name}\marg{code}.
%    \begin{macrocode}
\newcommand*{\@sAfterAtEndOfPackage}[1]{%
  \@ifundefined{#1.\@pkgextension-l@ading}{%
    \@ifpackageloaded{#1}{%
      \@secondoftwo
    }{%
      \@@AfterAtEndOfPackage
    }%
  }{%
    \@@AfterAtEndOfPackage
  }%
  {#1}%
}
%    \end{macrocode}
% \end{macro}%^^A \@sAfterAtEndOfPackage
% \begin{macro}{\@AfterAtEndOfPackage}
% \changes{v3.32}{2020/09/02}{new (internal)}
% \begin{macro}{\@@AfterAtEndOfPackage}
% \changes{v3.27a}{2019/10/15}{new (internal)}
% \changes{v3.27a}{2019/10/16}{group removed}
% \changes{v3.32}{2020/09/02}{renamed from \cs{@AfterAtEndOfPackage}}
% Note that until 3.27a \cs{AfterAtEndOfPackage} executed the \meta{code}
% after each try to load a package, even if the package has already been
% loaded and you've try to load it again. Since 3.27a this is the
% functionality of \cs{@AfterAtEndOfPackage}. \cs{AfterAtEndOfPackage} now
% does not do anything for already loaded packages or while loading the
% package.
%    \begin{macrocode}
\newcommand*{\@AfterAtEndOfPackage}[1]{%
  \@ifpackageloaded{#1}{%
    \@ifundefined{#1.\@pkgextension-l@ading}{%
      \@gobbletwo
    }{%
      \@@AfterAtEndOfPackage
    }%
  }{%
    \@@AfterAtEndOfPackage
  }%
  {#1}%  
}
\newcommand*{\@@AfterAtEndOfPackage}[1]{%
  \scr@set@load@hook{lateafter}{#1.\@pkgextension}%
}
%    \end{macrocode}
% \end{macro}%^^A \@@AfterAtEndOfPackage
% \end{macro}%^^A \@AfterEndOfPackage
% \end{macro}%^^A \AfterAtEndOfPackage
% \end{macro}%^^A \AfterPackage
%  
%
% \subsection{File Substitution}
%
% \begin{macro}{\ReplaceInput}
% \changes{v2.96}{2006/07/19}{new command}
% \changes{v3.12}{2013/11/15}{implemented}
% Defining the replacement by definition of \cs{\#1-@alias}.
%    \begin{macrocode}
\newcommand*{\ReplaceInput}[2]{%
  \expandafter\edef\csname #1-@alias\endcsname{#2}%
}
%    \end{macrocode}
% \begin{macro}{\ReplaceFile}
% \changes{v2.96}{2006/07/19}{new command}
% \changes{v3.12}{2013/11/15}{throw error}
% Sorry, but I've implemented the wrong command some time ago.
%    \begin{macrocode}
\newcommand*{\ReplaceFile}{%
  \PackageError{scrlfile}{%
    usage of not documented command `\string\ReplaceFile'%
  }{%
    Command `\string\ReplaceFile' has been implemented accidentally in
    KOMA-Script 2.96\MessageBreak
    and replaced by `\string\ReplaceInput' in KOMA-Script 3.12.\MessageBreak
    You should replace `\string\ReplaceFile' by `\string\ReplaceInput' to
    avoid this error\MessageBreak
    message%
  }%
  \ReplaceInput
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\UnReplaceInput}
% \changes{v3.12}{2013/11/15}{new command}
% Do not longer replace a file.
%    \begin{macrocode}
\newcommand*{\UnReplaceInput}[1]{%
  \ifcsname #1-@alias\endcsname
    \expandafter\let\csname #1-@alias\endcsname\relax
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ReplacePackage}
% \changes{v2.96}{2006/07/19}{new command}
% \begin{macro}{\ReplaceClass}
% \changes{v2.96}{2006/07/19}{new command}
% \begin{macro}{\@replacefilewithext}
% \changes{v2.96}{2006/07/19}{new helper command}
% \changes{v3.32}{2020/09/09}{works global}
% Note that replacing a package or class will not replace the input of the
% file of the package or class but only the input of package or class.
%    \begin{macrocode}
\newcommand*{\@replacefilewithext}[3]{%
  \expandafter\xdef\csname #1.#3-@aliasname\endcsname{#2}%
}
\newcommand*{\ReplacePackage}[2]{%
  \@replacefilewithext{#1}{#2}\@pkgextension
}
\newcommand*{\ReplaceClass}[2]{%
  \@replacefilewithext{#1}{#2}\@clsextension
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\UnReplacePackage}
% \changes{v3.12}{2013/11/15}{new command}
% \begin{macro}{\UnReplaceClass}
% \changes{v3.12}{2013/11/15}{new command}
% \begin{macro}{\@unreplacefilewithext}
% \changes{v3.12}{2013/11/15}{new helper command}%^^A
% \changes{v3.32}{2020/09/09}{works global}
% Do not nolonger replace a class or package.
%    \begin{macrocode}
\newcommand*{\@unreplacefilewithext}[2]{%
  \ifcsname #1.#2-@aliasname\endcsname
    \expandafter\global\expandafter\let\csname #1.#2-@aliasname\endcsname\relax
  \fi
}
\newcommand*{\UnReplacePackage}[1]{%
  \@unreplacefilewithext{#1}\@pkgextension
}
\newcommand*{\UnReplaceClass}[1]{%
  \@unreplacefilewithext{#1}\@clsextension
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsection{Prevent Package from Loading}
%
% \begin{macro}{\PreventPackageFromLoading}
% \changes{v3.08}{2010/11/04}{New}
% \changes{v3.11a}{2012/06/13}{write fault error in \cs{MessageBreak} fixed}
% \changes{v3.12}{2012/06/15}{new star version allowes empty argument with
%   info instead of warning}
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
% \changes{v3.32}{2020/09/09}{works global}
% \begin{macro}{\@PreventPackageFromLoading}
% \changes{v3.12}{2012/06/15}{New (internal)}%^^A
% \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
%   \cs{kernel@ifnextchar}}%^^A
% \begin{macro}{\@@PreventPackageFromLoading}
% \changes{v3.12}{2013/11/22}{New (internal)}%^^A
% \changes{v3.12}{2013/11/22}{\cs{usepackage} before \cs{documentclass}
%   fixed}%^^A
% \begin{macro}{\scrlfile@excludedpackages}
% \changes{v3.08}{2010/11/04}{New (internal)}%^^A
% \begin{macro}{\scrlfile@saved@RequirePackage}
% \changes{v3.08}{2010/11/04}{New (internal)}%^^A
%    \begin{macrocode}
\newcommand*{\scrlfile@excludedpackages}{}
\let\scrlfile@excludedpackages\@empty
\newcommand*{\scrlfile@saved@RequirePackage}{}
\newcommand*{\PreventPackageFromLoading}{%
  \kernel@ifstar{\@PreventPackageFromLoading\PackageInfo}%
          {\@PreventPackageFromLoading\PackageWarning}%
}
\newcommand*{\@PreventPackageFromLoading}[1]{%
  \kernel@ifnextchar [%]
    {\@@PreventPackageFromLoading#1}{\@@PreventPackageFromLoading#1[]}%
}
\newcommand*{\@@PreventPackageFromLoading}{}
\def\@@PreventPackageFromLoading#1[#2]#3{%
%    \end{macrocode}
% We do nothing, if the argument was empty:
%    \begin{macrocode}
  \edef\reserved@b{\zap@space#3 \@empty}%
  \ifx \reserved@b\@empty
    #1{scrlfile}{%
      \string\PreventPackageFromLoading\space with empty packages\MessageBreak
      argument ignored%
    }%
  \else
%    \end{macrocode}
% If the argument was not empty we first need to patch \cs{RequirePackage} and
% maybe \cs{usepackage} too.
%    \begin{macrocode}
    \ifx\scrlfile@excludedpackages\@empty
      \global\let\scrlfile@saved@RequirePackage\RequirePackage
      \global\let\RequirePackage\scrlfile@RequirePackage
      \def\reserved@a##1##{%  
        \@latex@error
        {\noexpand \usepackage before \string\documentclass}%
        {\noexpand \usepackage may only appear in the document
          preamble, i.e.,\MessageBreak
          between \noexpand\documentclass and
          \string\begin{document}.}%
        \@gobble}%
      \ifx\reserved@a\usepackage\else
        \ifx\usepackage\scrlfile@saved@RequirePackage\else
          \PackageWarning{scrlfile}{%
            Maybe serious problem: unexpected definition of\MessageBreak
            \string\usepackage
          }%
        \fi
        \global\let\usepackage\RequirePackage
      \fi
    \fi
%    \end{macrocode}
% We have to test, if a package was already loaded. Only not yet loaded
% packages will be added to the exclusion list. Empty arguments will be
% ignored.
% \changes{v3.11a}{2012/06/11}{missing \cs{scrlfile@excludedpackages} at
%   redefinition of \cs{scrlfile@excludedpackages} added}%^^A
% \changes{v3.26}{2018/08/29}{spurious \cs{expandafter} removed}%^^A
%    \begin{macrocode}
    \@for\reserved@a:=\reserved@b\do {%
      \ifx\reserved@a\@empty
        #1{scrlfile}{%
          empty package argument for
          \string\PreventPackageFromLoading\MessageBreak
          ignored%
        }%
      \else
        \expandafter\@ifpackageloaded\expandafter{\reserved@a}{%
          #1{scrlfile}{%
            package `\reserved@a' already loaded.\MessageBreak
            Cannot prevent it from beeing loaded%
          }%
        }{%
          \ifx\scrlfile@excludedpackages\@empty
            \global\let\scrlfile@excludedpackages\reserved@a
          \else
            \xdef\scrlfile@excludedpackages{\scrlfile@excludedpackages,%
              \reserved@a}%
          \fi
          \if\relax\detokenize{#2}\relax\else
            \@ifundefined{scrlfile@exclude@package@\reserved@a @do}{%
              \@namedef{scrlfile@exclude@package@\reserved@a @do}{}%
            }{}%
            \expandafter\gdef
            \csname scrlfile@exclude@package@\reserved@a 
                    @do\expandafter\expandafter\expandafter\endcsname
            \expandafter\expandafter\expandafter{%
              \csname scrlfile@exclude@package@\reserved@a @do\endcsname 
              #2%
            }%
          \fi
        }%
      \fi
    }%
%    \end{macrocode}
% If the exclusion list is still empty (because all packages has been loaded
% already), remove the patch:
%    \begin{macrocode}
    \ifx\scrlfile@excludedpackages\@empty
      \ResetPreventPackageFromLoading
    \fi
  \fi
}
\@onlypreamble\PreventPackageFromLoading
%    \end{macrocode}
% \begin{macro}{\ResetPreventPackageFromLoading}
% \changes{v3.08}{2010/11/04}{New}
% \changes{v3.32}{2020/09/09}{works global}
% \begin{macro}{\StorePreventPackageFromLoading}
% \changes{v3.08}{2010/11/04}{New}
% These two are very simple
%    \begin{macrocode}
\newcommand*{\ResetPreventPackageFromLoading}{%
  \global\let\scrlfile@excludedpackages\@empty
  \ifx\RequirePackage\scrlfile@RequirePackage
    \ifx\usepackage\RequirePackage
      \global\let\usepackage\scrlfile@saved@RequirePackage
    \fi
    \global\let\RequirePackage\scrlfile@saved@RequirePackage
  \fi
}
\@onlypreamble\ResetPreventPackageFromLoading
\newcommand*{\StorePreventPackageFromLoading}[1]{%
  \let#1\scrlfile@excludedpackages
}
\@onlypreamble\StorePreventPackageFromLoading
%    \end{macrocode}
% \begin{macro}{\UnPreventPackageFromLoading}
% \changes{v3.12}{2013/11/22}{New}%^^A
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} durch \cs{kernel@ifstar}
%     ersetzt}%^^A
% \changes{v3.32}{2020/09/09}{works global}
% \begin{macro}{\@UnPreventPackageFromLoading}
% \changes{v3.12}{2013/11/22}{New (internal)}%^^A
% This removes packages from the prevent exclude list. The star variant also
% undefines the instead code.
%    \begin{macrocode}
\newcommand*{\UnPreventPackageFromLoading}{%
  \kernel@ifstar {\@tempswatrue\@UnPreventPackageFromLoading}%
           {\@tempswafalse\@UnPreventPackageFromLoading}%
}
\newcommand*{\@UnPreventPackageFromLoading}[1]{%
  \edef\reserved@b{\zap@space#1 \@empty}%
  \if@tempswa
    \@for\reserved@a:=\reserved@b\do {%
      \ifx\reserved@a\@empty \else
        \ifcsname scrlfile@exclude@package@\reserved@a @do\endcsname
          \expandafter\let
          \csname scrlfile@exclude@package@\reserved@a @do\endcsname
          \undefined
        \fi
      \fi
    }%
  \fi
  \ifcsname scrlfile@excludedpackages\endcsname
    \let\reserved@a\scrlfile@excludedpackages
    \let\scrlfile@excludedpackages\@empty
    \expandafter\@for\expandafter\reserved@c
    \expandafter:\expandafter=\reserved@a
    \do{%
      \ifx\reserved@c\@empty\else
        \@expandtwoargs\in@{,\reserved@c,}{,\reserved@b,}%
        \ifin@
        \else
          \ifx\scrlfile@excludepackages\@empty
            \global\let\scrlfile@excludepackages\reserved@c
          \else
            \xdef\scrlfile@excludedpackages{%
              \scrlfile@excludedpackages,\reserved@c
            }%
          \fi
        \fi
      \fi
    }%
    \ifx\scrlfile@excludedpackages\@empty\ResetPreventPackageFromLoading\fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\scrlfile@RequirePackage}
% \changes{v3.08}{2010/11/04}{New (internal)}
% \changes{v3.12}{2013/11/22}{tolerate spaces}
% \changes{v3.12}{2013/11/22}{execute the optional argument of
%     \cs{PreventPackageFromLoading}}
% This macro will replace \cs{RequirePackage} whenever the exlude list is not
% empty
%    \begin{macrocode}
\newcommand*{\scrlfile@RequirePackage}[2][]{%
  \let\reserved@c\@empty
  \let\reserved@d\@empty
  \edef\reserved@b{\zap@space#2 \@empty}%
  \expandafter\@for\expandafter\reserved@b\expandafter:\expandafter=\reserved@b
  \do {%
    \begingroup
      \@tempswatrue
      \@for\reserved@a:=\scrlfile@excludedpackages\do {%
        \ifx\reserved@a\reserved@b
          \@tempswafalse
        \fi
      }%
      \if@tempswa
        \xdef\reserved@c{\reserved@c,\reserved@b}%
      \else
        \ifx\reserved@b\@empty\else
          \PackageInfo{scrlfile}{Excluding package `\reserved@b'\MessageBreak
            from loading}%
          \@ifundefined{scrlfile@exclude@package@\reserved@b @do}{%
          }{%
            \expandafter\g@addto@macro\expandafter\reserved@d
            \csname scrlfile@exclude@package@\reserved@b @do\endcsname
          }%
        \fi
      \fi
    \endgroup
  }%
  \ifx\reserved@c\@empty
    \let\reserved@a\scr@gobbleopt
  \else
    \edef\reserved@a{\noexpand\scrlfile@saved@RequirePackage[#1]{%
        \expandafter\@gobble\reserved@c\@empty}}%
  \fi
  \expandafter\reserved@d
  \reserved@a
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\scr@gobbleopt}
% \changes{v3.08}{2010/11/04}{New (internal)}
% \changes{v3.08a}{2011/02/11}{\cs{@gobbleopt} renamed due to compaibility
%   problems with \texttt{ntheorem}}
% Eat the optional argument if it exists.
%    \begin{macrocode}
\providecommand{\scr@gobbleopt}[1][]{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kernel@ifstar}
% \changes{v3.20}{2016/04/12}{New (internal)}
% I hope, the \LaTeX{} kernel will define this command in future. But
% nevertheless, there are \LaTeX{} kernels in real live without this
% command. So I do it again if needed:
%    \begin{macrocode}
\providecommand*\kernel@ifstar[1]{\kernel@ifnextchar*{\@firstoftwo{#1}}}
%    \end{macrocode}
% \end{macro}%^^A \kernel@ifstar
%
%
% \subsection{Extra Document Hooks}
%
% \begin{macro}{\BeforeClosingMainAux}
% \changes{v2.95}{2002/09/03}{new}
% \begin{macro}{\AfterReadingMainAux}
% \changes{v3.03}{2009/03/25}{new}
% These are completly different. First we have to establish a new hook
% at \cs{end\{document\}}, just after closing the group and after the
% final \cs{clearpage}.
% TODO: Handling/ignoring an optional argument to make them more compatible
% with \LaTeX's new \cs{AddToHook}. 
%    \begin{macrocode}
\AtEndDocument{%
  \let\scr@saved@checkend=\@checkend
  \renewcommand*{\@checkend}[1]{%
    \def\reserved@a{#1}\def\reserved@b{document}%
    \scr@saved@checkend{#1}%
    \ifx\reserved@a\reserved@b
      \let\scr@saved@clearpage=\clearpage
      \renewcommand*{\clearpage}{\scr@saved@clearpage
        \scr@hook@bcma
        \scr@restore@newpage
        \let\clearpage=\scr@saved@clearpage}%
    \fi%
    \let\scr@saved@dofilelist\@dofilelist
    \def\@dofilelist{%
      \scr@hook@acma
      \let\@dofilelist\scr@saved@dofilelist
      \scr@saved@dofilelist
    }%
  }%
}
%    \end{macrocode}
% \begin{macro}{\scr@saved@checkend}
% \changes{v2.95}{2002/09/03}{new (internal)}
% \begin{macro}{\scr@saved@dofilelist}
% \changes{v3.03}{2009/03/25}{new}
% You may see above, that we need save-to macros.
%    \begin{macrocode}
\newcommand*{\scr@saved@checkend}{}
\newcommand*{\scr@saved@dofilelist}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\scr@redefine@newpage}
% \changes{v3.00}{2008/07/09}{new (internal)}
%   Some classes (e.g. \textsf{letter}) write output to the aux file.
%   Because of this, redefinition of \cs{newpage} will only be done, if
%   \cs{BeforeClosingMainAux} is used.
% \begin{macro}{\scr@saved@newpage}
% \changes{v3.00}{2008/07/09}{new (internal)}
%   And \cs{newpage} will be restored after the hook.
% \begin{macro}{\scr@restore@newpage}
%    \begin{macrocode}
\newcommand*{\scr@redefine@newpage}{%
  \let\scr@saved@newpage\newpage
  \def\scr@restore@newpage{%
    \renewcommand*{\newpage}{%
      \PackageWarningNoLine{%
        scrlfile%
      }{%
        \string\newpage\space at main aux file!\MessageBreak
        You have output material at the main aux file.\MessageBreak
        Note, that the \string\BeforeClosingMainAux\space commands\MessageBreak
        are processed before the output from aux file%
      }%
      \let\newpage\scr@saved@newpage
      \newpage
    }%
  }%
  \renewcommand*{\newpage}{%
    \PackageError{%
      scrlfile%
    }{%
      \string\newpage\space at \string\BeforeClosingMainAux
    }{%
      You should never use \string\newpage\space or
      \string\clear(double)page or other output material at the\MessageBreak
      argument of \string\BeforeClosingMainAux!\MessageBreak
      If you'll continue, the \string\newpage\space will be ignored to
      avoid corruption of\MessageBreak
      every prior use of \string\BeforeClosingMainAux.
    }%
  }%
}
\newcommand*{\scr@restore@newpage}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\scr@hook@bcma}
% \changes{v2.95}{2002/09/03}{new (internal)}
% \begin{macro}{\scr@hook@acma}
% \changes{v3.03}{2009/03/25}{new (internal)}
% We also need macros, that saves the commands at the hooks and are
% extendable using \cs{g@addto@macro}.
%    \begin{macrocode}
\newcommand*{\scr@hook@bcma}{}
\newcommand*{\scr@hook@acma}{}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
% Now we are able to write the user command. Here we also warn, if we detect
% one more \cs{newpage}\,---\,maybe at a \cs{clearpage} or
% \cs{cleardoublepage}.
%    \begin{macrocode}
\newcommand*{\BeforeClosingMainAux}{%
  \ifx\scr@hook@bcma\@empty
    \gdef\scr@hook@bcma{%
      \renewcommand*{\BeforeClosingMainAux}[1]{####1}%
      \let\protected@write\protected@immediate@write
      \scr@redefine@newpage
    }%
  \fi
  \g@addto@macro\scr@hook@bcma%
}
\newcommand*{\AfterReadingMainAux}{%
  \ifx\scr@hook@acma\@empty
    \gdef\scr@hook@acma{%
      \renewcommand*{\AfterReadingMainAux}[1]{####1}%
      \let\protected@write\protected@immediate@write
    }%
  \fi
  \g@addto@macro\scr@hook@acma%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\protected@immediate@write}
% Like \LaTeX{} kernel's |\protected@write| but using |\immediate\write|. In
% this case it is even not a good idea to protect |\thepage|!
%    \begin{macrocode}
\long\def\protected@immediate@write#1#2#3{%
  \begingroup
    #2%
    \let\protect\@unexpandable@protect
    \edef\reserved@a{\immediate\write#1{#3}}%
    \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi
}
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \KOMAScript{} Logo}
%
% Last but not least this is a \KOMAScript{} package, so we also define the
% \KOMAScript{} logo:
%    \begin{macrocode}
\RequirePackage{scrlogo}
%    \end{macrocode}
%
% \Finale
%
% \endinput
% Local Variables:
% mode: doctex
% TeX-master: t
% End: