Skip to main content
The filecontents environment writes an external file—such as a .bib bibliography, a .tex snippet, or a CSV data file—directly from your main .tex source during compilation. Since the October 2019 LaTeX release, filecontents and filecontents* are built into the LaTeX kernel and work anywhere in the document, so you no longer need \usepackage{filecontents}.

Write a file and read it back

Put the target filename in the mandatory argument of filecontents or filecontents*, and write the file’s exact contents inside the environment. TeX writes the lines verbatim without expanding macros, so you can immediately load the file later in the same compile with \input, \verbatiminput, or a data package.
write-and-input.tex
Ready to use this syntax? Continue in the browser editor when you want to adapt the example in a real project.Open LaTeX Cloud Studio
If you load \usepackage{filecontents} on a modern TeX distribution (including TeX Live 2026), the package disables itself and emits Package filecontents Warning: This package is obsolete. Disabling it and passing control to the filecontents environment defined by the LaTeX kernel. Remove \usepackage{filecontents} and use the kernel environment directly.

Optional arguments and filecontents*

By default, filecontents refuses to overwrite an existing file anywhere on the TeX search path and prepends a four-line %% LaTeX2e file ... comment header. You can change that behavior with a comma-separated optional argument list or the starred environment: Without [overwrite], if the target file already exists, LaTeX leaves it untouched and records LaTeX Info: File `...' already exists on the system. Not generating it from this source. (or already in current directory when [nosearch] is given). Attempting to overwrite the main source file itself (\jobname.tex) stops compilation with ! LaTeX Error: Trying to overwrite `<jobname>.tex'.. The compiled output below writes the same text twice—first with filecontents and then with filecontents*—and displays both files with \verbatiminput from the verbatim package:
header-vs-star.tex

Embedding data tables and self-contained .bib files

Use filecontents* (or [noheader]) whenever the generated file is read by a parser that does not treat % as a comment—such as CSV files, JSON, or BibTeX .bib databases. Because the October 2019 kernel update lifted the old restriction that filecontents could only appear before \documentclass, you can place the environment in the preamble or body right next to the code that uses it.
embedded-data-table.tex
The most common real-world use of filecontents* is a self-contained minimal working example (MWE) that embeds a small references.bib inside one .tex file before \addbibresource{references.bib} (for BibLaTeX) or \bibliography{references} (for BibTeX). For complete multi-file bibliography setups, see Bibliography management with BibTeX and BibLaTeX guide. Keep two formatting rules in mind when editing filecontents:
  • Put \end{filecontents} or \end{filecontents*} on its own line with no trailing text after it.
  • Add [overwrite] during development if you edit the embedded content; otherwise the file created on the first run will remain stale on later compiles.

Try it in the editor

LaTeX Cloud Studio runs TeX Live 2026 (full distribution), which includes the built-in kernel filecontents environment as well as verbatim, booktabs, and csvsimple. Start from the Basic Article template in LaTeX Cloud Studio and replace main.tex with the complete example.