[latex3-commits] [git/LaTeX3-latex3-latex2e] clsguide-cleanup: Add section 'structure' (57f00e3e)
Joseph Wright
joseph.wright at morningstar2.co.uk
Wed Jan 11 10:50:29 CET 2023
Repository : https://github.com/latex3/latex2e
On branch : clsguide-cleanup
Link : https://github.com/latex3/latex2e/commit/57f00e3e1db83505b7d0f17c73b4dbd43fd087ed
>---------------------------------------------------------------
commit 57f00e3e1db83505b7d0f17c73b4dbd43fd087ed
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date: Wed Jan 11 09:50:29 2023 +0000
Add section 'structure'
The 2.09 refs are dropped here, otherwise
largely copy-paste.
Notice that at present the options section is
unchanged: that will be addressed once the
keyval ideas are written up.
>---------------------------------------------------------------
57f00e3e1db83505b7d0f17c73b4dbd43fd087ed
base/doc/clsguide.tex | 270 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 270 insertions(+)
diff --git a/base/doc/clsguide.tex b/base/doc/clsguide.tex
index dd589f3c..eca19a94 100644
--- a/base/doc/clsguide.tex
+++ b/base/doc/clsguide.tex
@@ -316,4 +316,274 @@ 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}
+
+The first thing a class or package file does is identify itself.
+Package files do this as follows:
+\begin{verbatim}
+ \NeedsTeXFormat{LaTeX2e}
+ \ProvidesPackage{<package>}[<date> <other information>]
+\end{verbatim}
+For example:
+\begin{verbatim}
+ \NeedsTeXFormat{LaTeX2e}
+ \ProvidesPackage{latexsym}[2002-06-01 Standard LaTeX package]
+\end{verbatim}
+Class files do this as follows:
+\begin{verbatim}
+ \NeedsTeXFormat{LaTeX2e}
+ \ProvidesClass{<class-name>}[<date> <other information>]
+\end{verbatim}
+For example:
+\begin{verbatim}
+ \NeedsTeXFormat{LaTeX2e}
+ \ProvidesClass{article}[2002-06-01 Standard LaTeX class]
+\end{verbatim}
+The \m{date} should be given in the form `\textsc{YYYY-MM-DD}' and must be
+present if the optional argument is used (this is also true for the
+|\NeedsTeXFormat| command). Any derivation from this syntax will result in
+low-level \TeX{} errors---the commands expect a valid syntax to speed up the
+daily usage of the package or class and make no provision for the case that the
+developer made a mistake!
+
+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}[2002-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[<options>]{<package>}[<date>]
+\end{verbatim}
+For example:
+\begin{verbatim}
+ \RequirePackage{ifthen}[2002-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[<options>]{<class-name>}[<date>]
+\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{<class-name>}[<date>]
+ \RequirePackageWithOptions{<package>}[<date>]
+\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.
+
+An option is declared as follows:
+\begin{verbatim}
+ \DeclareOption{<option>}{<code>}
+\end{verbatim}
+For example, the |dvips| option (slightly simplified)
+to the |graphics| package is implemented as:
+\begin{verbatim}
+ \DeclareOption{dvips}{\input{dvips.def}}
+\end{verbatim}
+This means that when an author writes |\usepackage[dvips]{graphics}|, the file
+|dvips.def| is loaded. As another example, the |a4paper| option is declared in
+the |article| class to set the |\paperheight| and |\paperwidth| lengths:
+\begin{verbatim}
+ \DeclareOption{a4paper}{%
+ \setlength{\paperheight}{297mm}%
+ \setlength{\paperwidth}{210mm}%
+ }
+\end{verbatim}
+Sometimes a user will request an option which the class or package has not
+explicitly declared. By default this will produce a warning (for classes) or
+error (for packages); this behaviour can be altered as follows:
+\begin{verbatim}
+ \DeclareOption*{<code>}
+\end{verbatim}
+For example, to make the package |fred| produce a warning rather than an error
+for unknown options, you could specify:
+\begin{verbatim}
+ \DeclareOption*{%
+ \PackageWarning{fred}{Unknown option `\CurrentOption'}%
+ }
+\end{verbatim}
+Then, if an author writes |\usepackage[foo]{fred}|, they will get a warning
+\texttt{Package fred Warning: Unknown option `foo'.} As another example, the
+|fontenc| package tries to load a file |<ENC>enc.def| whenever the \m{ENC}
+option is used. This can be done by writing:
+\begin{verbatim}
+ \DeclareOption*{%
+ \input{\CurrentOption enc.def}%
+ }
+\end{verbatim}
+
+It is possible to pass options on to another package or class, using the
+command |\PassOptionsToPackage| or |\PassOptionsToClass| (note that this is a
+specialised operation that works only for option names). For example, to pass
+every unknown option on to the |article| class, you can use:
+\begin{verbatim}
+ \DeclareOption*{%
+ \PassOptionsToClass{\CurrentOption}{article}%
+ }
+\end{verbatim}
+If you do this then you should make sure you load the class at some later
+point, otherwise the options will never be processed!
+
+So far, we have explained only how to declare options, not how to execute them.
+To process the options with which the file was called, you should use:
+\begin{verbatim}
+ \ProcessOptions\relax
+\end{verbatim}
+This executes the \m{code} for each option that was both specified and declared
+(see Section~\ref{Sec:commands.options} for details of how this is done).
+
+For example, if the |jane| package file contains:
+\begin{verbatim}
+ \DeclareOption{foo}{\typeout{Saw foo.}}
+ \DeclareOption{baz}{\typeout{Saw baz.}}
+ \DeclareOption*{\typeout{What's \CurrentOption?}}
+ \ProcessOptions\relax
+\end{verbatim}
+and an author writes |\usepackage[foo,bar]{jane}|, then they will see the
+messages \texttt{Saw foo.} and \texttt{What's bar?}
+
+\subsection{Example: a local letter class}
+
+A company may have its own letter class, for setting letters in the
+company style. This section shows a simple implementation of such a
+class, although a real class would need more structure.
+
+The class begins by announcing itself as |neplet.cls|.
+\begin{verbatim}
+ \NeedsTeXFormat{LaTeX2e}
+ \ProvidesClass{neplet}[2022-06-01 NonExistent Press letter class]
+\end{verbatim}
+Then this next bit passes any options on to the |letter| class, which is loaded
+with the |a4paper| option.
+\begin{verbatim}
+ \DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}
+ \ProcessOptions\relax
+ \LoadClass[a4paper]{letter}
+\end{verbatim}
+In order to use the company letter head, it redefines the |firstpage| page
+style: this is the page style that is used on the first page of letters.
+\begin{verbatim}
+ \renewcommand{\ps at firstpage}{%
+ \renewcommand{\@oddhead}{<letterhead goes here>}%
+ \renewcommand{\@oddfoot}{<letterfoot goes here>}%
+ }
+\end{verbatim}
+And that's it!
+
+\subsection{Example: a newsletter class}
+
+A simple newsletter can be typeset with \LaTeX{}, using a variant of the
+|article| class. The class begins by announcing itself as |smplnews.cls|.
+\begin{verbatim}
+ \NeedsTeXFormat{LaTeX2e}
+ \ProvidesClass{smplnews}[2022-06-01 The Simple News newsletter class]
+
+ \newcommand{\headlinecolor}{\normalcolor}
+\end{verbatim}
+It passes most specified options on to the |article| class: apart from the
+|onecolumn| option, which is switched off, and the |green| option, which sets
+the headline in green.
+\begin{verbatim}
+ \DeclareOption{onecolumn}{\OptionNotUsed}
+ \DeclareOption{green}{\renewcommand{\headlinecolor}{\color{green}}}
+
+ \DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
+
+ \ProcessOptions\relax
+\end{verbatim}
+It then loads the class |article| with the option |twocolumn|.
+\begin{verbatim}
+ \LoadClass[twocolumn]{article}
+\end{verbatim}
+Since the newsletter is to be printed in colour, it now loads the |color|
+package. The class does not specify a device driver option since this should be
+specified by the user of the |smplnews| class.
+\begin{verbatim}
+ \RequirePackage{color}
+\end{verbatim}
+The class then redefines |\maketitle| to produce the title in 72\,pt Helvetica
+bold oblique, in the appropriate colour.
+\begin{verbatim}
+ \renewcommand{\maketitle}{%
+ \twocolumn[%
+ \fontsize{72}{80}\fontfamily{phv}\fontseries{b}%
+ \fontshape{sl}\selectfont\headlinecolor
+ \@title
+ ]%
+ }
+\end{verbatim}
+It redefines |\section| and switches off section numbering.
+\begin{verbatim}
+ \renewcommand{\section}{%
+ \@startsection
+ {section}{1}{0pt}{-1.5ex plus -1ex minus -.2ex}%
+ {1ex plus .2ex}{\large\sffamily\slshape\headlinecolor}%
+ }
+
+ \setcounter{secnumdepth}{0}
+\end{verbatim}
+It also sets the three essential things.
+\begin{verbatim}
+ \renewcommand{\normalsize}{\fontsize{9}{10}\selectfont}
+ \setlength{\textwidth}{17.5cm}
+ \setlength{\textheight}{25cm}
+\end{verbatim}
+In practice, a class would need more than this: it would provide commands for
+issue numbers, authors of articles, page styles and so on; but this skeleton
+gives a start. The |ltnews| class file is not much more complex than this one.
+
\end{document}
More information about the latex3-commits
mailing list.