| 1 |
% ====================================================================== |
| 2 |
% scrlfile-en.tex |
| 3 |
% Copyright (c) Markus Kohm, 2001-2025 |
| 4 |
% |
| 5 |
% This file is part of the LaTeX2e KOMA-Script bundle. |
| 6 |
% |
| 7 |
% This work may be distributed and/or modified under the conditions of |
| 8 |
% the LaTeX Project Public License, version 1.3c of the license. |
| 9 |
% The latest version of this license is in |
| 10 |
% http://www.latex-project.org/lppl.txt |
| 11 |
% and version 1.3c or later is part of all distributions of LaTeX |
| 12 |
% version 2005/12/01 or later and of this work. |
| 13 |
% |
| 14 |
% This work has the LPPL maintenance status "author-maintained". |
| 15 |
% |
| 16 |
% The Current Maintainer and author of this work is Markus Kohm. |
| 17 |
% |
| 18 |
% This work consists of all files listed in MANIFEST.md. |
| 19 |
% ====================================================================== |
| 20 |
% |
| 21 |
% Chapter about scrlfile of the KOMA-Script guide |
| 22 |
% Maintained by Markus Kohm |
| 23 |
% |
| 24 |
% ============================================================================ |
| 25 |
|
| 26 |
\KOMAProvidesFile{scrlfile-en.tex} |
| 27 |
[$Date: 2025-07-01 20:35:29 +0200 (Di, 01. Jul 2025) $ |
| 28 |
KOMA-Script guide (chapter: scrlfile)] |
| 29 |
\translator{Gernot Hassenpflug\and Markus Kohm\and Karl Hagen} |
| 30 |
|
| 31 |
\chapter{Controlling Package Dependencies with \Package{scrlfile}} |
| 32 |
\labelbase{scrlfile} |
| 33 |
\BeginIndexGroup |
| 34 |
\BeginIndex{Package}{scrlfile} |
| 35 |
|
| 36 |
The introduction of \LaTeXe{} in 1994 brought many changes in the handling of |
| 37 |
\LaTeX{} extensions. The package author today has a whole series of macros |
| 38 |
available to determine if another package or class has been loaded and whether |
| 39 |
specific options are being used. The package author can even load other |
| 40 |
packages or specify certain options in case the package is loaded later. This |
| 41 |
has led to the expectation that the order in which package are loaded would |
| 42 |
not be important. Sadly, this hope has not been fulfilled. |
| 43 |
|
| 44 |
\section{About Package Dependencies} |
| 45 |
\seclabel{dependency} |
| 46 |
%\begin{Explain} |
| 47 |
More and more often, different packages either newly define or redefine the |
| 48 |
same macro. In such a case, the order in which a package is loaded becomes |
| 49 |
very important. Sometimes, users find it very difficult to understand the |
| 50 |
resulting behaviour. Sometimes it is necessary to react in a specific way when |
| 51 |
another package is loaded. |
| 52 |
|
| 53 |
As a simple example, consider loading the \Package{longtable} package with a |
| 54 |
\KOMAScript{} document class. The \Package{longtable} package defines its own |
| 55 |
table captions. These are perfectly suited to the standard classes, but they |
| 56 |
do not match the default settings for \KOMAScript{} captions, nor do they |
| 57 |
react to the relevant configuration options. To solve this problem, the |
| 58 |
\Package{longtable} package commands which are responsible for the table |
| 59 |
captions need to be redefined. However, by the time the \Package{longtable} |
| 60 |
package is loaded, the \KOMAScript{} class has already been processed. |
| 61 |
|
| 62 |
Previously, the only way to solve this problem was to delay the redefinition |
| 63 |
until the beginning of the document using \Macro{AtBeginDocument}. However, if |
| 64 |
users want to change the relevant commands themselves, they should do so in |
| 65 |
the preamble of the document. But this is impossible because \KOMAScript{} |
| 66 |
will overwrite the users' definitions at \Macro{begin}\PParameter{document}. |
| 67 |
They would also need to perform the redefinition with \Macro{AtBeginDocument}. |
| 68 |
|
| 69 |
But \KOMAScript{} does not actually need to wait for |
| 70 |
\Macro{begin}\PParameter{document} to redefine the macros. It would be |
| 71 |
sufficient to postpone the redefinition until after the \Package{longtable} |
| 72 |
package has been loaded. Unfortunately, the \LaTeX{} kernel does not define |
| 73 |
necessary commands. The \Package{scrlfile} package provides a remedy for this |
| 74 |
problem. |
| 75 |
|
| 76 |
It is also conceivable that you would like to save the definition of a macro |
| 77 |
in a temporary macro before a package is loaded and restore it after the |
| 78 |
package has been loaded. The \Package{scrlfile} package allows this, too. |
| 79 |
|
| 80 |
The use of \Package{scrlfile} is not limited to package dependencies. |
| 81 |
Dependencies can also be considered for any other file. For example, you can |
| 82 |
ensure that loading the not unimportant file \File{french.ldf} automatically |
| 83 |
leads to a warning. |
| 84 |
|
| 85 |
Although the package is particularly of interest for package authors, there |
| 86 |
are also applications for normal \LaTeX{} users. Therefore, this chapter |
| 87 |
gives examples for both groups. |
| 88 |
%\end{Explain} |
| 89 |
|
| 90 |
\begin{Declaration} |
| 91 |
\Option{withdeprecated} |
| 92 |
\end{Declaration} |
| 93 |
With\ChangedAt{v3.32}{\Package{scrlfile}} \LaTeX{} 2020-10-01 a completely new |
| 94 |
hook mechanism is available. Instead of redefining macros of the \LaTeX{} |
| 95 |
kernel, \Package{scrlfile} 3.32 and newer makes use of it. For this purpose it |
| 96 |
loads the internal package |
| 97 |
\Package{scrfile-hook}\IndexPackage[indexmain]{scrlfile-hook}. With older |
| 98 |
\LaTeX{} versions, depending\ChangedAt{v3.35}{\Package{scrlfile}} on the |
| 99 |
kernel version, either the internal package |
| 100 |
\Package{scrlayer-hook-3.34}\IndexPackage[indexmain]{scrlfile-hook-3.34} or |
| 101 |
\Package{scrlayer-patcholdkernel}% |
| 102 |
\IndexPackage[indexmain]{scrlfile-patcholdkernel} is used. However, since it |
| 103 |
was not possible to achieve identical functionality with the new mechanism in |
| 104 |
\LaTeX, some commands of \Package{scrlfile} were marked as |
| 105 |
deprecated. Furthermore, the opportunity was taken to organize the |
| 106 |
proliferation of command variants. If a user uses a class or package that is |
| 107 |
still based on the commands that are no longer supported by |
| 108 |
\Package{scrlfile}, the package \Package{scrlfile} can be loaded with |
| 109 |
\begin{lstcode} |
| 110 |
\RequirePackage[withdeprecated]{scrlfile} |
| 111 |
\end{lstcode} |
| 112 |
before loading the class. \Package{scrlfile} then additionally emulates some |
| 113 |
of the deprecated commands in a way that should be sufficient in most |
| 114 |
cases. In any case, the author of the corresponding class or package should be |
| 115 |
informed, so that he or she can make an adaptation to the current version of |
| 116 |
\Package{scrlfile} or directly to \LaTeX{} from version 2021-11-15.% |
| 117 |
% |
| 118 |
\EndIndexGroup |
| 119 |
|
| 120 |
\section{Actions Before and After Loading} |
| 121 |
\seclabel{macros} |
| 122 |
|
| 123 |
The \Package{scrlfile} package can execute actions both before and after |
| 124 |
loading files. The commands used to do so distinguish between ordinary files, |
| 125 |
classes, and packages. |
| 126 |
|
| 127 |
\begin{Declaration} |
| 128 |
\Macro{BeforeFile}\Parameter{file}\OParameter{label}\Parameter{commands}% |
| 129 |
\Macro{AfterFile}\Parameter{file}\OParameter{label}\Parameter{commands} |
| 130 |
\end{Declaration}% |
| 131 |
\Macro{BeforeFile} ensures that the \PName{commands} are executed before the |
| 132 |
next time \PName{file} is loaded. \Macro{AfterFile} works in a similar |
| 133 |
fashion, and the \PName{commands} will be executed after the \PName{file} has |
| 134 |
been loaded. Of course, if \PName{file} is never loaded, the \PName{commands} |
| 135 |
will never be executed. For \PName{file}, you should specify any extensions as |
| 136 |
part of the file name, as you would with \Macro{input}. |
| 137 |
|
| 138 |
To implement those featuresfor \LaTeX{} before version 2020-10-01, |
| 139 |
\Package{scrlfile-patcholdlatex} redefines the well-known \LaTeX{} command |
| 140 |
\Macro{InputIfFileExists}. If this command does not have the expected |
| 141 |
definition, \Package{scrlfile-patcholdlatex} issues a warning. This occurs in |
| 142 |
case the command has already been redefined by an incompatible package. |
| 143 |
|
| 144 |
With\ChangedAt{v3.32}{\Package{scrlfile}}\IndexCmd{AddToHook} \LaTeX{} from |
| 145 |
version 2020-10-01 on, \Package{scrlfile-hook} uses |
| 146 |
\Macro{AddToHook}\PParameter{file/before/\PName{file}}\OParameter{label}\Parameter{commands} |
| 147 |
or |
| 148 |
\Macro{AddToHook}\PParameter{file/after/\PName{file}}\OParameter{label}\Parameter{commands}. |
| 149 |
For more information on the meaning of the optional argument \PName{label}, |
| 150 |
please refer to the corresponding manual of the \LaTeX{} core statement |
| 151 |
\Macro{AddToHook}. As a rule, the omission of the optional argument is |
| 152 |
recommended. With older \LaTeX{} versions this is ignored anyway. |
| 153 |
|
| 154 |
\LaTeX{} uses the \Macro{InputIfFileExists} command every time it loads a |
| 155 |
file. This occurs regardless of whether the file is loaded with |
| 156 |
\Macro{include}, \Macro{LoadClass}, \Macro{documentclass}, \Macro{usepackage}, |
| 157 |
\Macro{RequirePackage}, or similar commands. Only |
| 158 |
\begin{lstcode} |
| 159 |
\input foo |
| 160 |
\end{lstcode} |
| 161 |
loads the file \texttt{foo} without using |
| 162 |
\Macro{InputIfFileExists}. You should therefore always use |
| 163 |
\begin{lstcode} |
| 164 |
\input{foo} |
| 165 |
\end{lstcode} |
| 166 |
instead. Notice the parentheses surrounding the file name!% |
| 167 |
% |
| 168 |
\EndIndexGroup |
| 169 |
|
| 170 |
|
| 171 |
\begin{Declaration} |
| 172 |
\Macro{BeforeClass}\Parameter{class}\OParameter{label}\Parameter{commands}% |
| 173 |
\Macro{BeforePackage}\Parameter{package}\OParameter{label}\Parameter{commands} |
| 174 |
\end{Declaration}% |
| 175 |
These two commands work the same way as \DescRef{\LabelBase.cmd.BeforeFile}. |
| 176 |
The only difference is that the \PName{class} or \PName{package} is specified |
| 177 |
with its class or package name and not with its file name. That means you |
| 178 |
should omit the file extensions \File{.cls} or \File{.sty}. |
| 179 |
|
| 180 |
Note,\important{Attention!} \Package{scrlfile-hook} also uses \PValue{file} |
| 181 |
and not \PValue{class} or \PValue{package} for the hook. This is the only way |
| 182 |
to ensure that the execution of the \PName{commands} already takes place in |
| 183 |
the context of the class or package. For the meaning of the optional argument |
| 184 |
\PName{label} please refer to the \LaTeX{} core statement |
| 185 |
\Macro{AddToHook}\IndexCmd{AddToHook}. As a rule, the omission of the optional |
| 186 |
argument is recommended. With older \LaTeX{} versions this is ignored anyway.% |
| 187 |
\EndIndexGroup |
| 188 |
|
| 189 |
|
| 190 |
\begin{Declaration} |
| 191 |
\Macro{AfterClass}\Parameter{class}\OParameter{label}\Parameter{commands}% |
| 192 |
\Macro{AfterClass*}\Parameter{class}\OParameter{label}\Parameter{commands}% |
| 193 |
\Macro{AfterAtEndOfClass}\Parameter{class}\OParameter{label}\Parameter{commands}% |
| 194 |
\Macro{AfterAtEndOfClass*}\Parameter{class}\OParameter{label}\Parameter{commands}% |
| 195 |
\Macro{AfterPackage}\Parameter{package}\OParameter{label}\Parameter{commands}% |
| 196 |
\Macro{AfterPackage*}\Parameter{package}\OParameter{label}\Parameter{commands}% |
| 197 |
\Macro{AfterAtEndOfPackage}\Parameter{package}\OParameter{label}\Parameter{commands} |
| 198 |
\Macro{AfterAtEndOfPackage*}\Parameter{package}\OParameter{label}\Parameter{commands} |
| 199 |
\end{Declaration}% |
| 200 |
The \Macro{AfterClass} and \Macro{AfterPackage} commands work much like |
| 201 |
\DescRef{\LabelBase.cmd.AfterFile}. The only difference is that the |
| 202 |
\PName{class} or \PName{package} is specified with its class or package name |
| 203 |
and not with its file name. That means you should omit the file extensions |
| 204 |
\File{.cls} or \File{.sty}. |
| 205 |
|
| 206 |
The\important[i]{\Macro{AfterClass*}\\\Macro{AfterPackage*}} starred versions |
| 207 |
function somewhat differently. If the class or package has already been |
| 208 |
loaded, they execute the \PName{commands} immediately rather than waiting |
| 209 |
until the next time the class or package is loaded. |
| 210 |
|
| 211 |
However\ChangedAt{v3.09}{\Package{scrlfile}}% |
| 212 |
\important[i]{\Macro{AfterAtEndOfClass}\\\Macro{AfterAtEndOfPackage}}, code |
| 213 |
whose execution is delayed by \Macro{AtEndOfClass} or \Macro{AtEndOfPackage} |
| 214 |
within the class or package is first executed afterwards. In contrast, |
| 215 |
\Macro{AfterAtEndOfClass} or \Macro{AfterAtEndOfPackage} ensures that |
| 216 |
\PName{commands} are executed after such code. |
| 217 |
|
| 218 |
Again\ChangedAt{v3.32}{\Package{scrlfile}}% |
| 219 |
\important[i]{\Macro{AfterAtEndOfClass*}\\\Macro{AfterAtEndOfPackage*}}, there |
| 220 |
are star variants that ensure that \PName{commands} are executed immediately |
| 221 |
if the class or package is already completely loaded. If a class or package is |
| 222 |
currently loaded, the execution is delayed like in the version without star. |
| 223 |
|
| 224 |
Note that \Package{scrlfile-hook} uses a \PValue{file/after} hook for |
| 225 |
\Macro{AfterClass}, \Macro{AfterPackage} and their star variants. In contrast |
| 226 |
\Macro{AfterAtEndOfClass} and its star variant set a \PValue{class/after} hook |
| 227 |
and \Macro{AfterAtEndOfPackage} and its star variant set a |
| 228 |
\PValue{package/after} hook accordingly. For the meaning of the optional |
| 229 |
argument \PName{label} please refer to the \LaTeX{} core statement |
| 230 |
\Macro{AddToHook}\IndexCmd{AddToHook}. As a rule, the omission of the optional |
| 231 |
argument is recommended. With older \LaTeX{} versions this is ignored anyway. |
| 232 |
|
| 233 |
\begin{Example} |
| 234 |
The following example for class and package authors shows how \KOMAScript{} |
| 235 |
itself makes use of the new commands. The class \Class{scrbook} contains the |
| 236 |
following: |
| 237 |
\begin{lstcode} |
| 238 |
\AfterPackage{hyperref}{% |
| 239 |
\@ifpackagelater{hyperref}{2001/02/19}{}{% |
| 240 |
\ClassWarningNoLine{scrbook}{% |
| 241 |
You are using an old version of the hyperref |
| 242 |
package!\MessageBreak% |
| 243 |
This version has a buggy hack in many |
| 244 |
drivers,\MessageBreak% |
| 245 |
causing \string\addchap\space to behave |
| 246 |
strangely.\MessageBreak% |
| 247 |
Please update hyperref to at least version |
| 248 |
6.71b}}} |
| 249 |
\end{lstcode} |
| 250 |
Old versions of the \Package{hyperref} package redefine a macro of the |
| 251 |
\Class{scrbook} class in a way that is incompatible with newer \KOMAScript{} |
| 252 |
versions. Newer versions of \Package{hyperref} refrain from making this |
| 253 |
change if a newer version of \KOMAScript{} is detected. In case |
| 254 |
\Package{hyperref} is loaded at a later stage, \Class{scrbook} ensures that |
| 255 |
a check for an acceptable version of \Package{hyperref} is performed |
| 256 |
immediately after the package is loaded. If this is not the case, a warning |
| 257 |
is issued. |
| 258 |
|
| 259 |
There are also, however, instances where these commands are useful to normal |
| 260 |
\LaTeX{} users. For example, suppose you create a document from which you |
| 261 |
want to generate both a PostScript file, using \LaTeX{} and dvips, and a PDF |
| 262 |
file, using \mbox{pdf{\LaTeX}}. The document should also contain hyperlinks. |
| 263 |
In the table of contents, you have entries that span several lines. This is |
| 264 |
not a problem for the \mbox{pdf{\LaTeX}} method, since here hyperlinks can |
| 265 |
be broken across multiple lines. However, this is not possible with the |
| 266 |
\Package{hyperref} driver for dvips or \mbox{hyper{\TeX}}. In this case, you |
| 267 |
would like \Package{hyperref} to use the \Option{linktocpage} option. The |
| 268 |
decision as to which driver is loaded is made automatically by |
| 269 |
\File{hyperref}. |
| 270 |
|
| 271 |
Everything else can now be left to \DescRef{\LabelBase.cmd.AfterFile}: |
| 272 |
\begin{lstcode} |
| 273 |
\documentclass{article} |
| 274 |
\usepackage{scrlfile} |
| 275 |
\AfterFile{hdvips.def}{\hypersetup{linktocpage}} |
| 276 |
\AfterFile{hypertex.def}{\hypersetup{linktocpage}} |
| 277 |
\usepackage{hyperref} |
| 278 |
\begin{document} |
| 279 |
\listoffigures |
| 280 |
\clearpage |
| 281 |
\begin{figure} |
| 282 |
\caption{This is an example of a fairly long figure caption, but |
| 283 |
one that does not use the optional caption argument that would |
| 284 |
allow you to write a short caption in the list of figures.} |
| 285 |
\end{figure} |
| 286 |
\end{document} |
| 287 |
\end{lstcode} |
| 288 |
If either of the \Package{hyperref} drivers \Option{hypertex} or |
| 289 |
\Option{dvips} is used, the useful \Package{hyperref} option |
| 290 |
\Option{linktocpage} will be set. However, if you create a PDF file |
| 291 |
with \mbox{pdf{\LaTeX}}, the option |
| 292 |
will not be set because then the \Package{hyperref} driver |
| 293 |
\File{hpdftex.def} will be used. This means that neither \File{hdvips.def} |
| 294 |
nor \File{hypertex.def} will be loaded. |
| 295 |
\end{Example} |
| 296 |
\iffalse% Umbruchkorrekturtext (der besser nicht mehr verwendet wird!) |
| 297 |
Furthermore\textnote{Hint!}, you can also load \Package{scrlfile} and the |
| 298 |
\DescRef{\LabelBase.cmd.AfterFile} command into a private |
| 299 |
\File{hyperref.cfg}. In this case, however, you should use |
| 300 |
\Macro{RequirePackage} instead of \Macro{usepackage} to load the package |
| 301 |
(see \cite{latex:clsguide}). In the example above, the new lines have to be |
| 302 |
inserted directly after the \Macro{ProvidesFile} line, that is, immediately |
| 303 |
before the \Option{dvips} or \Option{pdftex} options are executed.% |
| 304 |
\fi |
| 305 |
Incidentally\textnote{Hint!}, you can also load \Package{scrlfile} before |
| 306 |
\DescRef{maincls.cmd.documentclass}\IndexCmd{documentclass}. In this case, |
| 307 |
however, you should use \Macro{RequirePackage}\IndexCmd{RequirePackage} |
| 308 |
instead of \DescRef{maincls.cmd.usepackage} (see \cite{latex:clsguide}).% |
| 309 |
\EndIndexGroup |
| 310 |
|
| 311 |
|
| 312 |
\begin{Declaration} |
| 313 |
\Macro{BeforeClosingMainAux}\Parameter{commands}% |
| 314 |
\Macro{AfterReadingMainAux}\Parameter{commands}% |
| 315 |
\end{Declaration}% |
| 316 |
These commands differ in one detail from the commands explained previously. |
| 317 |
Those commands enable actions before or after loading files. That is not the |
| 318 |
case here. Package authors often want to write something to the \File{aux} |
| 319 |
file after the last document page has been shipped out. To do so, ignoring the |
| 320 |
resulting problems they create, they often use code such as the following: |
| 321 |
\begin{lstcode} |
| 322 |
\AtEndDocument{% |
| 323 |
\if@filesw |
| 324 |
\write\@auxout{% |
| 325 |
\protect\writethistoaux% |
| 326 |
}% |
| 327 |
\fi |
| 328 |
} |
| 329 |
\end{lstcode} |
| 330 |
However, this does not really solve the problem. If the last page of the |
| 331 |
document has already been shipped out before \Macro{end}\PParameter{document}, |
| 332 |
the code above will not result in any output to the \File{aux} file. If you |
| 333 |
try to fix this new problem using \Macro{immediate} just before \Macro{write}, |
| 334 |
you would have the opposite problem: if the last page has not yet been shipped |
| 335 |
out before \Macro{end}\PParameter{document}, \Macro{writethistoaux} would be |
| 336 |
written to the \File{aux} file too early. Therefore you often see solutions |
| 337 |
like: |
| 338 |
\begin{lstcode} |
| 339 |
\AtEndDocument{% |
| 340 |
\if@filesw |
| 341 |
\clearpage |
| 342 |
\immediate\write\@auxout{% |
| 343 |
\protect\writethistoaux% |
| 344 |
}% |
| 345 |
\fi |
| 346 |
} |
| 347 |
\end{lstcode} |
| 348 |
However, this solution has the disadvantage that it forces the last page to be |
| 349 |
shipped out. A command such as |
| 350 |
\begin{lstcode} |
| 351 |
\AtEndDocument{% |
| 352 |
\par\vspace*{\fill}% |
| 353 |
Note at the end of the document.\par |
| 354 |
} |
| 355 |
\end{lstcode} |
| 356 |
will no longer cause the note to appear beneath the text of the last real page |
| 357 |
of the document but at the end of one additional page. Furthermore, |
| 358 |
\Macro{writethistoaux} will again be written to the \File{aux} file one page |
| 359 |
too early. |
| 360 |
|
| 361 |
The best solution for this problem would be if you could write directly to the |
| 362 |
\File{aux} file immediately after the final \DescRef{maincls.cmd.clearpage} |
| 363 |
that is part of \Macro{end}\PParameter{document} but before closing the |
| 364 |
\File{aux} file. This is the purpose of \Macro{BeforeClosingMainAux}: |
| 365 |
\begin{lstcode} |
| 366 |
\BeforeClosingMainAux{% |
| 367 |
\if@filesw |
| 368 |
\immediate\write\@auxout{% |
| 369 |
\protect\writethistoaux% |
| 370 |
}% |
| 371 |
\fi |
| 372 |
} |
| 373 |
\end{lstcode} |
| 374 |
This will be successful even if the final \DescRef{maincls.cmd.clearpage} |
| 375 |
inside of \Macro{end}\PParameter{document} does not actually ship out any page |
| 376 |
or if \DescRef{maincls.cmd.clearpage} is used within an \Macro{AtEndDocument} |
| 377 |
command. |
| 378 |
|
| 379 |
However, there one important limitation using \Macro{BeforeClosingMainAux}: |
| 380 |
you should not use any typesetting commands inside the \PName{commands} of |
| 381 |
\Macro{BeforeClosingMainAux}! If you ignore this restriction, the result is |
| 382 |
just as unpredictable as the results of the problematic suggestions above that |
| 383 |
use \Macro{AtEndDocument}. |
| 384 |
|
| 385 |
The \Macro{AfterReadingMainAux}\ChangedAt{v3.03}{\Package{scrlfile}} command |
| 386 |
actually executes the \PName{commands} after closing and reading the |
| 387 |
\File{aux} file inside \Macro{end}\PParameter{document}. This is only useful |
| 388 |
in a few very rare cases, for example to write statistical information to the |
| 389 |
\File{log} file which is valid only after reading the \File{aux} file, or to |
| 390 |
implement additional \emph{rerun} requests. Typesetting commands are even more |
| 391 |
dangerous inside these \PName{commands} than inside the argument of |
| 392 |
\Macro{BeforeClosingMainAux}.% |
| 393 |
% |
| 394 |
\EndIndexGroup |
| 395 |
|
| 396 |
|
| 397 |
\section{Replacing Files at Input} |
| 398 |
\seclabel{replace} |
| 399 |
|
| 400 |
The previous sections in this chapter have explained commands to perform |
| 401 |
actions before or after loading a particular file, package, or class. You can |
| 402 |
also use \Package{scrlfile} to input a completely different file than the one |
| 403 |
that was requested. |
| 404 |
|
| 405 |
\begin{Declaration} |
| 406 |
\Macro{ReplaceInput}\Parameter{original file}% |
| 407 |
\Parameter{replacement file}% |
| 408 |
\end{Declaration}% |
| 409 |
This command\ChangedAt{v2.96}{\Package{scrlfile}} defines a replacement |
| 410 |
for the file specified in the first argument, \PName{original file}. If |
| 411 |
\LaTeX{} is instructed to load this file, the \PName{replacement file} will be |
| 412 |
loaded instead. The replacement-file definition affects all files loaded using |
| 413 |
\Macro{InputIfFileExists}, whether they are loaded by the user or internally |
| 414 |
by \LaTeX{}. To do so, \Package{scrlfile} redefines \Macro{InputIfFileExists}. |
| 415 |
|
| 416 |
When\ChangedAt{v3.32}{\Package{scrlfile}} using a \LaTeX{} version up to |
| 417 |
2020-04-01, the package \Package{scrlfile-patcholdlatex} must redefine the |
| 418 |
\Macro{InputIfFileExists} statement for this purpose. When using \LaTeX{} from |
| 419 |
version 2020-10-01 on, \Package{scrlfile-hook} uses the internal \LaTeX{} |
| 420 |
statement \Macro{declare@file@substitution}. The \LaTeX{} team asks to make |
| 421 |
such a substitution only if there is no other way to achieve the desired |
| 422 |
result, for example if such a substitution is indispensable to maintain |
| 423 |
compatibility and if the \PName{replacement file} provides the same |
| 424 |
functionality. |
| 425 |
|
| 426 |
\begin{Example} |
| 427 |
You want to input the \File{\Macro{jobname}.tol} file instead of the |
| 428 |
\File{\Macro{jobname.lot}} file. To do this, you use |
| 429 |
\begin{lstcode} |
| 430 |
\ReplaceInput{\jobname.tol}{\jobname.lot} |
| 431 |
\end{lstcode} |
| 432 |
If additionally you replace \File{\Macro{jobname}.tol} by |
| 433 |
\File{\Macro{jobname}.tlo} using |
| 434 |
\begin{lstcode} |
| 435 |
\ReplaceInput{\jobname.tol}{\jobname.tlo} |
| 436 |
\end{lstcode} |
| 437 |
then \File{\Macro{jobname}.tol} will also be replaced by |
| 438 |
\File{\Macro{jobname}.tlo}. In this way, you can process the whole |
| 439 |
replacement chain. |
| 440 |
|
| 441 |
However, a replacement that results in a loop such as |
| 442 |
\begin{lstcode} |
| 443 |
\ReplaceInput{\jobname.lot}{\jobname.tol} |
| 444 |
\ReplaceInput{\jobname.tol}{\jobname.lot} |
| 445 |
\end{lstcode} |
| 446 |
will cause an error. So it is not possible to replace |
| 447 |
a file that has already been replaced once by itself again. |
| 448 |
\end{Example} |
| 449 |
|
| 450 |
In theory, it would also be possible to use this command to replace one |
| 451 |
package or class with another .However, it is recommended to use the following |
| 452 |
instructions for replacing a package or class. For older \LaTeX{} versions |
| 453 |
this is even mandatory.% |
| 454 |
\EndIndexGroup |
| 455 |
|
| 456 |
|
| 457 |
\begin{Declaration} |
| 458 |
\Macro{ReplaceClass}\Parameter{original class}% |
| 459 |
\Parameter{replacement class}% |
| 460 |
\Macro{ReplacePackage}\Parameter{original package}% |
| 461 |
\Parameter{replacement package}% |
| 462 |
\end{Declaration}% |
| 463 |
You\ChangedAt{v2.96}{\Package{scrlfile}}\textnote{Attention!} should never |
| 464 |
replace a class or package using the \DescRef{\LabelBase.cmd.ReplaceInput} |
| 465 |
command described above. Instead, \Macro{ReplacePackage} should be used for |
| 466 |
package replacements and \Macro{ReplaceClass} for class replacements. Note |
| 467 |
that in this case, as with \Macro{documentclass} and \Macro{usepackage}, you |
| 468 |
should give the name of the class or package and not the complete file name. |
| 469 |
|
| 470 |
The replacement class works for all classes loaded with |
| 471 |
\Macro{documentclass}, \Macro{LoadClassWithOptions}, or \Macro{LoadClass}. The |
| 472 |
replacement package works for all packages loaded with |
| 473 |
\Macro{usepackage}, \Macro{RequirePackageWithOptions}, or |
| 474 |
\Macro{RequirePackage}. |
| 475 |
|
| 476 |
Please\textnote{Attention!} note that the \PName{replacement class} or the |
| 477 |
\PName{replacement package} will be loaded with the same options that would |
| 478 |
have been passed to the \PName{original class} or \PName{original package}. If |
| 479 |
you replace a class or package with one that does not support a requested |
| 480 |
option, you will receive the usual warnings and errors. |
| 481 |
|
| 482 |
Of course, to replace classes, you must load \Package{scrlfile} before the |
| 483 |
class using \Macro{RequirePackage} instead of \Macro{usepackage}. |
| 484 |
|
| 485 |
When\ChangedAt{v3.32}{\Package{scrlfile}} using a \LaTeX{} version up to |
| 486 |
2020-04-01 the package \Package{scrlfile-patcholdlatex} must redefine the |
| 487 |
internal statements \Macro{@onefilewithoptions} and |
| 488 |
\Macro{@loadwithoptions}. When using \LaTeX{} from version 2020-10-01 on, |
| 489 |
\Package{scrlfile-hook} uses the internal \LaTeX{} statement |
| 490 |
\Macro{declare@file@substitution}. The \LaTeX{} Team asks to perform such a |
| 491 |
substitution only if there is no other way to achieve the desired result, |
| 492 |
e.g. if such a substitution is indispensable to maintain compatibility and the |
| 493 |
\PName{replacement package} or \PName{replacement class} provides the same |
| 494 |
functionality.% |
| 495 |
\EndIndexGroup |
| 496 |
|
| 497 |
|
| 498 |
\begin{Declaration} |
| 499 |
\Macro{UnReplaceInput}\Parameter{file name}% |
| 500 |
\Macro{UnReplacePackage}\Parameter{package}% |
| 501 |
\Macro{UnReplaceClass}\Parameter{class}% |
| 502 |
\end{Declaration} |
| 503 |
You\ChangedAt{v3.12}{\Package{scrlfile}} can also remove a replacement. You |
| 504 |
should remove the replacement definition for an input file using |
| 505 |
\Macro{UnReplaceInput}, for a package using \Macro{UnReplacePackage}, and for |
| 506 |
a class using \Macro{UnReplaceClass}.% |
| 507 |
\EndIndexGroup |
| 508 |
|
| 509 |
|
| 510 |
\section{Preventing File Loading} |
| 511 |
\seclabel{prevent} |
| 512 |
|
| 513 |
Classes\ChangedAt{v3.08}{\Package{scrlfile}} and packages written for use |
| 514 |
within companies or academic institutions often load many packages only |
| 515 |
because users need them frequently, not because they are required by the class |
| 516 |
or package itself. If a problem occurs with one of these automatically loaded |
| 517 |
packages, you somehow have to keep the problematic package from being loaded. |
| 518 |
Once again, \Package{scrlfile} provides a simple solution. |
| 519 |
|
| 520 |
\begin{Declaration} |
| 521 |
\Macro{PreventPackageFromLoading}\OParameter{alternate code}% |
| 522 |
\Parameter{package list}% |
| 523 |
\Macro{PreventPackageFromLoading*}\OParameter{alternate code}% |
| 524 |
\Parameter{package list} |
| 525 |
\end{Declaration} |
| 526 |
Calling this command\ChangedAt{v3.08}{\Package{scrlfile}} before loading a |
| 527 |
package with \Macro{usepackage}\IndexCmd{usepackage}, |
| 528 |
\Macro{RequirePackage}\IndexCmd{RequirePackage}, or |
| 529 |
\Macro{RequirePackageWithOptions}\IndexCmd{RequirePackageWithOptions} |
| 530 |
effectively prevents the package from being loaded if it is found in the the |
| 531 |
\PName{package list}. |
| 532 |
% |
| 533 |
\begin{Example} |
| 534 |
Suppose you work at a company where all documents are created using Latin |
| 535 |
Modern. The company class, \Class{compycls}, therefore contains these lines: |
| 536 |
\begin{lstcode} |
| 537 |
\RequirePackage[T1]{fontenc} |
| 538 |
\RequirePackage{lmodern} |
| 539 |
\end{lstcode} |
| 540 |
But now, for the first time, you want to use \XeLaTeX{} or \LuaLaTeX{}. |
| 541 |
Since the recommended \Package{fontspec} package uses Latin Modern as the |
| 542 |
default font anyway, and loading \Package{fontenc} would not be a good idea, |
| 543 |
you want to prevent both packages from being loaded. Therefore, you load the |
| 544 |
class in your own document as follows: |
| 545 |
\begin{lstcode} |
| 546 |
\RequirePackage{scrlfile} |
| 547 |
\PreventPackageFromLoading{fontenc,lmodern} |
| 548 |
\documentclass{firmenci} |
| 549 |
\end{lstcode} |
| 550 |
\end{Example} |
| 551 |
The example above also shows that you can load \Package{scrlfile} before the |
| 552 |
class. In this case, you must use |
| 553 |
\Macro{RequirePackage}\IndexCmd{RequirePackage} because \Macro{usepackage} |
| 554 |
before \Macro{documentclass} is not permitted. |
| 555 |
|
| 556 |
If you specify an empty \PName{package list} or if it contains a package that |
| 557 |
has already been loaded, \Macro{PreventPackageFromLoading} issues a warning, |
| 558 |
while\ChangedAt{v3.12}{\Package{scrlfile}} \Macro{PreventPackageFromLoading*} |
| 559 |
merely writes a note to the log file only. |
| 560 |
|
| 561 |
You\ChangedAt{v3.12}{\Package{scrlfile}} can use the optional argument to |
| 562 |
execute code instead of loading the package. But you cannot load any other |
| 563 |
packages or files inside \PName{alternate code}. To load another package, see |
| 564 |
\DescRef{\LabelBase.cmd.ReplacePackage} in \autoref{sec:scrlfile.macros} on |
| 565 |
\DescPageRef{\LabelBase.cmd.ReplacePackage}. Note also that the |
| 566 |
\PName{alternate code} will be executed several times if you try to load the |
| 567 |
package more than once!% |
| 568 |
\EndIndexGroup |
| 569 |
|
| 570 |
|
| 571 |
\begin{Declaration} |
| 572 |
\Macro{StorePreventPackageFromLoading}\Parameter{\textMacro{command}}% |
| 573 |
\Macro{ResetPreventPackageFromLoading} |
| 574 |
\end{Declaration} |
| 575 |
\Macro{StorePreventPackageFromLoad}\ChangedAt{v3.08}{\Package{scrlfile}} |
| 576 |
defines \Macro{command} to be the current list of packages that should not be |
| 577 |
loaded. In contrast, |
| 578 |
\Macro{ResetPreventPackageFromLoading}\ChangedAt{v3.08}{\Package{scrlfile}} |
| 579 |
resets the list of packages that should not be loaded. After |
| 580 |
\Macro{ResetPreventPackageFromLoading}, you can load all packages again. |
| 581 |
\begin{Example} |
| 582 |
Suppose you need to load a package inside another package and you do not |
| 583 |
want the user to be able to prevent that package from being loaded with |
| 584 |
\DescRef{\LabelBase.cmd.PreventPackageFromLoading}% |
| 585 |
\IndexCmd{PreventPackageFromLoading}. So you reset the do-not-load list |
| 586 |
before you load this package: |
| 587 |
\begin{lstcode} |
| 588 |
\ResetPreventPackageFromLoading |
| 589 |
\RequirePackage{foo} |
| 590 |
\end{lstcode} |
| 591 |
Unfortunately, from this point on the user's entire do-not-load list would |
| 592 |
be lost. To avoid this, you first store the list and then restore it later: |
| 593 |
\begin{lstcode} |
| 594 |
\newcommand*{\Users@PreventList}{}% |
| 595 |
\StorePreventPackageFromLoading\Users@PreventList |
| 596 |
\ResetPreventPackageFromLoading |
| 597 |
\RequirePackage{foo} |
| 598 |
\PreventPackageFromLoading{\Users@PreventList} |
| 599 |
\end{lstcode} |
| 600 |
Note\textnote{Attention!} that \Macro{StorePreventPackageFromLoading} |
| 601 |
defines the \Macro{Users@PreventList} macro even if it has already been |
| 602 |
defined. In other words, calling \Macro{StorePreventPackageFromLoading} |
| 603 |
overwrites existing \Macro{command} definitions without checking. Therefore |
| 604 |
this example uses \Macro{newcommand*} to get an error message if |
| 605 |
\Macro{Users@PreventList} has already been defined. |
| 606 |
\end{Example} |
| 607 |
Note that when you manipulate the list stored by |
| 608 |
\Macro{StorePreventPackageFromLoading}, you are responsible for making sure it |
| 609 |
can be restored. For example, the list elements must be separated by comma, |
| 610 |
must not contain white space or group braces, and must be fully expandable. |
| 611 |
|
| 612 |
Also note\textnote{Attention!}, that \Macro{ResetPreventPackageFromLoading} |
| 613 |
does not clear the \PName{alternate code} for a package. But this code |
| 614 |
will not be executed so long as the package is not added again to the |
| 615 |
do-not-load list.% |
| 616 |
\EndIndexGroup |
| 617 |
|
| 618 |
|
| 619 |
\begin{Declaration} |
| 620 |
\Macro{UnPreventPackageFromLoading}\Parameter{package list}% |
| 621 |
\Macro{UnPreventPackageFromLoading*}\Parameter{package list} |
| 622 |
\end{Declaration} |
| 623 |
Instead\ChangedAt{v3.12}{\Package{scrlfile}} of completely resetting the list |
| 624 |
of packages that should not be loaded, you can also specify individual |
| 625 |
packages to remove from the list. The starred version of the command also |
| 626 |
deletes the \PName{alternate code}. For example, restoring packages to the |
| 627 |
do-not-load list from a stored list will not reactivate their \PName{alternate |
| 628 |
code} in this case.% |
| 629 |
% |
| 630 |
\begin{Example} |
| 631 |
Suppose you want to prevent a \Package{foo} package from being loaded, but |
| 632 |
you do not want to execute any outdated \PName{alternate code} that may |
| 633 |
exist. Instead, only your new \PName{alternate code} should be executed. You |
| 634 |
can do this as follows: |
| 635 |
\begin{lstcode} |
| 636 |
\UnPreventPackageFromLoading*{foo} |
| 637 |
\PreventPackageFromLoading[\typeout{alternate code}]{foo} |
| 638 |
\end{lstcode} |
| 639 |
For the \Macro{UnPreventPackageFromLoading} command, it does not matter |
| 640 |
whether or not the package has been prevented from being loaded before. |
| 641 |
|
| 642 |
Of course you can also use the command to indirectly delete the |
| 643 |
\PName{alternate code} of all packages: |
| 644 |
\begin{lstcode} |
| 645 |
\StorePreventPackageFromLoading\TheWholePreventList |
| 646 |
\UnPreventPackageFromLoading*{\TheWholePreventList} |
| 647 |
\PreventPackageFromLoading{\TheWholePreventList} |
| 648 |
\end{lstcode} |
| 649 |
In this case the packages that have been prevented from being loaded are |
| 650 |
still prevented from being loaded, but their \PName{alternate code} has been |
| 651 |
deleted and will no longer be executed.% |
| 652 |
\end{Example}% |
| 653 |
\EndIndexGroup |
| 654 |
% |
| 655 |
\EndIndexGroup |
| 656 |
|
| 657 |
\endinput |
| 658 |
|
| 659 |
%%% Local Variables: |
| 660 |
%%% mode: latex |
| 661 |
%%% TeX-master: "scrguide-en.tex" |
| 662 |
%%% coding: utf-8 |
| 663 |
%%% ispell-local-dictionary: "en_GB" |
| 664 |
%%% eval: (flyspell-mode 1) |
| 665 |
%%% End: |