% \iffalse meta-comment
%
% Copyright 1993 1994 1995 1996 1997
% The LaTeX3 Project and any individual authors listed elsewhere
% in this file.
%
% For further copyright information, and conditions for modification
% and distribution, see the file legal.txt, and any other copyright
% notices in this file.
%
% This file is part of the LaTeX2e system.
% ----------------------------------------
% This system is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%
% For error reports concerning UNCHANGED versions of this file no
% more than one year old, see bugs.txt.
%
% Please do not request updates from us directly. Primary
% distribution is through the CTAN archives.
%
%
% IMPORTANT COPYRIGHT NOTICE:
%
% You are NOT ALLOWED to distribute this file alone.
%
% You are allowed to distribute this file under the condition that it
% is distributed together with all the files listed in manifest.txt.
%
% If you receive only some of these files from someone, complain!
%
%
% Permission is granted to copy this file to another file with a
% clearly different name and to customize the declarations in that
% copy to serve the needs of your installation, provided that you
% comply with the conditions in the file legal.txt.
%
% However, NO PERMISSION is granted to produce or to distribute a
% modified version of this file under its original name.
%
% You are NOT ALLOWED to change this file.
%
%
%
% \fi
% Filename: clsguide.tex
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\documentclass{ltxguide}[1995/11/28]
\title{\LaTeXe~for class and package writers}
\author{Copyright \copyright~1995 The \LaTeX3 Project\\
All rights reserved}
\date{28 November 1996}
\begin{document}
\maketitle
\tableofcontents
\section{Introduction}
This document is an introduction to writing classes and packages for
\LaTeX{}, with special attention given to upgrading existing
\LaTeX~2.09 packages to \LaTeXe{}. The latter subject is also
covered in an article by Johannes Braams published in TUGboat~15.3.
\subsection{Writing classes and packages for \LaTeXe}
\LaTeX{} is a document preparation system that enables the document
writer to concentrate on the contents of their text, without bothering
too much about the formatting of it. For example, chapters are
indicated by |\chapter{
}| rather than by selecting 18pt bold.
The file that contains the information about how to turn logical
structure (like `|\chapter|') into formatting (like `18pt bold ragged
right') is a \emph{document class}. In addition, some features (such
as colour or included graphics) are independent of the document class
and these are contained in \emph{packages}.
One of the largest differences between \LaTeX~2.09 and \LaTeXe{} is in
the commands used to write packages and classes. In \LaTeX~2.09,
there was very little support for writing |.sty| files, and so writers
had to resort to using low-level commands.
\LaTeXe{} provides high-level commands for structuring packages.
It is also much easier to build classes and packages on top of each
other, for example writing a local technical report class |cetechr|
(for the Chemical Engineering department) based on |article|.
\subsection{Overview}
This document contains an overview of how to write classes and
packages for \LaTeX{}. It does \emph{not} introduce all of the
commands necessary to write packages: these can be found in either
\emph{\LaTeXbook} or \emph{\LaTeXcomp}. But it does describe the new
commands for structuring classes and packages.
\begin{description}
\item[Section~\ref{Sec:general}] contains some general advice about
writing classes and packages. It describes the difference between
classes and packages, the command naming conventions, the use of
|doc| and |docstrip|, how \TeX's primitive file and box commands
interact with \LaTeX{}. It also contains some hints about general
\LaTeX{} style.
\item[Section~\ref{Sec:structure}] describes the structure of classes
and packages. This includes building classes and packages on top of
other classes and packages, declaring options and declaring
commands. It also contains example classes.
\item[Section~\ref{Sec:commands}] lists the new class and package
commands.
\item[Section~\ref{Sec:upgrade}] gives detailed advice on how to
upgrade existing \LaTeX~2.09 classes and packages to \LaTeXe{}.
\end{description}
\subsection{Further information}
For a general introduction to \LaTeX{}, including the new features of
\LaTeXe{}, you should read \emph{\LaTeXbook}
by Leslie Lamport~\cite{A-W:LLa94}.
A more detailed description of the new features of \LaTeX{}, and an
overview of more than 150 packages, is to be found in
\emph{\LaTeXcomp} by Michel Goossens, Frank Mittelbach and Alexander
Samarin~\cite{A-W:GMS94}.
The \LaTeX{} system is based on \TeX{}, which is
described in \emph{The \TeX book} by Donald E.~Knuth~\cite{A-W:DEK91}.
There are a number of documentation files which accompany every copy
of \LaTeX{}. A copy of \emph{\LaTeX{} News} will come out with each
six-monthly release of \LaTeX{}, and is found in the files
|ltnews*.tex|. The author's guide \emph{\usrguide} describes the new
\LaTeX{} document features; it is in |usrguide.tex|. The guide
\emph{\fntguide} describes the \LaTeX{} font selection scheme for
class- and package-writers; it is in |fntguide.tex|. Configuring
\LaTeX{} is covered by the guide \emph{\cfgguide} in
\texttt{cfgguide.tex} whilst the philosophy behind our policy on
modifying \LaTeX{} is described in \emph{\modguide} in
\texttt{modguide.tex}.
We are gradually turning the source code for \LaTeX{} into a \LaTeX{}
document \emph{\sourcecode}. This document includes an index of
\LaTeX{} commands and can be typeset from |source2e.tex|.
For more information about \TeX{} and \LaTeX{}, please contact your
local \TeX{} Users Group, or the international \TeX{} Users Group.
Here are some useful addresses.
\begin{quote}\small\label{addrs}
\TeX{} Users Group, 1850 Union Street \#1637,
San~Francisco, CA~94123, USA\\
Fax:~+1~415~982~8559 \
Email: \texttt{tug@tug.org}
UK TUG, 1 Eymore Close, Selly Oak, Birmingham B29~4LB, UK\\
Fax: +44 121 476 2159 \ Email: \texttt{uktug-enquiries@tex.ac.uk}
\end{quote}
\section{Writing classes and packages}
\label{Sec:writing}
This section covers some general points concerned with writing
\LaTeX{} classes and packages.
\subsection{Old versions}
If you are upgrading an existing \LaTeX~2.09 style file then we
recommend freezing the 2.09 version and no longer maintaining it.
Experience with the various dialects of \LaTeX{} which existed in the
early 1990's suggests that maintaining packages for different versions
of \LaTeX{} is almost impossible. It will, of course, be necessary
for some organisations to maintain both versions in parallel for some
time but this is not essential for those packages and classes
supported by individuals: they should support only the new standard
\LaTeXe{}, not obsolete versions of \LaTeX{}.
\subsection{Using `docstrip' and `doc'}
If you are going to write a large class or package for \LaTeX{} then
you should consider using the |doc| software which comes with
\LaTeX{}.
\LaTeX{} classes and packages written using this can be
processed in two ways: they can be run through \LaTeX{}, to produce
documentation; and they can be processed with |docstrip|, to produce
the |.cls| or |.sty| file.
The |doc| software can automatically generate indexes of definitions,
indexes of command use, and change-log lists. It is very useful for
maintaining and documenting large \TeX{} sources.
The documented sources of the \LaTeX{} kernel itself, and of the
standard classes, etc, are |doc| documents; they are in the |.dtx|
files in the distribution. You can, in fact, typeset the source code
of the kernel as one long document, complete with index, by running
\LaTeX{} on |source2e.tex|.
For more information on |doc| and |docstrip|, consult the files
|docstrip.dtx|, |doc.dtx|, and \emph{\LaTeXcomp}. For examples of its
use, look at the |.dtx| files.
\subsection{Is it a class or a package?}
\label{Sec:classorpkg}
The first thing to do when you want to put some new \LaTeX{} commands
in a file is to decide whether it should be a \emph{document class} or a
\emph{package}. The rule of thumb is:
\begin{quote}
If the commands could be used with any document class, then make
them a package; and if not, then make them a class.
\end{quote}
There are two major types of class: those like |article|, |report| or
|letter|, which are free-standing; and those which are extensions or
variations of other classes---for example, the |proc| document class,
which is built on the |article| document class.
Thus, a company might have a local |ownlet| class for printing letters
with their own headed note-paper. Such a class would build on top of
the existing |letter| class but it cannot be used with any other
document class, so we have |ownlet.cls| rather than |ownlet.sty|.
The |graphics| package, in contrast, provides commands for including
images into a \LaTeX{} document. Since these commands can be used
with any document class, we have |graphics.sty| rather than
|graphics.cls|.
\subsection{Command names}
\LaTeX{} has three types of command.
There are the author commands, such as |\section|, |\emph| and
|\times|: most of these have short names, all in lower case.
There are also the class and package writer commands:
most of these have long mixed-case names such as the following.
\begin{verbatim}
\InputIfFileExists \RequirePackage \PassOptionsToClass
\end{verbatim}
Finally, there are the internal commands used in the \LaTeX{}
implementation, such as |\@tempcnta|, |\@ifnextchar| and |\@eha|:
most of these commands contain |@| in their name, which means they
cannot be used in documents, only in class and package files.
Unfortunately, for historical reasons the distinction between these
commands is often blurred. For example, |\hbox| is an internal
command which should only be used in the \LaTeX{} kernel, whereas
|\m@ne| is the constant $-1$ and could have been |\MinusOne|.
However, this rule of thumb is still useful: if a command has |@| in
its name then it is not part of the supported \LaTeX{} language---and
its behaviour may change in future releases! If a command is
mixed-case, or is described in \emph{\LaTeXbook}, then you can rely on
future releases of \LaTeXe{} supporting the command.
\subsection{Box commands and colour}
\label{Sec:colour}
Even if you do not intend to use colour in your own documents, by
taking note of the points in this section you can ensure that your
class or package is compatible with the |color| package. This may
benefit people using your class or package who have access to colour
printers.
The simplest way to ensure `colour safety' is to always use \LaTeX{}
box commands rather than \TeX{} primitives, that is use |\sbox| rather
than |\setbox|, |\mbox| rather than |\hbox| and |\parbox| or
the |minipage| environment rather than |\vbox|.
The \LaTeX{} box commands have new options which mean that they are now
as powerful as the \TeX{} primitives.
As an example of what can go wrong, consider that in
|{\ttfamily }|
the font is restored just \emph{before} the |}|, whereas in the
similar looking construction
|{\color{green} }|
the colour is restored just \emph{after} the final |}|. Normally this
distinction does not matter at all; but consider a primitive \TeX{}
box assignment such as:
\begin{verbatim}
\setbox0=\hbox{\color{green} }
\end{verbatim}
Now the colour-restore occurs after the |}| and so is \emph{not}
stored in the box. Exactly what bad effects this can have depends on
how colour is implemented: it can range from getting the wrong
colours in the rest of the document, to causing errors in the
dvi-driver used to print the document.
Also of interest is the command |\normalcolor|. This is
normally just |\relax| (i.e., does nothing)
but you can use it rather like |\normalfont| to
set regions of the page such as captions or section headings to the
`main document colour'.
\subsection{Defining text and math characters}
\label{Sec:chars}
Because \LaTeXe{} supports different encodings, definitions of commands
for producing symbols, accents, composite glyphs, etc.\ must be
defined using the commands provided for this purpose and described in
\emph{\fntguide}. This part of the system is still under development
so such tasks should be undertaken with great caution.
Also, |\DeclareRobustCommand| should be used for encoding-independent
commands of this type.
Note that it is no longer possible to refer to the math font set-up
outside math mode: for example, neither |\textfont 1| nor
|\scriptfont 2| are guaranteed to be defined in other modes.
\subsection{General style}
\label{Sec:general}
The new system provides many commands designed to help you produce
well-structured class and package files that are both robust and
portable. This section outlines some ways to make intelligent use of
these.
\subsubsection{Loading other files}
\label{Sec:loading}
\NEWdescription{1995/12/01}
\LaTeX{} provides these commands:
\begin{verbatim}
\LoadClass \LoadClassWithOptions
\RequirePackage \RequirePackageWithOptions
\end{verbatim}
for using classes or packages inside other classes or packages. We
recommend strongly that you use them, rather than the primitive
|\input| command, for a number of reasons.
Files loaded with |\input | will not be listed in the
|\listfiles| list.
If a package is always loaded with |\RequirePackage...| or |\usepackage|
then, even if its loading is requested several times, it will be
loaded only once. By contrast, if it is loaded with |\input| then it
can be loaded more than once; such an extra loading may waste time and
memory and it may produce strange results.
If a package provides option-processing then, again, strange results
are possible if the package is |\input| rather than loaded by means of
|\usepackage| or |\RequirePackage...|.
If the package |foo.sty| loads the package |baz.sty| by use of
|\input baz.sty| then the user will get a warning:
\begin{verbatim}
LaTeX Warning: You have requested package `foo',
but the package provides `baz'.
\end{verbatim}
Thus, for several reasons, using |\input| to load packages is not a
good idea.
Unfortunately, if you are upgrading the file |myclass.sty| to a class
file then you have to make sure that any old files which contain
|\input myclass.sty| still work.
This was also true for the standard classes (|article|, |book| and
|report|), since a lot of existing \LaTeX~2.09 document styles contain
|\input article.sty|. The approach which we use to solve this is
to provide minimal files |article.sty|, |book.sty| and |report.sty|,
which simply load the appropriate class files.
For example, |article.sty| contains just the following lines:
\begin{verbatim}
\NeedsTeXFormat{LaTeX2e}
\@obsoletefile{article.cls}{article.sty}
\LoadClass{article}
\end{verbatim}
You may wish to do the same or, if you think that it is safe to do so,
you may decide to just remove |myclass.sty|.
\subsubsection{Make it robust}
We consider it good practice, when writing packages and classes, to use
\LaTeX{} commands as much as possible.
Thus, instead of using |\def...| we recommend using one of
|\newcommand|, |\renewcommand| or |\providecommand|; |\CheckCommand| is
also useful. Doing this makes
it less likely that you will inadvertently redefine a command, giving
unexpected results.
When you define an environment, use |\newenvironment| or
|\renewenvironment| instead |\def\foo{...}| and |\def\endfoo{...}|.
If you need to set or change the value of a \m{dimen} or \m{skip}
register, use |\setlength|.
To manipulate boxes, use \LaTeX{} commands such as |\sbox|,
|\mbox| and |\parbox| rather than |\setbox|, |\hbox| and |\vbox|.
Use |\PackageError|, |\PackageWarning| or |\PackageInfo| (or the
equivalent class commands) rather than |\@latexerr|, |\@warning| or
|\wlog|.
It is still possible to declare options by defining |\ds@