Class and package structure

A class file or package file typically has four parts.

  1. In the identification part, the file says that it is a LaTeX package or class and describes itself, using the \NeedsTeXFormat and \ProvidesClass or \ProvidesPackage commands.
  2. The preliminary declarations part declares some commands and can also load other files. Usually these commands will be those needed for the code used in the next part. For example, an smcmemo class might be called with an option to read in a file with a list of people for the to-head, as \documentclass[mathto]{smcmemo}, and therefore needs to define a command \newcommand{\setto}[1]{\def\@tolist{#1}} used in that file.
  3. In the handle options part the class or package declares and processes its options. Class options allow a user to start their document as \documentclass[option list]{class name}, to modify the behavior of the class. An example is when you declare \documentclass[11pt]{article} to set the default document font size.
  4. Finally, in the more declarations part the class or package usually does most of its work: declaring new variables, commands and fonts, and loading other files.

Here is a starting class file, which should be saved as stub.cls where LaTeX can find it, for example in the same directory as the .tex file.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{stub}[2017/07/06 stub to start building classes from]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}

It identifies itself, handles the class options via the default of passing them all to the article class, and then loads the article class to provide the basis for this class’s code.

For more, see the official guide for class and package writers, the Class Guide, at https://www.latex-project.org/help/documentation/clsguide.pdf (much of the descriptions here derive from this document), or the tutorial https://www.tug.org/TUGboat/tb26-3/tb84heff.pdf.

© 2007–2018 Karl Berry
Public Domain Software
http://latexref.xyz/Class-and-package-structure.html