% \iffalse meta-comment % % Copyright (C) 2023-2024 % The LaTeX Project and any individual authors listed elsewhere % in this file. % % This file is part of the LaTeX base system. % ------------------------------------------- % % It may be distributed and/or modified under the % conditions of the LaTeX Project Public License, either version 1.3c % of this license or (at your option) any later version. % The latest version of this license is in % http://www.latex-project.org/lppl.txt % and version 1.3c or later is part of all distributions of LaTeX % version 2008 or later. % % This file has the LPPL maintenance status "maintained". % % The list of all files belonging to the LaTeX base distribution is % given in the file `manifest.txt'. See also `legal.txt' for additional % information. % % The list of derived (unpacked) files belonging to the distribution % and covered by LPPL is defined by the unpacking scripts (with % extension .ins) which are part of the distribution. % % \fi % Filename: clsguide.tex \documentclass{ltxguide} \usepackage[T1]{fontenc} % needed for \textbackslash in tt \usepackage{csquotes} \title{\LaTeX\ for package and class authors\\current version} \author{\copyright~Copyright 2023--2024, \LaTeX\ Project Team.\\ All rights reserved.% \footnote{This file may distributed and/or modified under the conditions of the \LaTeX{} Project Public License, either version 1.3c of this license or (at your option) any later version. See the source \texttt{clsguide.tex} for full details.}% } \date{2024-11-14} \NewDocumentCommand\cs{m}{\texttt{\textbackslash\detokenize{#1}}} \NewDocumentCommand\marg{m}{\arg{#1}} \NewDocumentCommand\meta{m}{\ensuremath{\langle}\textit{#1}\ensuremath{\rangle}} \NewDocumentCommand\pkg{m}{\textsf{#1}} \NewDocumentCommand\text{m}{\ifmmode\mbox{#1}\else#1\fi} % Fix a 'feature' \makeatletter \renewcommand \verbatim@font {\normalfont \ttfamily} \makeatother \providecommand\url[1]{\texttt{#1}} \begin{document} \maketitle \tableofcontents \section{Introduction} \LaTeXe{} was released in 1994 and added a number of then-new concepts to \LaTeX{}. For package and class authors, these are described in the document \texttt{clsguide-historic}, which has largely remained unchanged. Since then, the \LaTeX{} team have worked on a number of ideas, firstly a programming language for \LaTeX{} (L3 programming layer) and then a range of tools for authors which build on that language. Here, we describe the current, stable set of tools provided by the \LaTeX{} kernel for package and class developers. We assume familiarity with general \LaTeX{} usage as a document author, and that the material here is read in conjunction with \texttt{usrguide}, which provides information for general \LaTeX{} users on up-to-date approaches to creating commands, etc. \section{Writing classes and packages} \label{sec:writing} This section covers some general points concerned with writing \LaTeX{} classes and packages. \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{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|. Typesetting these documents uses the class file |ltxdoc.cls|. 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{Policy on standard classes} Many of the problem reports we receive concerning the standard classes are not concerned with bugs but are suggesting, more or less politely, that the design decisions embodied in them are `not optimal' and asking us to modify them. There are several reasons why we should not make such changes to these files. \begin{itemize} \item However misguided, the current behavior is clearly what was intended when these classes were designed. \item It is not good practice to change such aspects of `standard classes' because many people will be relying on them. \end{itemize} We have therefore decided not to even consider making such modifications, nor to spend time justifying that decision. This does not mean that we do not agree that there are many deficiencies in the design of these classes, but we have many tasks with higher priority than continually explaining why the standard classes for \LaTeX{} cannot be changed. We would, of course, welcome the production of better classes, or of packages that can be used to enhance these classes. So your first thought when you consider such a deficiency will, we hope, be ``what can I do to improve this?'' \subsection{Command names} Prior to the introduction of the L3~programming layer described in the next section, \LaTeX{} had 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 behavior 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 \LaTeX{} supporting the command. \subsection{Programming support} As noted in the introduction, the \LaTeX{} kernel today loads dedicated support from programming, here referred to as the L3 programming layer but also often called \pkg{expl3}. Details of the general approach taken by the L3 programming layer are given in the document \texttt{expl3}, while a reference for all current code interfaces is available as \texttt{interface3}. This layer contains two types of command: a documented set of commands making up the API and a large number of private internal commands. The latter all start with two underscores and should not be used outside of the code module which defines them. This more structured approach means that using the L3 programming layer does not suffer from the somewhat fluid situation mentioned above with `\texttt{@} commands'. We do not cover the detail of using the L3 programming layer here. A good introduction to the approach is provided at \url{https://www.alanshawn.com/latex3-tutorial/}. \subsection{Box commands and color} \label{sec:color} Even if you do not intend to use color 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 and wish to use color. The simplest way to ensure `color 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 color 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 color-restore occurs after the |}| and so is \emph{not} stored in the box. Exactly what bad effects this can have depends on how color is implemented: it can range from getting the wrong colors 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 color'. \subsection{General style} \label{sec:general} \LaTeX{} 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} \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. 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| for programming and for defining document interfaces |\NewDocumentCommand|, etc. (see \texttt{usrguide} for details of these commands). When you define an environment, use |\NewDocumentEnvironment|, etc., (or |\newenvironment|, etc., for simple cases) instead of using |\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|. The advantage of this kind of practice is that your code is more readable and accessible to other experienced \LaTeX{} programmers. \subsubsection{Make it portable} It is also sensible to make your files are as portable as possible. To ensure this, files must not have the same name as a file in the standard \LaTeX{} distribution, however similar its contents may be to one of these files. It is also still lower risk to stick to file names which use only the ASCII range: whilst \LaTeX{} works natively with UTF-8, the same cannot be said with certainty for all tools. For the same reason, avoid spaces in file names. It is also useful if local classes or packages have a common prefix, for example the University of Nowhere classes might begin with |unw|. This helps to avoid every University having its own thesis class, all called |thesis.cls|. If you rely on some features of the \LaTeX{} kernel, or on a package, please specify the release-date you need. For example, the keyval options (see Section~\ref{sec:opt:keyval}) were introduced in the June 2022 release so, if you use them then you should put: \begin{verbatim} \NeedsTeXFormat{LaTeX2e}[2022-06-01] \end{verbatim} \subsubsection{Useful hooks} It is sometimes necessary for a package to arrange for code to be executed at the start or end of the preamble, at the end of the document or at the start of every use of an environment. This can be carried out by using hooks. As a document author, you will likely be familiar with |\AtBeginDocument|, a wrapper around the more powerful command |\AddToHook|. The \LaTeX{} kernel provides a large number of dedicated hooks (applying in a pre-defined location) and generic hooks (applying to arbitrary commands): the interface for using these is described in \texttt{lthooks}. There are also hooks to apply to files, described in \texttt{ltfilehooks}. \section{The structure of a class or package} \label{sec:structure} The outline of a class or package file is: \begin{description} \item[Identification] The file says that it is a \LaTeXe{} package or class, and gives a short description of itself. \item[Preliminary declarations] Here the file declares some commands and can also load other files. Usually these commands will be just those needed for the code used in the declared options. \item[Options] The file declares and processes its options. \item[More declarations] This is where the file does most of its work: declaring new variables, commands and fonts; and loading other files. \end{description} \subsection{Identification% \label{sec:ID}} The first thing a class or package file does is identify itself. Package files do this as follows: \begin{verbatim} \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{}[ ] \end{verbatim} For example: \begin{verbatim} \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{latexsym}[1998-08-17 Standard LaTeX package] \end{verbatim} Class files do this as follows: \begin{verbatim} \NeedsTeXFormat{LaTeX2e} \ProvidesClass{}[ ] \end{verbatim} For example: \begin{verbatim} \NeedsTeXFormat{LaTeX2e} \ProvidesClass{article}[2022-06-01 Standard LaTeX class] \end{verbatim} The \m{date} should be given in the form `\textsc{yyyy-mm-dd}' or `\textsc{yyyy/mm/dd}' and must be present if the optional argument is used. Exactly four digits are required for the year and two each for the month and day. Where necessary, zeros should be added to pad the month and day appropriately. If digits or separators are missing, the date will likely be misinterpreted: the commands expect a valid syntax to speed up the routine usage of the package or class and make no provision for the case there is an error in the date specification. This date is checked whenever a user specifies a date in their |\documentclass| or |\usepackage| command. For example, if you wrote: \begin{verbatim} \documentclass{article}[2022-06-01] \end{verbatim} then users at a different location would get a warning that their copy of |article| was out of date. The description of a class is displayed when the class is used. The description of a package is put into the log file. These descriptions are also displayed by the |\listfiles| command. The phrase \texttt{Standard LaTeX} \textbf{must not} be used in the identification banner of any file other than those in the standard \LaTeX{} distribution. \subsection{Using classes and packages} A \LaTeX{} package or class can load a package as follows: \begin{verbatim} \RequirePackage[]{}[] \end{verbatim} For example: \begin{verbatim} \RequirePackage{ifthen}[2022-06-01] \end{verbatim} This command has the same syntax as the author command |\usepackage|. It allows packages or classes to use features provided by other packages. For example, by loading the |ifthen| package, a package writer can use the `if\dots then\dots else\dots' commands provided by that package. A \LaTeX{} class can load one other class as follows: \begin{verbatim} \LoadClass[]{}[] \end{verbatim} For example: \begin{verbatim} \LoadClass[twocolumn]{article} \end{verbatim} This command has the same syntax as the author command |\documentclass|. It allows classes to be based on the syntax and appearance of another class. For example, by loading the |article| class, a class writer only has to change the bits of |article| they don't like, rather than writing a new class from scratch. The following commands can be used in the common case that you want to simply load a class or package file with exactly those options that are being used by the current class. \begin{verbatim} \LoadClassWithOptions{}[] \RequirePackageWithOptions{}[] \end{verbatim} For example: \begin{verbatim} \LoadClassWithOptions{article} \RequirePackageWithOptions{graphics}[1995/12/01] \end{verbatim} \subsection{Declaring options} Packages and classes can declare options and these can be specified by authors; for example, the |twocolumn| option is declared by the |article| class. Note that the name of an option should contain only those characters allowed in a `\LaTeX{} name'; in particular it must not contain any control sequences. \LaTeX{} supports two methods for creating options: a key--value system and a `simple text' approach. The key--value system is recommended for new classes and packages, and is more flexible in handling of option classes than the simple text approach. Both option methods use the same basic structure within the \LaTeX{} source: declaration of options first then processing options in a second step. Both also allow options to be passed on to other packages or an underlying class. As the `classical' simple text approach is conceptually more straight-forward to illustrate, it is used here to show the general structure: see Section~\ref{sec:opt:keyval} for full details of the key--value approach. An option is declared as follows: \begin{verbatim} \DeclareOption{