Next: , Previous: , Up: Environments   [Contents][Index]

8.11 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:


Overwrite an existing file.


Omit the header. Equivalent to using filecontents*.


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:

\newcommand{\myname}{Jim Hef{}feron}
Article by \myname.

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