How Are LaTeX Class Files and Style Files Structured?
At a glance
- 1. Choose the file type
Use a .cls file to define a document class, or a .sty file to add reusable features to an existing class.
- 2. Add the declaration
Start with LaTeX format and file identity declarations: \NeedsTeXFormat and the matching \ProvidesClass or \ProvidesPackage.
- 3. Load dependencies
A class loads a base class with \LoadClass; either file can load packages with \RequirePackage.
- 4. Define features
Put commands, environments, defaults, and configuration in the file that owns those features.
- 5. Use and test it
Place the file beside your document, load the class with \documentclass or package with \usepackage, then compile.
A LaTeX class file and a style file can both define commands and load packages, but they have different roles. A class sets the overall document structure and defaults; a package adds a feature to a document that already has a class. This guide shows the usual file layout and a small example you can compile.
The difference between a class and a package
A class file has the extension .cls and is selected in the document preamble with \documentclass. Examples include article, report, and book. A class typically chooses a base class, sets page or heading defaults, and defines class-specific commands.
A style file—usually called a package—has the extension .sty and is loaded with \usepackage. Packages are for reusable features, such as graphics, colors, or commands for a particular subject. A document normally loads one class and can load several packages.
| File | Loaded with | Typical purpose |
|---|---|---|
myclass.cls | \documentclass{myclass} | Define a document type and its defaults |
mytools.sty | \usepackage{mytools} | Add reusable commands or features |
LaTeX finds these files in its search path. While developing, putting them in the same directory as the main .tex file is the simplest approach.
Standard file structure
A basic class file begins like this:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2025/03/08 Example document class]
\LoadClass{article}
% Class settings and definitions go here.
\endinput
\NeedsTeXFormat identifies the LaTeX format the file expects. \ProvidesClass declares the class name; it should match the file name, without .cls. \LoadClass loads the base class. A class can then set defaults or define commands and environments. \endinput marks the end of the file for TeX.
A basic package file follows the same pattern but declares itself as a package:
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mytools}[2025/03/08 Example package]
\RequirePackage{ifthen}
% Package settings and definitions go here.
\endinput
The name in \ProvidesPackage should match the file name, without .sty. Use \RequirePackage in a class or package to load dependencies. In a document preamble, use \usepackage instead. Avoid using \documentclass inside a class file or package file: that command belongs in the main document.
A complete working example
This example uses LaTeX’s filecontents* environment to create the two files before the document loads them. In ordinary projects, save each file’s contents separately as myclass.cls, mytools.sty, and demo.tex in the same directory.
\begin{filecontents*}{myclass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2025/03/08 Minimal example class]
\LoadClass[11pt]{article}
\RequirePackage{mytools}
\newcommand{\classgreeting}{This command is defined by the class.}
\endinput
\end{filecontents*}
\begin{filecontents*}{mytools.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mytools}[2025/03/08 Minimal example package]
\newcommand{\toolgreeting}{This command is defined by the package.}
\endinput
\end{filecontents*}
\documentclass{myclass}
\title{A Small Example}
\author{A. Writer}
\begin{document}
\maketitle
\classgreeting\par
\toolgreeting
\end{document}
The class loads article as its base and then requires the package. The main document only needs to select myclass; it can use commands provided by both files. Compile the example with a current LaTeX distribution. For separate files, the class and package must be saved before compiling demo.tex.
Class options and package options
A class can accept options and pass them to its base class. For example, this forwards any otherwise-unrecognized class option to article:
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}
Put the option declarations and \ProcessOptions before \LoadClass. Then a document using \documentclass[12pt]{myclass} passes 12pt to article. Packages can also declare and process options; use \DeclareOption, \ProcessOptions\relax, and, when needed, \PassOptionsToPackage.
For a first version, keep option handling out unless the file actually needs configurable behavior. Add it deliberately and check that every accepted option has the intended effect.
Common mistakes
- Mismatched names:
\ProvidesClass{myclass}belongs inmyclass.cls;\ProvidesPackage{mytools}belongs inmytools.sty. - Using the wrong loading command: select a class with
\documentclass, but load a package with\usepackageor\RequirePackagefrom another class or package. - Loading a base class from a package:
\LoadClassis for a class file. A package should extend the document’s existing class instead. - Using
\inputfor dependencies:\RequirePackageis the standard package-loading command. It provides package-aware loading and avoids accidentally loading a package more than once. - Editing a system class directly: copy or create a custom class with a distinct name rather than modifying a TeX distribution’s built-in files, which updates may replace.
- Leaving definitions untested: compile a tiny document that loads the file and exercises its commands. This makes missing dependencies and naming errors easier to locate.
As a rule, put document-wide structure and defaults in a .cls file, and reusable, independently loadable functionality in a .sty file. A class may require packages, and a package may define many commands, but each file should have one clear role.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.