LaTeX after 2019-10-01
The LaTeX kernel has extended its filecontents environment to support an optional argument, see LaTeX News, Issue 30, LaTeX releas 2019-10-01.
If the option force or its alias overwrite is specified, the environment follows the behavior of package filecontents and allows that the file is overwritten.
Example (without package filecontents):
\begin{filecontents}[force]{exampleclass.cls}
...
\end{filecontents}
LaTeX before 2019-10-01
Package filecontents allows the overwriting of files:
\RequirePackage{filecontents}
\begin{filecontents}{exampleclass.cls}
...
\end{filecontents}
LaTeX kernel
The original environment filecontents from the LaTeX kernel is documented in source2e:
The environment filecontents is intended for passing the contents of
pack ages, options, or other files along with a document
in a single file. It has one argument, which is the name
of the file to create. If that file already exists maybe only in the
current directory if the OS supports a notion of a "current directory"
or "default directory") then nothing happens (except for an
information message) and the body of the environment is bypassed.
Otherwise, the body of the environment is written verbatim to the file
name given as the first argument, together with some comments about
how it was produced.
The environment is allowed only before \documentclass to ensure that
all packages or options necessary for this particular run are present
when needed. The begin and end tags should each be on a line by
itself. There is also a star-form; this does not write extra comments
into the file.
Package
Package filecontents lifts the restrictions:
- It allows the overwriting.
- The place for the environment is not restricted to before
\documentclass. It can also be placed in the preamble or after \begin{document}.