8.11 filecontents

Synopsis:

\begin{filecontents}[option]{filename}
  text
\end{filecontents}

or

\begin{filecontents*}[option]{filename}
  text
\end{filecontents*}

Create a file named filename in the current directory (or the output directory, if specified; see output directory) and write text to it. By default, an existing file is not overwritten.

The unstarred version of the environment filecontents prefixes the content of the created file with a header of TeX comments; see the example below. The starred version filecontents* does not include the header.

The possible options are:

force
overwrite

Overwrite an existing file.

noheader

Omit the header. Equivalent to using filecontents*.

nosearch

Only check the current directory (and the output directory, if specified) for an existing file, not the entire search path.

These options were added in a 2019 release of LaTeX.

This environment can be used anywhere in the preamble, although it often appears before the \documentclass command. It is commonly used to create a .bib or other such data file from the main document source, to make the source file self-contained. Similarly, it can be used to create a custom style or class file, again making the source self-contained.

For example, this document:

\documentclass{article}
\begin{filecontents}{JH.sty}
\newcommand{\myname}{Jim Hef{}feron}
\end{filecontents}
\usepackage{JH}
\begin{document}
Article by \myname.
\end{document}

produces this file JH.sty:

%% LaTeX2e file `JH.sty'
%% generated by the `filecontents' environment
%% from source `test' on 2015/10/12.
%%
\newcommand{\myname}{Jim Hef{}feron}

Unofficial LaTeX2e reference manual