LaTeX/Creating Packages

If you define a lot of new environments and commands, the preamble of your document will get quite long. In this situation, it is a good idea to create a LaTeX package or class containing all your command and environment definitions. It can be made dynamic enough to fit to all your future documents.

Classes are .cls files; packages are stored in .sty files. They are very similar, the main difference being that you can load only one class per document.

After deciding to create your own package or class, you should think about which license the package/class has. A license is of great importance, either to protect your file, or to make it available for others.

makeatletter and makeatother
By default, LaTeX will allow the use of the '@' characters for control sequences from within package and class files, but not from within an end-user document. This way it is possible to protect commands, i.e. to make them accessible from packages only.

However it is possible to override this security with the duo and. These commands only make sense in a regular document, they are not needed in package or class files.

Creating your own package
Your package can be made available in your document just like any other package: using the command. Writing a package basically consists of copying the contents of your document preamble into a separate file with a name ending in .sty.

Let's write a first custom.sty file as an example package:


 * specifies which version of TeX or LaTeX is required  at least to run your package. The optional date may be used to specify the version more precisely.


 * A package introduces itself using this command.   should be identical to the basename of the file itself. The   should begin with a date in the format YYYY/MM/DD. Version information should be kept updated while developing a package.
 * Next you may write some TeX or LaTeX code like loading package, but write only the bare minimum needed for the package options set below.
 * is equivalent to.
 * are end-user parameters. Each option is declared by one such command.
 * tells which are the default.
 * terminates the option processing.
 * Write whatever you want in it using all the LaTeX commands you know. Normally you should define new commands or import other packages.
 * : this must be the last command.

Once your package is ready, we can use it in any document. Import your new package with the known command. The file custom.sty and the LaTeX source you are compiling must be in the same directory.

For a more convenient use, it is possible to place the package within $TEXMFHOME (which is <tt>~/texmf</tt> by default) according to the TeX Directory Structure (TDS). That would be

$TEXMFHOME/tex/latex/custom/custom.sty

On Windows '~' is often <tt>C:\Users\username</tt>.

You may have to run <tt>texhash</tt> (or equivalent) to make your TeX distribution index the new file, thus making it available for use for any document. It will allow you to use your package as detailed above, but without it needing to be in the same directory as your document.

Creating your own class
It is also possible to create your own class file. The process is similar to the creation of your own package, you can call your own class file in the preamble of any document by the command:

The name of the class file is then <tt>myclass.cls</tt>. Let's write a simple example, where we also make use of <tt>custom.sty</tt> that we defined above:


 * is the counterpart of.
 * and are used to automatically invoke the corresponding options when the class or the package is loaded.
 * : the starred version lets you handle non-implemented options.
 * will show the corresponding message in the TeX compiler output.
 * specifies the unique parent class, if any.

Hooks
There are also hooks for classes and packages.

They behave as the document hooks. See LaTeX Hooks.