Package & class authoring

If you keep pasting the same macros into your preamble, factor them into your own package (a .sty file you \usepackage) or class (a .cls). This page shows the skeleton — the identification line, option handling, loading dependencies — and how packages and classes differ.

The .sty skeleton

A package is a .sty file in your project (or installed). It opens by declaring what it is, then defines macros. \NeedsTeXFormat{LaTeX2e} states the required format, and \ProvidesPackage{name}[date version description] identifies the package and its version (the name must match the file’s basename; the date shows in the log and enables version checks). After that you define the content with \newcommand, \newenvironment, and so on.

latex
% mypackage.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[2026/01/01 v1.0 My helpers]
\newcommand{\hello}{こんにちは、\LaTeX!}

Load dependencies — \RequirePackage

When your .sty depends on other packages, use \RequirePackage{...} — the .sty equivalent of \usepackage. Load xcolor for color, tikz for drawing, and so on. Put it before \ProcessOptions if your option handling needs it, otherwise after.

latex
\RequirePackage{xcolor}

Accept options — \DeclareOption

To accept options like \usepackage[option]{name}, declare each with \DeclareOption{option}{code}, catch unknown ones with \DeclareOption*{...} (warn, or pass them to a loaded class), and finish with \ProcessOptions\relax. For key=value options, use the modern \DeclareKeys (l3keys) or the kvoptions package.

latex
\DeclareOption{color}{\renewcommand\hello{\textcolor{red}{こんにちは}}}
\DeclareOption*{\PackageWarning{mypackage}{Unknown option '\CurrentOption'}}
\ProcessOptions\relax

Classes vs packages

A class is a .cls file (declared with \ProvidesClass) that defines the whole document type. You load it with \documentclass, and only one per document. It usually builds on an existing class via \LoadClass{article}. A package (.sty), by contrast, adds features on top, and you can load as many as you like. The identification and option-handling mechanics are nearly identical. Building a class itself is covered on its own page.