Learning to write LaTeX classes and packages

There's nothing particularly magic about the commands you use when writing a package, so you can simply bundle up a set of LaTeX \(re)newcommand and \(re)newenvironment commands, put them in a file package.sty and you have a package.

However, any but the most trivial package will require rather more sophistication. Some details of LaTeX commands for the job are to be found in 'LaTeX2e for class and package writers' (http://www.tex.ac.uk/tex-archive/macros/latex/doc/html/clsguide: the LaTeX source of this document appears in the LaTeX distribution). Beyond this, a good knowledge of TeX itself is valuable. With such knowledge it is possible to use the documented source of LaTeX as reference material (dedicated authors will acquaint themselves with the source as a matter of course). A complete set of the documented source of LaTeX may be prepared by processing the file source2e.tex in the LaTeX distribution.

Writing good classes is not easy; it's a good idea to read some established ones (classes.dtx, for example, produces the standard classes other than letter, and may itself be formatted with LaTeX). Classes that are not part of the distribution are commonly based on ones that are, and start by loading the standard class with \LoadClass - an example of this technique may be seen in ltxguide.cls

classes.dtx
macros/latex/base/classes.dtx
ltxguide.cls
macros/latex/base/ltxguide.cls
source2e.tex
macros/latex/base/source2e.tex