Tidy up char 127
[latex2e.git] / trunk / doc / clsguide.tex
blobc90bc539b26c451abf6c9c5213b531c7ac773792
1 % \iffalse meta-comment
3 % Copyright 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
4 % The LaTeX3 Project and any individual authors listed elsewhere
5 % in this file.
6 %
7 % This file is part of the LaTeX base system.
8 % -------------------------------------------
9 %
10 % It may be distributed and/or modified under the
11 % conditions of the LaTeX Project Public License, either version 1.3c
12 % of this license or (at your option) any later version.
13 % The latest version of this license is in
14 % http://www.latex-project.org/lppl.txt
15 % and version 1.3c or later is part of all distributions of LaTeX
16 % version 2005/12/01 or later.
18 % This file has the LPPL maintenance status "maintained".
20 % The list of all files belonging to the LaTeX base distribution is
21 % given in the file `manifest.txt'. See also `legal.txt' for additional
22 % information.
24 % The list of derived (unpacked) files belonging to the distribution
25 % and covered by LPPL is defined by the unpacking scripts (with
26 % extension .ins) which are part of the distribution.
28 % \fi
29 % Filename: clsguide.tex
31 \NeedsTeXFormat{LaTeX2e}[1995/12/01]
33 \documentclass{ltxguide}[1995/11/28]
35 \title{\LaTeXe~for class and package writers}
37 \author{Copyright \copyright~1995--2006 The \LaTeX3 Project\\
38 All rights reserved}
40 \date{15 February 2006}
42 \begin{document}
44 \maketitle
46 \tableofcontents
48 \section{Introduction}
50 This document is an introduction to writing classes and packages for
51 \LaTeX{}, with special attention given to upgrading existing
52 \LaTeX~2.09 packages to \LaTeXe{}. The latter subject is also
53 covered in an article by Johannes Braams published in TUGboat~15.3.
55 \subsection{Writing classes and packages for \LaTeXe}
57 \LaTeX{} is a document preparation system that enables the document
58 writer to concentrate on the contents of their text, without bothering
59 too much about the formatting of it. For example, chapters are
60 indicated by |\chapter{<title>}| rather than by selecting 18pt bold.
62 The file that contains the information about how to turn logical
63 structure (like `|\chapter|') into formatting (like `18pt bold ragged
64 right') is a \emph{document class}. In addition, some features (such
65 as colour or included graphics) are independent of the document class
66 and these are contained in \emph{packages}.
68 One of the largest differences between \LaTeX~2.09 and \LaTeXe{} is in
69 the commands used to write packages and classes. In \LaTeX~2.09,
70 there was very little support for writing |.sty| files, and so writers
71 had to resort to using low-level commands.
73 \LaTeXe{} provides high-level commands for structuring packages.
74 It is also much easier to build classes and packages on top of each
75 other, for example writing a local technical report class |cetechr|
76 (for the Chemical Engineering department) based on |article|.
78 \subsection{Overview}
80 This document contains an overview of how to write classes and
81 packages for \LaTeX{}. It does \emph{not} introduce all of the
82 commands necessary to write packages: these can be found in either
83 \emph{\LaTeXbook} or \emph{\LaTeXcomp}. But it does describe the new
84 commands for structuring classes and packages.
86 \begin{description}
88 \item[Section~\ref{Sec:general}] contains some general advice about
89 writing classes and packages. It describes the difference between
90 classes and packages, the command naming conventions, the use of
91 |doc| and |docstrip|, how \TeX's primitive file and box commands
92 interact with \LaTeX{}. It also contains some hints about general
93 \LaTeX{} style.
95 \item[Section~\ref{Sec:structure}] describes the structure of classes
96 and packages. This includes building classes and packages on top of
97 other classes and packages, declaring options and declaring
98 commands. It also contains example classes.
100 \item[Section~\ref{Sec:commands}] lists the new class and package
101 commands.
103 \item[Section~\ref{Sec:upgrade}] gives detailed advice on how to
104 upgrade existing \LaTeX~2.09 classes and packages to \LaTeXe{}.
106 \end{description}
108 \subsection{Further information}
110 For a general introduction to \LaTeX{}, including the new features of
111 \LaTeXe{}, you should read \emph{\LaTeXbook}
112 by Leslie Lamport~\cite{A-W:LLa94}.
114 A more detailed description of the new features of \LaTeX, including an
115 overview of more than 200 packages and nearly 1000 ready to run examples, is
116 to be found in \emph{\LaTeXcomp second edition} by Frank Mittelbach and
117 Michel Goossens~\cite{A-W:MG2004}.
119 The \LaTeX{} system is based on \TeX{}, which is
120 described in \emph{The \TeX book} by Donald E.~Knuth~\cite{A-W:DEK91}.
122 There are a number of documentation files which accompany every copy
123 of \LaTeX{}. A copy of \emph{\LaTeX{} News} will come out with each
124 six-monthly release of \LaTeX{}, and is found in the files
125 |ltnews*.tex|. The author's guide \emph{\usrguide} describes the new
126 \LaTeX{} document features; it is in |usrguide.tex|. The guide
127 \emph{\fntguide} describes the \LaTeX{} font selection scheme for
128 class- and package-writers; it is in |fntguide.tex|. Configuring
129 \LaTeX{} is covered by the guide \emph{\cfgguide} in
130 \texttt{cfgguide.tex} whilst the philosophy behind our policy on
131 modifying \LaTeX{} is described in \emph{\modguide} in
132 \texttt{modguide.tex}.
134 The documented source code (from the files used to produce the kernel
135 format via |latex.ltx|) is now available as
136 \emph{The \LaTeXe\ Sources}.
137 This very large document also includes an index of
138 \LaTeX{} commands. It can be typeset from the \LaTeX{} file
139 |source2e.tex| in the |base| directory, using the source files and
140 the class file |ltxdoc.cls| from this directory.
142 For more information about \TeX{} and \LaTeX{}, please contact your
143 local \TeX{} Users Group, or the international \TeX{} Users Group.
144 Addresses and other details can be found at:
145 \begin{quote}\small\label{addrs}
146 \texttt{http://www.tug.org/lugs.html}
147 \end{quote}
150 \subsection{Policy on standard classes}
152 Many of the problem reports we receive concerning the standard classes
153 are not concerned with bugs but are suggesting, more or less politely,
154 that the design decisions embodied in them are `not optimal' and
155 asking us to modify them.
157 There are several reasons why we should not make such changes to these
158 files.
159 \begin{itemize}
160 \item
161 However misguided, the current behaviour is clearly what was
162 intended when these classes were designed.
163 \item
164 It is not good practice to change such aspects of `standard classes'
165 because many people will be relying on them.
166 \end{itemize}
168 We have therefore decided not to even consider making such
169 modifications, nor to spend time justifying that decision. This does
170 not mean that we do not agree that there are many deficiencies in the
171 design of these classes, but we have many tasks with higher priority
172 than continually explaining why the standard classes for \LaTeX{}
173 cannot be changed.
175 We would, of course, welcome the production of better classes, or of
176 packages that can be used to enhance these classes. So your first
177 thought when you consider such a deficiency will, we hope, be ``what
178 can I do to improve this?''
180 Similar considerations apply to those parts of the kernel that are
181 implementing design decisions, many of which should be left to the
182 class file but are not in the current system. We realise that in such
183 cases it is much more difficult for you to rectify the problem
184 yourself but it is also the case that making such changes in the
185 kernel would probably be a major project for us; therefore such
186 enhancements will have to wait for \LaTeX3.
188 \section{Writing classes and packages}
189 \label{Sec:writing}
191 This section covers some general points concerned with writing
192 \LaTeX{} classes and packages.
195 \subsection{Old versions}
197 If you are upgrading an existing \LaTeX~2.09 style file then we
198 recommend freezing the 2.09 version and no longer maintaining it.
199 Experience with the various dialects of \LaTeX{} which existed in the
200 early 1990's suggests that maintaining packages for different versions
201 of \LaTeX{} is almost impossible. It will, of course, be necessary
202 for some organisations to maintain both versions in parallel for some
203 time but this is not essential for those packages and classes
204 supported by individuals: they should support only the new standard
205 \LaTeXe{}, not obsolete versions of \LaTeX{}.
208 \subsection{Using `docstrip' and `doc'}
210 If you are going to write a large class or package for \LaTeX{} then
211 you should consider using the |doc| software which comes with
212 \LaTeX{}.
213 \LaTeX{} classes and packages written using this can be
214 processed in two ways: they can be run through \LaTeX{}, to produce
215 documentation; and they can be processed with |docstrip|, to produce
216 the |.cls| or |.sty| file.
218 The |doc| software can automatically generate indexes of definitions,
219 indexes of command use, and change-log lists. It is very useful for
220 maintaining and documenting large \TeX{} sources.
222 The documented sources of the \LaTeX{} kernel itself, and of the
223 standard classes, etc, are |doc| documents; they are in the |.dtx|
224 files in the distribution. You can, in fact, typeset the source code
225 of the kernel as one long document, complete with index, by running
226 \LaTeX{} on |source2e.tex|. Typesetting these documents uses the
227 class file |ltxdoc.cls|.
229 For more information on |doc| and |docstrip|, consult the files
230 |docstrip.dtx|, |doc.dtx|, and \emph{\LaTeXcomp}. For examples of its
231 use, look at the |.dtx| files.
233 \subsection{Is it a class or a package?}
234 \label{Sec:classorpkg}
236 The first thing to do when you want to put some new \LaTeX{} commands
237 in a file is to decide whether it should be a \emph{document class} or a
238 \emph{package}. The rule of thumb is:
239 \begin{quote}
240 If the commands could be used with any document class, then make
241 them a package; and if not, then make them a class.
242 \end{quote}
244 There are two major types of class: those like |article|, |report| or
245 |letter|, which are free-standing; and those which are extensions or
246 variations of other classes---for example, the |proc| document class,
247 which is built on the |article| document class.
249 Thus, a company might have a local |ownlet| class for printing letters
250 with their own headed note-paper. Such a class would build on top of
251 the existing |letter| class but it cannot be used with any other
252 document class, so we have |ownlet.cls| rather than |ownlet.sty|.
254 The |graphics| package, in contrast, provides commands for including
255 images into a \LaTeX{} document. Since these commands can be used
256 with any document class, we have |graphics.sty| rather than
257 |graphics.cls|.
260 \subsection{Command names}
262 \LaTeX{} has three types of command.
264 There are the author commands, such as |\section|, |\emph| and
265 |\times|: most of these have short names, all in lower case.
267 There are also the class and package writer commands:
268 most of these have long mixed-case names such as the following.
269 \begin{verbatim}
270 \InputIfFileExists \RequirePackage \PassOptionsToClass
271 \end{verbatim}
273 Finally, there are the internal commands used in the \LaTeX{}
274 implementation, such as |\@tempcnta|, |\@ifnextchar| and |\@eha|:
275 most of these commands contain |@| in their name, which means they
276 cannot be used in documents, only in class and package files.
278 Unfortunately, for historical reasons the distinction between these
279 commands is often blurred. For example, |\hbox| is an internal
280 command which should only be used in the \LaTeX{} kernel, whereas
281 |\m@ne| is the constant $-1$ and could have been |\MinusOne|.
283 However, this rule of thumb is still useful: if a command has |@| in
284 its name then it is not part of the supported \LaTeX{} language---and
285 its behaviour may change in future releases! If a command is
286 mixed-case, or is described in \emph{\LaTeXbook}, then you can rely on
287 future releases of \LaTeXe{} supporting the command.
289 \subsection{Box commands and colour}
290 \label{Sec:colour}
292 Even if you do not intend to use colour in your own documents, by
293 taking note of the points in this section you can ensure that your
294 class or package is compatible with the |color| package. This may
295 benefit people using your class or package who have access to colour
296 printers.
298 The simplest way to ensure `colour safety' is to always use \LaTeX{}
299 box commands rather than \TeX{} primitives, that is use |\sbox| rather
300 than |\setbox|, |\mbox| rather than |\hbox| and |\parbox| or
301 the |minipage| environment rather than |\vbox|.
302 The \LaTeX{} box commands have new options which mean that they are now
303 as powerful as the \TeX{} primitives.
305 As an example of what can go wrong, consider that in
306 |{\ttfamily <text>}|
307 the font is restored just \emph{before} the |}|, whereas in the
308 similar looking construction
309 |{\color{green} <text>}|
310 the colour is restored just \emph{after} the final |}|. Normally this
311 distinction does not matter at all; but consider a primitive \TeX{}
312 box assignment such as:
313 \begin{verbatim}
314 \setbox0=\hbox{\color{green} <text>}
315 \end{verbatim}
316 Now the colour-restore occurs after the |}| and so is \emph{not}
317 stored in the box. Exactly what bad effects this can have depends on
318 how colour is implemented: it can range from getting the wrong
319 colours in the rest of the document, to causing errors in the
320 dvi-driver used to print the document.
322 Also of interest is the command |\normalcolor|. This is
323 normally just |\relax| (i.e., does nothing)
324 but you can use it rather like |\normalfont| to
325 set regions of the page such as captions or section headings to the
326 `main document colour'.
329 \subsection{Defining text and math characters}
330 \label{Sec:chars}
332 Because \LaTeXe{} supports different encodings, definitions of commands
333 for producing symbols, accents, composite glyphs, etc.\ must be
334 defined using the commands provided for this purpose and described in
335 \emph{\fntguide}. This part of the system is still under development
336 so such tasks should be undertaken with great caution.
338 Also, |\DeclareRobustCommand| should be used for encoding-independent
339 commands of this type.
341 Note that it is no longer possible to refer to the math font set-up
342 outside math mode: for example, neither |\textfont 1| nor
343 |\scriptfont 2| are guaranteed to be defined in other modes.
346 \subsection{General style}
347 \label{Sec:general}
349 The new system provides many commands designed to help you produce
350 well-structured class and package files that are both robust and
351 portable. This section outlines some ways to make intelligent use of
352 these.
354 \subsubsection{Loading other files}
355 \label{Sec:loading}
357 \NEWdescription{1995/12/01}
358 \LaTeX{} provides these commands:
359 \begin{verbatim}
360 \LoadClass \LoadClassWithOptions
361 \RequirePackage \RequirePackageWithOptions
362 \end{verbatim}
363 for using classes or packages inside other classes or packages. We
364 recommend strongly that you use them, rather than the primitive
365 |\input| command, for a number of reasons.
367 Files loaded with |\input <filename>| will not be listed in the
368 |\listfiles| list.
370 If a package is always loaded with |\RequirePackage...| or |\usepackage|
371 then, even if its loading is requested several times, it will be
372 loaded only once. By contrast, if it is loaded with |\input| then it
373 can be loaded more than once; such an extra loading may waste time and
374 memory and it may produce strange results.
376 If a package provides option-processing then, again, strange results
377 are possible if the package is |\input| rather than loaded by means of
378 |\usepackage| or |\RequirePackage...|.
380 If the package |foo.sty| loads the package |baz.sty| by use of
381 |\input baz.sty| then the user will get a warning:
382 \begin{verbatim}
383 LaTeX Warning: You have requested package `foo',
384 but the package provides `baz'.
385 \end{verbatim}
386 Thus, for several reasons, using |\input| to load packages is not a
387 good idea.
389 Unfortunately, if you are upgrading the file |myclass.sty| to a class
390 file then you have to make sure that any old files which contain
391 |\input myclass.sty| still work.
393 This was also true for the standard classes (|article|, |book| and
394 |report|), since a lot of existing \LaTeX~2.09 document styles contain
395 |\input article.sty|. The approach which we use to solve this is
396 to provide minimal files |article.sty|, |book.sty| and |report.sty|,
397 which simply load the appropriate class files.
399 For example, |article.sty| contains just the following lines:
400 \begin{verbatim}
401 \NeedsTeXFormat{LaTeX2e}
402 \@obsoletefile{article.cls}{article.sty}
403 \LoadClass{article}
404 \end{verbatim}
405 You may wish to do the same or, if you think that it is safe to do so,
406 you may decide to just remove |myclass.sty|.
408 \subsubsection{Make it robust}
410 We consider it good practice, when writing packages and classes, to use
411 \LaTeX{} commands as much as possible.
413 Thus, instead of using |\def...| we recommend using one of
414 |\newcommand|, |\renewcommand| or |\providecommand|; |\CheckCommand| is
415 also useful. Doing this makes
416 it less likely that you will inadvertently redefine a command, giving
417 unexpected results.
419 When you define an environment, use |\newenvironment| or
420 |\renewenvironment| instead |\def\foo{...}| and |\def\endfoo{...}|.
422 If you need to set or change the value of a \m{dimen} or \m{skip}
423 register, use |\setlength|.
425 To manipulate boxes, use \LaTeX{} commands such as |\sbox|,
426 |\mbox| and |\parbox| rather than |\setbox|, |\hbox| and |\vbox|.
428 Use |\PackageError|, |\PackageWarning| or |\PackageInfo| (or the
429 equivalent class commands) rather than |\@latexerr|, |\@warning| or
430 |\wlog|.
432 It is still possible to declare options by defining |\ds@<option>| and
433 calling |\@options|; but we recommend the |\DeclareOption| and
434 |\ProcessOptions| commands instead. These are more powerful and use
435 less memory. So rather than using:
436 \begin{verbatim}
437 \def\ds@draft{\overfullrule 5pt}
438 \@options
439 \end{verbatim}
440 you should use:
441 \begin{verbatim}
442 \DeclareOption{draft}{\setlength{\overfullrule}{5pt}}
443 \ProcessOptions\relax
444 \end{verbatim}
446 The advantage of this kind of practice is that your code is more
447 readable and, more important, that it is less likely to break when
448 used with future versions of \LaTeX{}.
450 \subsubsection{Make it portable}
452 It is also sensible to make your files are as portable as possible. To
453 ensure this; they should contain only visible 7-bit text; and the
454 filenames should contain at most eight characters (plus the three
455 letter extension). Also, of course, it \textbf{must not} have the
456 same name as a file in the standard \LaTeX{} distribution, however
457 similar its contents may be to one of these files.
459 It is also useful if local classes or packages have a common prefix,
460 for example the University of Nowhere classes might begin with |unw|.
461 This helps to avoid every University having its own thesis class, all
462 called |thesis.cls|.
464 If you rely on some features of the \LaTeX{} kernel, or on a package,
465 please specify the release-date you need. For example, the package
466 error commands were introduced in the June 1994 release so, if you use
467 them then you should put:
468 \begin{verbatim}
469 \NeedsTeXFormat{LaTeX2e}[1994/06/01]
470 \end{verbatim}
472 \subsubsection{Useful hooks}
474 Some packages and document styles had to redefine the command
475 |\document| or |\enddocument| to achieve their goal. This is no
476 longer necessary. You can now use the `hooks' |\AtBeginDocument| and
477 |\AtEndDocument| (see Section~\ref{Sec:delays}). Again, using these
478 hooks makes it less likely that your code breaks with future versions
479 of \LaTeX{}. It also makes it more likely that your package can work
480 together with someone else's.
482 \NEWdescription{1996/12/01}
483 However, note that code in the |\AtBeginDocument| hook is part of the
484 preamble. Thus there are restrictions on what can be put there; in
485 particular, no typesetting can be done.
487 \section{The structure of a class or package}
488 \label{Sec:structure}
490 \LaTeXe{} classes and packages have more structure than \LaTeX~2.09
491 style files did. The outline of a class or package file is:
492 \begin{description}
493 \item[Identification] The file says that it is a \LaTeXe{} package or
494 class, and gives a short description of itself.
495 \item[Preliminary declarations]
496 Here the file declares some commands and can also load
497 other files. Usually these commands will be just those needed for
498 the code used in the declared options.
499 \item[Options] The file declares and processes its options.
500 \item[More declarations] This is where the file does most of its work:
501 declaring new variables, commands and fonts; and loading other files.
502 \end{description}
504 \subsection{Identification}
506 The first thing a class or package file does is identify itself.
507 Package files do this as follows:
508 \begin{verbatim}
509 \NeedsTeXFormat{LaTeX2e}
510 \ProvidesPackage{<package>}[<date> <other information>]
511 \end{verbatim}
512 For example:
513 \begin{verbatim}
514 \NeedsTeXFormat{LaTeX2e}
515 \ProvidesPackage{latexsym}[1994/06/01 Standard LaTeX package]
516 \end{verbatim}
517 Class files do this as follows:
518 \begin{verbatim}
519 \NeedsTeXFormat{LaTeX2e}
520 \ProvidesClass{<class-name>}[<date> <other information>]
521 \end{verbatim}
522 For example:
523 \begin{verbatim}
524 \NeedsTeXFormat{LaTeX2e}
525 \ProvidesClass{article}[1994/06/01 Standard LaTeX class]
526 \end{verbatim}
527 \NEWdescription{1998/06/19}
528 The \m{date} should be given in the form `\textsc{yyyy/mm/dd}' and
529 must be present if the optional argument is used (this is also true
530 for the |\NeedsTeXFormat| command).
531 Any derivation from this syntax will result in low-level \TeX{}
532 errors---the commands expect a valid syntax to speed up the daily
533 usage of the package or class and make no provision for the case that
534 the developer made a mistake!
536 This date is checked whenever a user specifies a date in their
537 |\documentclass| or |\usepackage| command. For example, if you wrote:
538 \begin{verbatim}
539 \documentclass{article}[1995/12/23]
540 \end{verbatim}
541 then users at a different location
542 would get a warning that their copy of |article| was out of
543 date.
545 The description of a class is displayed when the class is used. The
546 description of a package is put into the log file. These descriptions
547 are also displayed by the |\listfiles| command. The phrase
548 \texttt{Standard LaTeX} \textbf{must not} be used in the identification
549 banner of any file other than those in the standard \LaTeX{}
550 distribution.
553 \subsection{Using classes and packages}
555 The first major difference between \LaTeX~2.09 style files and
556 \LaTeXe{} packages and classes is that \LaTeXe{} supports
557 \emph{modularity}, in the sense of building files from small
558 building-blocks rather than using large single files.
560 A \LaTeX{} package or class can load a package as follows:
561 \begin{verbatim}
562 \RequirePackage[<options>]{<package>}[<date>]
563 \end{verbatim}
564 For example:
565 \begin{verbatim}
566 \RequirePackage{ifthen}[1994/06/01]
567 \end{verbatim}
568 This command has the same syntax as the author command |\usepackage|.
569 It allows packages or classes to use features provided by other
570 packages. For example, by loading the |ifthen| package, a package
571 writer can use the `if\dots then\dots else\dots' commands provided
572 by that package.
574 A \LaTeX{} class can load one other class as follows:
575 \begin{verbatim}
576 \LoadClass[<options>]{<class-name>}[<date>]
577 \end{verbatim}
578 For example:
579 \begin{verbatim}
580 \LoadClass[twocolumn]{article}
581 \end{verbatim}
582 This command has the same syntax as the author command |\documentclass|.
583 It allows classes to be based on the syntax and appearance of another
584 class. For example, by loading the |article| class, a class writer
585 only has to change the bits of |article| they don't like, rather than
586 writing a new class from scratch.
588 \NEWfeature{1995/12/01}
589 The following commands can be used in the common case that you want to
590 simply load a class or package file with exactly those options that
591 are being used by the current class.
592 \begin{verbatim}
593 \LoadClassWithOptions{<class-name>}[<date>]
594 \RequirePackageWithOptions{<package>}[<date>]
595 \end{verbatim}
596 For example:
597 \begin{verbatim}
598 \LoadClassWithOptions{article}
599 \RequirePackageWithOptions{graphics}[1995/12/01]
600 \end{verbatim}
602 \subsection{Declaring options}
604 \NEWdescription{1998/12/01}
605 The other major difference between \LaTeX~2.09 styles and \LaTeXe{}
606 packages and classes is in option handling. Packages and classes can
607 now declare options and these can be specified by authors; for
608 example, the |twocolumn| option is declared by the |article| class.
609 Note that the name of an option should contain only those characters
610 allowed in a `\LaTeX{} name'; in particular it must not contain any
611 control sequences.
613 An option is declared as follows:
614 \begin{verbatim}
615 \DeclareOption{<option>}{<code>}
616 \end{verbatim}
617 For example, the |dvips| option (slightly simplified)
618 to the |graphics| package is implemented as:
619 \begin{verbatim}
620 \DeclareOption{dvips}{\input{dvips.def}}
621 \end{verbatim}
622 This means that when an author writes |\usepackage[dvips]{graphics}|,
623 the file |dvips.def| is loaded. As another example, the |a4paper|
624 option is declared in the |article| class to set the |\paperheight|
625 and |\paperwidth| lengths:
626 \begin{verbatim}
627 \DeclareOption{a4paper}{%
628 \setlength{\paperheight}{297mm}%
629 \setlength{\paperwidth}{210mm}%
631 \end{verbatim}
632 Sometimes a user will request an option which the class
633 or package has not explicitly declared. By default this will produce
634 a warning (for classes) or error (for packages); this behaviour
635 can be altered as follows:
636 \begin{verbatim}
637 \DeclareOption*{<code>}
638 \end{verbatim}
639 For example, to make the package |fred| produce a warning rather than
640 an error for unknown options, you could specify:
641 \begin{verbatim}
642 \DeclareOption*{%
643 \PackageWarning{fred}{Unknown option `\CurrentOption'}%
645 \end{verbatim}
646 Then, if an author writes |\usepackage[foo]{fred}|, they will get a
647 warning \texttt{Package fred Warning: Unknown option `foo'.} As
648 another example, the |fontenc| package tries to load a file
649 |<ENC>enc.def| whenever the \m{ENC} option is used. This
650 can be done by writing:
651 \begin{verbatim}
652 \DeclareOption*{%
653 \input{\CurrentOption enc.def}%
655 \end{verbatim}
656 \NEWdescription{1998/12/01}
657 It is possible to pass options on to another package or class, using
658 the command |\PassOptionsToPackage| or |\PassOptionsToClass| (note
659 that this is a specialised operation that works only for option
660 names). For example, to pass every unknown option on to the |article|
661 class, you can use:
662 \begin{verbatim}
663 \DeclareOption*{%
664 \PassOptionsToClass{\CurrentOption}{article}%
666 \end{verbatim}
667 If you do this then you should make sure you load the class at some
668 later point, otherwise the options will never be processed!
670 So far, we have explained only how to declare options, not how to
671 execute them. To process the options with which the file was called,
672 you should use:
673 \begin{verbatim}
674 \ProcessOptions\relax
675 \end{verbatim}
676 This executes the \m{code} for each option that was both specified and
677 declared (see Section~\ref{Sec:commands.options} for details of how
678 this is done).
680 For example, if the |jane| package file contains:
681 \begin{verbatim}
682 \DeclareOption{foo}{\typeout{Saw foo.}}
683 \DeclareOption{baz}{\typeout{Saw baz.}}
684 \DeclareOption*{\typeout{What's \CurrentOption?}}
685 \ProcessOptions\relax
686 \end{verbatim}
687 and an author writes |\usepackage[foo,bar]{jane}|, then they will see
688 the messages \texttt{Saw foo.} and \texttt{What's bar?}
690 \subsection{A minimal class file}
692 Most of the work of a class or package is in defining new commands, or
693 changing the appearance of documents. This is done in the body of the
694 package, using commands such as |\newcommand| or |\setlength|.
696 \LaTeXe{} provides several new commands to help class and package
697 writers; these are described in detail in Section~\ref{Sec:commands}.
699 There are four things that every class file \emph{must} contain: these
700 are a definition of |\normalsize|, values for |\textwidth| and
701 |\textheight| and a specification for page-numbering. So a minimal
702 document class file\footnote {This class is now in the standard
703 distribution, as \texttt{minimal.cls}.} looks like this:
704 \begin{verbatim}
705 \NeedsTeXFormat{LaTeX2e}
706 \ProvidesClass{minimal}[1995/10/30 Standard LaTeX minimal class]
707 \renewcommand{\normalsize}{\fontsize{10pt}{12pt}\selectfont}
708 \setlength{\textwidth}{6.5in}
709 \setlength{\textheight}{8in}
710 \pagenumbering{arabic} % needed even though this class will
711 % not show page numbers
712 \end{verbatim}
713 However, this class file will not support footnotes, marginals,
714 floats, etc., nor will it provide any of the 2-letter font commands
715 such as |\rm|; thus most classes will contain more than this minimum!
717 \subsection{Example: a local letter class}
719 A company may have its own letter class, for setting letters in the
720 company style. This section shows a simple implementation of such a
721 class, although a real class would need more structure.
723 The class begins by announcing itself as |neplet.cls|.
724 \begin{verbatim}
725 \NeedsTeXFormat{LaTeX2e}
726 \ProvidesClass{neplet}[1995/04/01 NonExistent Press letter class]
727 \end{verbatim}
728 Then this next bit passes any options on to the |letter| class, which
729 is loaded with the |a4paper| option.
730 \begin{verbatim}
731 \DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}
732 \ProcessOptions\relax
733 \LoadClass[a4paper]{letter}
734 \end{verbatim}
735 In order to use the company letter head, it redefines the
736 |firstpage| page style: this is the page style that is used on
737 the first page of letters.
738 \begin{verbatim}
739 \renewcommand{\ps@firstpage}{%
740 \renewcommand{\@oddhead}{<letterhead goes here>}%
741 \renewcommand{\@oddfoot}{<letterfoot goes here>}%
743 \end{verbatim}
744 And that's it!
746 \subsection{Example: a newsletter class}
748 A simple newsletter can be typeset with \LaTeX{}, using a variant of the
749 |article| class.
750 The class begins by announcing itself as |smplnews.cls|.
751 \begin{verbatim}
752 \NeedsTeXFormat{LaTeX2e}
753 \ProvidesClass{smplnews}[1995/04/01 The Simple News newsletter class]
755 \newcommand{\headlinecolor}{\normalcolor}
756 \end{verbatim}
757 It passes most specified options on to the |article| class: apart from
758 the |onecolumn| option, which is switched off, and the |green| option,
759 which sets the headline in green.
760 \begin{verbatim}
761 \DeclareOption{onecolumn}{\OptionNotUsed}
762 \DeclareOption{green}{\renewcommand{\headlinecolor}{\color{green}}}
764 \DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
766 \ProcessOptions\relax
767 \end{verbatim}
768 It then loads the class |article| with the option |twocolumn|.
769 \begin{verbatim}
770 \LoadClass[twocolumn]{article}
771 \end{verbatim}
772 Since the newsletter is to be printed in colour, it now loads the
773 |color| package. The class does not specify a device driver option
774 since this should be specified by the user of the |smplnews| class.
775 \begin{verbatim}
776 \RequirePackage{color}
777 \end{verbatim}
778 The class then redefines |\maketitle| to produce the title in 72pt
779 Helvetica bold oblique, in the appropriate colour.
780 \begin{verbatim}
781 \renewcommand{\maketitle}{%
782 \twocolumn[%
783 \fontsize{72}{80}\fontfamily{phv}\fontseries{b}%
784 \fontshape{sl}\selectfont\headlinecolor
785 \@title
788 \end{verbatim}
789 It redefines |\section| and switches off section numbering.
790 \begin{verbatim}
791 \renewcommand{\section}{%
792 \@startsection
793 {section}{1}{0pt}{-1.5ex plus -1ex minus -.2ex}%
794 {1ex plus .2ex}{\large\sffamily\slshape\headlinecolor}%
797 \setcounter{secnumdepth}{0}
798 \end{verbatim}
799 It also sets the three essential things.
800 \begin{verbatim}
801 \renewcommand{\normalsize}{\fontsize{9}{10}\selectfont}
802 \setlength{\textwidth}{17.5cm}
803 \setlength{\textheight}{25cm}
804 \end{verbatim}
805 In practice, a class would need more than this: it would provide
806 commands for issue numbers, authors of articles, page styles and so
807 on; but this skeleton gives a start. The |ltnews| class file is not
808 much more complex than this one.
810 \section{Commands for class and package writers}
811 \label{Sec:commands}
813 This section describes briefly each of the new commands for class and
814 package writers. To find out about other aspects of the new system,
815 you should also read \emph{\LaTeXbook}, \emph{\LaTeXcomp} and
816 \emph{\usrguide}.
818 \subsection{Identification}
820 The first group of commands discussed here are those used
821 to identify your class or package file.
823 \begin{decl}
824 |\NeedsTeXFormat| \arg{format-name} \oarg{release-date}
825 \end{decl}
826 This command tells \TeX{} that this file should be processed using a
827 format with name \m{format-name}. You can use the optional argument
828 \m{release-date} to further specify the earliest release date of the
829 format that is needed. When the release date of the format is older
830 than the one specified a warning will be generated. The standard
831 \m{format-name} is \texttt{LaTeX2e}. The date, if present, must be in
832 the form \textsc{yyyy/mm/dd}.
834 Example:
835 \begin{verbatim}
836 \NeedsTeXFormat{LaTeX2e}[1994/06/01]
837 \end{verbatim}
839 \begin{decl}
840 |\ProvidesClass| \arg{class-name} \oarg{release-info} \\
841 |\ProvidesPackage| \arg{package-name} \oarg{release-info}
842 \end{decl}
843 This declares that the current file contains the definitions for the
844 document class \m{class-name} or package \m{package-name}.
846 The optional \m{release-info}, if used, must contain:
847 \begin{itemize}
848 \item the release date of
849 this version of the file, in the form \textsc{yyyy/mm/dd};
850 \item optionally followed by a space and a short description, possibly
851 including a version number.
852 \end{itemize}
853 The above syntax must be followed exactly so that this information
854 can be used by |\LoadClass| or |\documentclass| (for classes) or
855 |\RequirePackage| or |\usepackage| (for packages) to test that the
856 release is not too old.
858 The whole of this \m{release-info} information is displayed by
859 |\listfiles| and should therefore not be too long.
861 Example:
862 \begin{verbatim}
863 \ProvidesClass{article}[1994/06/01 v1.0 Standard LaTeX class]
864 \ProvidesPackage{ifthen}[1994/06/01 v1.0 Standard LaTeX package]
865 \end{verbatim}
867 \begin{decl}
868 |\ProvidesFile| \arg{file-name} \oarg{release-info}
869 \end{decl}
870 This is similar to the two previous commands except that here the full
871 filename, including the extension, must be given. It is used for
872 declaring any files other than main class and package files.
874 Example:
875 \begin{verbatim}
876 \ProvidesFile{T1enc.def}[1994/06/01 v1.0 Standard LaTeX file]
877 \end{verbatim}
879 Note that the phrase \texttt{Standard LaTeX} \textbf{must not} be used
880 in the identification banner of any file other than those in the
881 standard \LaTeX{} distribution.
883 \subsection{Loading files}
884 \label{Sec:loadf}
886 \NEWfeature{1995/12/01}
887 This group of commands can be used to create your own document class or
888 package by building on existing classes or packages.
889 \begin{decl}
890 |\RequirePackage| \oarg{options-list} \arg{package-name}
891 \oarg{release-info}\\
892 |\RequirePackageWithOptions| \arg{package-name}
893 \oarg{release-info}
894 \end{decl}
895 Packages and classes should use these commands to load other packages.
897 The use of |\RequirePackage| is the same as the author command
898 |\usepackage|.
900 Examples:
901 \begin{verbatim}
902 \RequirePackage{ifthen}[1994/06/01]
903 \RequirePackageWithOptions{graphics}[1995/12/01]
904 \end{verbatim}
906 \begin{decl}
907 |\LoadClass| \oarg{options-list} \arg{class-name}
908 \oarg{release-info}\\
909 |\LoadClassWithOptions| \arg{class-name}
910 \oarg{release-info}
911 \end{decl}
912 \NEWfeature{1995/12/01}
913 These commands are for use \emph{only} in class files, they cannot be
914 used in packages files;
915 they can be used at most once within a class file.
917 The use of |\LoadClass| is the same as
918 the use of |\documentclass| to load a class file.
920 Examples:
921 \begin{verbatim}
922 \LoadClass{article}[1994/06/01]
923 \LoadClassWithOptions{article}[1995/12/01]
924 \end{verbatim}
926 \NEWfeature{1995/12/01}
927 The two |WithOptions| versions simply load the class (or package) file
928 with exactly those options that are being used by the current file
929 (class or package). See below, in \ref{Sec:opmove}, for further
930 discussion of their use.
933 \subsection{Option declaration}
934 \label{Sec:commands.options.dec}
936 \NEWdescription{1998/12/01}
937 The following commands deal with the declaration and handling of
938 options to document classes and packages. Every option name must be a
939 `\LaTeX{} name'.
941 There are some commands designed especially for use within the
942 \m{code} argument of these commands (see below).
944 \begin{decl}
945 |\DeclareOption| \arg{option-name} \arg{code}
946 \end{decl}
947 This makes \m{option-name} a `declared option' of the class or package
948 in which it is put.
950 The \m{code} argument contains the code to be executed if that option
951 is specified for the class or package; it can contain any valid
952 \LaTeXe{} construct.
954 Example:
955 \begin{verbatim}
956 \DeclareOption{twoside}{\@twosidetrue}
957 \end{verbatim}
959 \begin{decl}
960 |\DeclareOption*| \arg{code}
961 \end{decl}
962 This declares the \m{code} to be executed for every option which is
963 specified for, but otherwise not explicitly declared by, the class or
964 package; this code is called the `default option code' and it can
965 contain any valid \LaTeXe{} construct.
967 If a class file contains no |\DeclareOption*| then, by default, all
968 specified but undeclared options for that class will be silently
969 passed to all packages (as will the specified and declared options for
970 that class).
972 If a package file contains no |\DeclareOption*| then, by default, each
973 specified but undeclared option for that package will produce an error.
976 \subsection{Commands within option code}
977 \label{Sec:within.code}
979 These two commands can be used only within the \m{code} argument of
980 either |\DeclareOption| or |\DeclareOption*|. Other commands commonly
981 used within these arguments can be found in the next few subsections.
983 \begin{decl}
984 |\CurrentOption|
985 \end{decl}
986 This expands to the name of the current option.
988 \begin{decl}
989 |\OptionNotUsed|
990 \end{decl}
991 This causes the current option to
992 be added to the list of `unused options'.
994 \NEWfeature{1995/06/01}
995 You can now include hash marks (\texttt{\#}) within these \m{code}
996 arguments without special treatment (formerly, it had been
997 necessary to double them).
999 \subsection{Moving options around}
1000 \label{Sec:opmove}
1002 These two commands are also very useful within the \m{code} argument
1003 of |\DeclareOption| or |\DeclareOption*|:
1004 \begin{decl}
1005 |\PassOptionsToPackage| \arg{options-list} \arg{package-name}\\
1006 |\PassOptionsToClass| \arg{options-list} \arg{class-name}
1007 \end{decl}
1008 The command |\PassOptionsToPackage| passes the option names in
1009 \m{options-list} to package \m{package-name}.
1010 This means that it adds the \m{option-list} to the
1011 list of options used by any future |\RequirePackage| or |\usepackage|
1012 command for package \m{package-name}.
1014 Example:
1015 \begin{verbatim}
1016 \PassOptionsToPackage{foo,bar}{fred}
1017 \RequirePackage[baz]{fred}
1018 \end{verbatim}
1019 is the same as:
1020 \begin{verbatim}
1021 \RequirePackage[foo,bar,baz]{fred}
1022 \end{verbatim}
1024 Similarly, |\PassOptionsToClass| may be used in a class file to pass
1025 options to another class to be loaded with |\LoadClass|.
1027 \NEWdescription{1995/12/01}
1028 The effects and use of these two commands should be contrasted with
1029 those of the following two (documented above, in \ref{Sec:loadf}):
1030 \begin{verbatim}
1031 \LoadClassWithOptions
1032 \RequirePackageWithOptions
1033 \end{verbatim}
1034 The command |\RequirePackageWithOptions| is similar to
1035 |\RequirePackage|, but it always loads the required package with
1036 exactly the same option list as that being used by the current class
1037 or package, rather than with any option explicitly supplied or passed
1038 on by |\PassOptionsToPackage|.
1040 The main purpose of |\LoadClassWithOptions| is to allow one class to
1041 simply build on another, for example:
1042 \begin{verbatim}
1043 \LoadClassWithOptions{article}
1044 \end{verbatim}
1045 This should be compared with the slightly different construction
1046 \begin{verbatim}
1047 \DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
1048 \ProcessOptions\relax
1049 \LoadClass{article}
1050 \end{verbatim}
1051 As used above, the effects are more or less the same, but the first is
1052 a lot less to type; also the |\LoadClassWithOptions| method runs
1053 slightly quicker.
1055 If, however, the class declares options of its own then
1056 the two constructions are different. Compare, for example:
1057 \begin{verbatim}
1058 \DeclareOption{landscape}{\@landscapetrue}
1059 \ProcessOptions\relax
1060 \LoadClassWithOptions{article}
1061 \end{verbatim}
1062 with:
1063 \begin{verbatim}
1064 \DeclareOption{landscape}{\@landscapetrue}
1065 \DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
1066 \ProcessOptions\relax
1067 \LoadClass{article}
1068 \end{verbatim}
1069 In the first example, the \textsf{article} class will be loaded with
1070 option |landscape| precisely when the current class is called with
1071 this option. By contrast, in the second example it will never be
1072 called with option \texttt{landscape} as in that case \textsf{article}
1073 is passed options only by the default option handler, but this handler
1074 is not used for |landscape| because that option is explicitly
1075 declared.
1077 \subsection{Delaying code}
1078 \label{Sec:delays}
1080 These first two commands are also intended primarily for use within
1081 the \m{code} argument of |\DeclareOption| or |\DeclareOption*|.
1083 \begin{decl}
1084 |\AtEndOfClass| \arg{code}\\
1085 |\AtEndOfPackage| \arg{code}
1086 \end{decl}
1087 These commands declare \m{code} that is saved away internally and then
1088 executed after processing the whole of the current class or package
1089 file.
1091 Repeated use of these commands is permitted: the code in the
1092 arguments is stored (and later executed) in the order of their
1093 declarations.
1095 \begin{decl}
1096 |\AtBeginDocument| \arg{code}\\
1097 |\AtEndDocument| \arg{code}
1098 \end{decl}
1099 These commands declare \m{code} to be saved internally and executed
1100 while \LaTeX{} is executing |\begin{document}| or |\end{document}|.
1102 The \m{code} specified in the argument to |\AtBeginDocument| is
1103 executed near the end of the |\begin{document}| code, \emph{after} the
1104 font selection tables have been set up. It is therefore a useful
1105 place to put code which needs to be executed after everything has been
1106 prepared for typesetting and when the normal font for the document is
1107 the current font.
1109 \NEWdescription{1995/12/01}
1110 The |\AtBeginDocument| hook should not be used for code that does any
1111 typesetting since the typeset result would be unpredictable.
1113 The \m{code} specified in the argument to |\AtEndDocument| is
1114 executed at the beginning of the |\end{document}| code,
1115 \emph{before} the final page is finished and before any leftover
1116 floating environments are processed. If some of the \m{code} is to be
1117 executed after these two processes, you should include a |\clearpage|
1118 at the appropriate point in \m{code}.
1120 Repeated use of these commands is permitted: the code in the
1121 arguments is stored (and later executed) in the order of their
1122 declarations.
1124 \begin{decl}[1994/12/01]
1125 |\AtBeginDvi| \arg{specials}
1126 \end{decl}
1127 These commands save in a box register things which are written to the
1128 |.dvi| file at the beginning of the `shipout' of the first page of the
1129 document.
1131 This should not be used for anything that will add any typeset
1132 material to the |.dvi| file.
1134 Repeated use of this command is permitted.
1137 \subsection{Option processing}
1138 \label{Sec:commands.options}
1140 \begin{decl}
1141 |\ProcessOptions|
1142 \end{decl}
1143 This command executes the \m{code} for each selected option.
1145 We shall first describe how |\ProcessOptions| works in a package file,
1146 and then how this differs in a class file.
1148 To understand in detail what |\ProcessOptions| does in a package file,
1149 you have to know the difference between \emph{local} and \emph{global}
1150 options.
1151 \begin{itemize}
1152 \item \textbf{Local options} are those which have been explicitly
1153 specified for this particular package in the \m{options} argument of
1154 any of these:
1155 \begin{quote}
1156 |\PassOptionsToPackage{<options>}| \ |\usepackage[<options>]|\\
1157 |\RequirePackage[<options>]|
1158 \end{quote}
1159 \item \textbf{Global options} are any other options that are specified
1160 by the author in the \m{options} argument of
1161 |\documentclass[<options>]|.
1162 \end{itemize}
1163 For example, suppose that a document begins:
1164 \begin{verbatim}
1165 \documentclass[german,twocolumn]{article}
1166 \usepackage{gerhardt}
1167 \end{verbatim}
1168 whilst package |gerhardt| calls package |fred| with:
1169 \begin{verbatim}
1170 \PassOptionsToPackage{german,dvips,a4paper}{fred}
1171 \RequirePackage[errorshow]{fred}
1172 \end{verbatim}
1173 then:
1174 \begin{itemize}
1175 \item |fred|'s local options are |german|, |dvips|, |a4paper|
1176 and |errorshow|;
1177 \item |fred|'s only global option is |twocolumn|.
1178 \end{itemize}
1180 When |\ProcessOptions| is called, the following happen.
1181 \begin{itemize}
1182 \item \emph{First}, for each option so far declared in |fred.sty|
1183 by |\DeclareOption|, it looks to see if that option is either a
1184 global or a local option for |fred|: if it is then the corresponding
1185 code is executed.
1187 This is done in the order in which these options
1188 were declared in |fred.sty|.
1189 \item \emph{Then}, for each remaining \emph{local} option, the command
1190 |\ds@<option>| is executed if it has been defined somewhere (other
1191 than by a |\DeclareOption|); otherwise, the `default option code' is
1192 executed. If no default option code has been declared then an error
1193 message is produced.
1195 This is done in the order in which these
1196 options were specified.
1197 \end{itemize}
1198 Throughout this process, the system ensures that the code declared for
1199 an option is executed at most once.
1201 Returning to the example, if |fred.sty| contains:
1202 \begin{verbatim}
1203 \DeclareOption{dvips}{\typeout{DVIPS}}
1204 \DeclareOption{german}{\typeout{GERMAN}}
1205 \DeclareOption{french}{\typeout{FRENCH}}
1206 \DeclareOption*{\PackageWarning{fred}{Unknown `\CurrentOption'}}
1207 \ProcessOptions\relax
1208 \end{verbatim}
1209 then the result of processing this document will be:
1210 \begin{verbatim}
1211 DVIPS
1212 GERMAN
1213 Package fred Warning: Unknown `a4paper'.
1214 Package fred Warning: Unknown `errorshow'.
1215 \end{verbatim}
1216 Note the following:
1218 \begin{itemize}
1219 \item the code for the |dvips| option is executed before that for the
1220 |german| option, because that is the order in which they are declared
1221 in |fred.sty|;
1222 \item the code for the |german| option is executed only once, when the
1223 declared options are being processed;
1224 \item the |a4paper| and |errorshow| options produce the warning from
1225 the code declared by |\DeclareOption*| (in the order in which they
1226 were specified), whilst the |twocolumn| option does not: this is
1227 because |twocolumn| is a global option.
1228 \end{itemize}
1230 In a class file, |\ProcessOptions| works in the same way, except
1231 that: \emph{all} options are local; and the default value for
1232 |\DeclareOption*| is |\OptionNotUsed| rather than an error.
1234 \NEWdescription{1995/12/01}
1235 Note that, because |\ProcessOptions| has a |*|-form, it is wise to
1236 follow the non-star form with |\relax|, as in the previous examples,
1237 since this prevents unnecessary look ahead and possibly misleading
1238 error messages being issued.
1240 \begin{decl}
1241 |\ProcessOptions*| \\
1242 |\@options|
1243 \end{decl}
1244 This is like |\ProcessOptions| but it executes the options in the
1245 order specified in the calling commands, rather than in the order of
1246 declaration in the class or package. For a package this means that the
1247 global options are processed first.
1249 The |\@options| command from \LaTeX~2.09 has been made equivalent to
1250 this in order to ease the task of updating old document styles to
1251 \LaTeXe{} class files.
1253 \begin{decl}
1254 |\ExecuteOptions| \arg{options-list}
1255 \end{decl}
1257 For each option in the \m{options-list}, in order,
1258 this command simply executes the command
1259 |\ds@<option>| (if this command is not defined, then that option is
1260 silently ignored).
1262 It can be used to provide a `default option list' just before
1263 |\ProcessOptions|. For example, suppose that in a class file you want
1264 to set up the default design to be: two-sided printing; 11pt fonts;
1265 in two columns. Then it could specify:
1266 \begin{verbatim}
1267 \ExecuteOptions{11pt,twoside,twocolumn}
1268 \end{verbatim}
1271 \subsection{Safe file commands}
1273 These commands deal with file input; they ensure that the non-existence
1274 of a requested file can be handled in a user-friendly way.
1276 \begin{decl}
1277 |\IfFileExists| \arg{file-name} \arg{true} \arg{false}
1278 \end{decl}
1279 If the file exists then the code specified in \m{true} is
1280 executed.
1282 If the file does not exist then the code specified in \m{false} is
1283 executed.
1285 This command does \emph{not} input the file.
1287 \begin{decl}
1288 |\InputIfFileExists| \arg{file-name} \arg{true} \arg{false}
1289 \end{decl}
1290 This inputs the file \m{file-name} if it exists and, immediately
1291 before the input, the code specified in \m{true} is executed.
1293 If the file does not exist then the code specified in \m{false} is
1294 executed.
1296 It is implemented using |\IfFileExists|.
1299 \subsection{Reporting errors, etc}
1301 These commands should be used by third party classes and packages to
1302 report errors, or to provide information to authors.
1304 \begin{decl}
1305 |\ClassError| \arg{class-name} \arg{error-text} \arg{help-text}\\
1306 |\PackageError| \arg{package-name} \arg{error-text} \arg{help-text}
1307 \end{decl}
1308 These produce an error message. The \m{error-text} is displayed and the
1309 |?| error prompt is shown. If the user types |h|, they will be shown
1310 the \m{help-text}.
1312 Within the \m{error-text} and \m{help-text}: |\protect| can be used to
1313 stop a command from expanding; |\MessageBreak| causes a line-break;
1314 and |\space| prints a space.
1316 Note that the \m{error-text} will have a full stop added to it, so do
1317 not put one into the argument.
1319 For example:
1320 \begin{verbatim}
1321 \newcommand{\foo}{FOO}
1322 \PackageError{ethel}{%
1323 Your hovercraft is full of eels,\MessageBreak
1324 and \protect\foo\space is \foo
1326 Oh dear! Something's gone wrong.\MessageBreak
1327 \space \space Try typing \space <<return>>
1328 \space to proceed, ignoring \protect\foo.
1330 \end{verbatim}
1331 produces this display:
1332 \begin{verbatim}
1333 ! Package ethel Error: Your hovercraft is full of eels,
1334 (ethel) and \foo is FOO.
1336 See the ethel package documentation for explanation.
1337 \end{verbatim}
1338 If the user types |h|, this will be shown:
1339 \begin{verbatim}
1340 Oh dear! Something's gone wrong.
1341 Try typing <<return>> to proceed, ignoring \foo.
1342 \end{verbatim}
1344 \begin{decl}
1345 |\ClassWarning| \arg{class-name} \arg{warning-text}\\
1346 |\PackageWarning| \arg{package-name} \arg{warning-text}\\
1347 |\ClassWarningNoLine| \arg{class-name} \arg{warning-text}\\
1348 |\PackageWarningNoLine| \arg{package-name} \arg{warning-text}\\
1349 |\ClassInfo| \arg{class-name} \arg{info-text}\\
1350 |\PackageInfo| \arg{package-name} \arg{info-text}
1351 \end{decl}
1352 The four |Warning| commands are similar to the error commands, except
1353 that they produce only a warning on the screen, with no error prompt.
1355 The first two, |Warning| versions, also show the line number where the
1356 warning occurred, whilst the second two, |WarningNoLine| versions, do
1357 not.
1359 The two |Info| commands are similar except that they log the
1360 information only in the transcript file, including the line number.
1361 There are no |NoLine| versions of these two.
1363 Within the \m{warning-text} and \m{info-text}: |\protect| can be used to
1364 stop a command from expanding; |\MessageBreak| causes a line-break;
1365 and |\space| prints a space.
1366 Also, these should not end with a full stop as one is
1367 automatically added.
1370 \subsection{Defining commands}
1371 \label{Sec:commands.define}
1373 \LaTeXe{} provides some extra methods of (re)defining commands that are
1374 intended for use in class and package files.
1376 \NEWfeature{1994/12/01}
1377 The \texttt{*}-forms of these commands should be used to define
1378 commands that are not, in \TeX{} terms, long. This can be useful for
1379 error-trapping with commands whose arguments are not intended to
1380 contain whole paragraphs of text.
1382 \begin{decl}
1383 |\DeclareRobustCommand| \arg{cmd} \oarg{num} \oarg{default}
1384 \arg{definition}\\
1385 |\DeclareRobustCommand*| \arg{cmd} \oarg{num} \oarg{default}
1386 \arg{definition}
1387 \end{decl}
1388 This command takes the same arguments as |\newcommand| but it declares
1389 a robust command, even if some code within the \m{definition} is
1390 fragile. You can use this command to define new robust commands, or
1391 to redefine existing commands and make them robust. A log is put into
1392 the transcript file if a command is redefined.
1394 For example, if |\seq| is defined as follows:
1395 \begin{verbatim}
1396 \DeclareRobustCommand{\seq}[2][n]{%
1397 \ifmmode
1398 #1_{1}\ldots#1_{#2}%
1399 \else
1400 \PackageWarning{fred}{You can't use \protect\seq\space in text}%
1403 \end{verbatim}
1404 Then the command |\seq| can be used in moving arguments, even though
1405 |\ifmmode| cannot, for example:
1406 \begin{verbatim}
1407 \section{Stuff about sequences $\seq{x}$}
1408 \end{verbatim}
1410 Note also that there is no need to put a |\relax| before the
1411 |\ifmmode| at the beginning of the definition; this is because the
1412 protection given by this |\relax| against expansion at the wrong time
1413 will be provided internally.
1415 \begin{decl}
1416 |\CheckCommand| \arg{cmd} \oarg{num} \oarg{default}
1417 \arg{definition}\\
1418 |\CheckCommand*| \arg{cmd} \oarg{num} \oarg{default}
1419 \arg{definition}
1420 \end{decl}
1421 This takes the same arguments as |\newcommand| but, rather than define
1422 \m{cmd}, it just checks that the current definition of \m{cmd} is
1423 exactly as given by \m{definition}. An error is raised if these
1424 definitions differ.
1426 This command is useful for checking the state of the system before
1427 your package starts altering the definitions of commands. It allows
1428 you to check, in particular, that no other package has redefined the
1429 same command.
1431 \subsection{Moving arguments}
1433 \NEWdescription{1994/12/01}
1434 The setting of protect whilst processing (i.e.~moving) moving arguments
1435 has been reimplemented, as has the method of writing information from
1436 the |.aux| file to other files such as the |.toc| file. Details can
1437 be found in the file |ltdefns.dtx|.
1439 We hope that these changes will not affect many packages.
1441 \section{Miscellaneous commands, etc}
1442 \label{Sec:commands.misc}
1444 \subsection{Layout parameters}
1446 \begin{decl}
1447 |\paperheight|\\
1448 |\paperwidth|
1449 \end{decl}
1450 These two parameters are usually set by the class to be the size of
1451 the paper being used. This should be actual paper size, unlike
1452 |\textwidth| and |\textheight| which are the size of the main text
1453 body within the margins.
1456 \subsection{Case changing}
1457 \label{sec:case}
1459 \begin{decl}
1460 |\MakeUppercase| \arg{text} \\
1461 |\MakeLowercase| \arg{text}
1462 \end{decl}
1464 \NEWfeature{1995/06/01}
1465 \TeX{} provides two primitives |\uppercase| and |\lowercase| for
1466 changing the case of text. These are sometimes used in document
1467 classes, for example to set information in running heads in all
1468 capitals.
1470 Unfortunately, these \TeX{} primitives do not change the case of
1471 characters accessed by commands like |\ae| or |\aa|. To overcome this
1472 problem, \LaTeX{} provides two new commands |\MakeUppercase| and
1473 |\MakeLowercase| to do this.
1475 For example:
1476 \begin{quotation}
1477 \begin{tabular}{rl}
1478 |\uppercase{aBcD\ae\AA\ss\OE}| & \uppercase{aBcD\ae\AA\ss\OE}\\
1479 |\lowercase{aBcD\ae\AA\ss\OE}| & \lowercase{aBcD\ae\AA\ss\OE}\\
1480 |\MakeUppercase{aBcD\ae\AA\ss\OE}| &
1481 \MakeUppercase{aBcD\ae\AA\ss\OE}\\
1482 |\MakeLowercase{aBcD\ae\AA\ss\OE}| & \MakeLowercase{aBcD\ae\AA\ss\OE}
1483 \end{tabular}
1484 \end{quotation}
1486 The commands |\MakeUppercase| and |\MakeLowercase| themselves are
1487 robust, but they have moving arguments.
1489 The commands use the \TeX{} primitives |\uppercase| and |\lowercase|,
1490 and so have a number of unexpected `features'. In particular, they
1491 change the case of everything (except characters in the names of
1492 control-sequences) in their text argument: this includes mathematics,
1493 environment names, and label names.
1495 For example:
1496 \begin{verbatim}
1497 \MakeUppercase{$x+y$ in \ref{foo}}
1498 \end{verbatim}
1499 produces $X+Y$ and the warning:
1500 \begin{verbatim}
1501 LaTeX Warning: Reference `FOO' on page ... undefined on ...
1502 \end{verbatim}
1503 In the long run, we would like to use all-caps fonts rather than any
1504 command like |\MakeUppercase| but this is not possible at the moment
1505 because such fonts do not exist.
1507 \NEWdescription{1995/12/01}
1508 In order that upper/lower-casing will work reasonably well, and in
1509 order to provide any correct hyphenation, \LaTeXe{} \emph{must} use,
1510 throughout a document, the same fixed table for changing case.
1511 The table used is designed for the font encoding |T1|; this works well
1512 with the standard \TeX{} fonts for all Latin alphabets but will cause
1513 problems when using other alphabets.
1515 \subsection{The `openany' option in the `book' class}
1517 \NEWdescription{1996/06/01}
1518 The |openany| option allows chapter and similar openings to
1519 occur on left hand pages. Previously this option affected only
1520 |\chapter| and |\backmatter|. It now also affects
1521 |\part|, |\frontmatter| and |\mainmatter|.
1523 \subsection{Better user-defined math display environments}
1525 \begin{decl}
1526 |\ignorespacesafterend|
1527 \end{decl}
1529 \NEWfeature{1996/12/01}
1530 \NEWdescription{2003/12/01}
1531 Suppose that you want to define an environment for displaying text
1532 that is numbered as an equation. A straightforward way to do this is
1533 as follows:
1534 \begin{verbatim}
1535 \newenvironment{texteqn}
1536 {\begin{equation}
1537 \begin{minipage}{0.9\linewidth}}
1538 {\end{minipage}
1539 \end{equation}}
1540 \end{verbatim}
1541 However, if you have tried this then you will probably have noticed
1542 that it does not work perfectly when used in the middle of a paragraph
1543 because an inter-word space appears at the beginning of the first
1544 line after the environment.
1546 There is now an extra command (with a very long name) available that
1547 you can use to avoid this problem; it should be inserted as shown here:
1548 \begin{verbatim}
1549 \newenvironment{texteqn}
1550 {\begin{equation}
1551 \begin{minipage}{0.9\linewidth}}
1552 {\end{minipage}
1553 \end{equation}
1554 \ignorespacesafterend}
1555 \end{verbatim}
1557 This command may also have other uses.
1559 \subsection{Normalising spacing}
1561 \begin{decl}
1562 |\normalsfcodes|
1563 \end{decl}
1565 \NEWfeature{1997/06/01}
1566 This command should be used to restore the normal settings of the
1567 parameters that affect spacing between words, sentences, etc.
1569 An important use of this feature is to correct a problem, reported by
1570 Donald Arseneau, that punctuation in page headers has always (in all
1571 known \TeX{} formats) been potentially wrong whenever a page break
1572 happens while a local setting of the space codes is in effect. These
1573 space codes are changed by, for example, the command
1574 \verb|\frenchspacing|) and the \textsf{verbatim} environment.
1576 It is normally given the correct definition automatically in
1577 |\begin{document}| and so need not be explicitly set; however, if it
1578 is explicitly made nonempty in a class file then automatic
1579 default setting will be over-ridden.
1582 \section{Upgrading \LaTeX~2.09 classes and packages}
1583 \label{Sec:upgrade}
1585 This section describes the changes you may need to make when you
1586 upgrade an existing \LaTeX{} style to a package or class but we shall
1587 start in optimistic mode.
1589 Many existing style files will run with \LaTeXe{} without any
1590 modification to the file itself. When everything is running OK,
1591 please put a note in the newly created package or class file to record
1592 that it runs with the new standard \LaTeX{}; then distribute it to
1593 your users.
1595 \subsection{Try it first!}
1596 \label{Sec:try-it}
1598 The first thing you should do is to test your style in `compatibility
1599 mode'. The only change you need to make in order to do this is,
1600 possibly, to change the extension of the file to |.cls|: you should
1601 make this change only if your file was used as a main document style.
1602 Now, without any other modifications, run \LaTeXe{} on a document that
1603 uses your file. This assumes that you have a suitable collection of
1604 files that tests all the functionality provided by your style file.
1605 (If you haven't, now is the time to make one!)
1607 You now need to change the test document files so that they are
1608 \LaTeXe{} documents: see \emph{\usrguide} for details of how to do
1609 this and then try them again. You have now tried the test documents
1610 in both \LaTeXe{} native mode and \LaTeX~2.09 compatibility mode.
1612 \subsection{Troubleshooting}
1613 \label{Sec:trouble}
1615 If your file does not work with \LaTeXe{}, there are two likely
1616 reasons.
1617 \begin{itemize}
1618 \item \LaTeX{} now has a robust, well-defined designer's interface for
1619 selecting fonts, which is very different from the \LaTeX~2.09
1620 internals.
1621 \item Your style file may have used some \LaTeX~2.09 internal commands
1622 which have changed, or which have been removed.
1623 \end{itemize}
1625 When you are debugging your file, you will probably need more
1626 information than is normally displayed by \LaTeXe. This is achieved
1627 by resetting the counter |errorcontextlines| from its default value of
1628 $-1$ to a much higher value, e.g.~999.
1630 \subsection{Accommodating compatibility mode}
1632 Sometimes an existing collection of \LaTeX~2.09 documents makes it
1633 inconvenient or impossible to abandon the old commands entirely.
1634 If this is the case, then it is possible to accommodate both conventions
1635 by making special provision for documents processed in compatibility
1636 mode.
1638 \begin{decl}
1639 |\if@compatibility|
1640 \end{decl}
1641 This switch is set when a document begins with |\documentstyle| rather
1642 than |\documentclass|. Appropriate code can be supplied for either
1643 condition, as follows:
1644 \begin{verbatim}
1645 \if@compatibility
1646 <code emulating LaTeX 2.09 behavior>
1647 \else
1648 <code suitable for LaTeX2e>
1650 \end{verbatim}
1653 \subsection{Font commands}
1655 Some font and size commands are now defined by the document class
1656 rather than by the \LaTeX{} kernel. If you are upgrading a
1657 \LaTeX~2.09 document style to a class that does not load one of the
1658 standard classes, then you will probably need to add definitions for
1659 these commands.
1661 \begin{decl}
1662 |\rm| |\sf| |\tt| |\bf| |\it| |\sl| |\sc|
1663 \end{decl}
1664 None of these short-form font selection commands are defined in the
1665 \LaTeXe{} kernel. They are defined by all the standard class files.
1667 If you want to define them in your class file, there are several
1668 reasonable ways to do this.
1670 One possible definition is:
1671 \begin{verbatim}
1672 \newcommand{\rm}{\rmfamily}
1674 \newcommand{\sc}{\scshape}
1675 \end{verbatim}
1676 This would make the font commands orthogonal; for example
1677 |{\bf\it text}| would produce bold italic, thus: \textbf{\textit{text}}.
1678 It will also make them produce an error if used in math mode.
1680 Another possible definition is:
1681 \begin{verbatim}
1682 \DeclareOldFontCommand{\rm}{\rmfamily}{\mathrm}
1684 \DeclareOldFontCommand{\sc}{\scshape}{\mathsc}
1685 \end{verbatim}
1686 This will make |\rm| act like |\rmfamily| in text mode (see above) and
1687 it will make |\rm| select the |\mathrm| math alphabet in math mode.
1689 Thus |${\rm math} = X + 1$| will produce `${\rm math} = X + 1$'.
1691 If you do not want font selection to be orthogonal then you can
1692 follow the standard classes and define:
1693 \begin{verbatim}
1694 \DeclareOldFontCommand{\rm}{\normalfont\rmfamily}{\mathrm}
1696 \DeclareOldFontCommand{\sc}{\normalfont\scshape}{\mathsc}
1697 \end{verbatim}
1698 This means, for example, that |{\bf\it text}| will produce medium
1699 weight (rather than bold) italic, thus: \textit{text}.
1701 \begin{decl}
1702 |\normalsize| \\
1703 |\@normalsize|
1704 \end{decl}
1705 The command |\@normalsize| is retained for compatibility with
1706 \LaTeX~2.09 packages which may have used its value; but redefining it
1707 in a class file will have no effect since it is always reset to have
1708 the same meaning as |\normalsize|.
1710 This means that classes \emph{must} now redefine |\normalsize| rather
1711 than redefining |\@normalsize|; for example (a rather incomplete one):
1712 \begin{verbatim}
1713 \renewcommand{\normalsize}{\fontsize{10}{12}\selectfont}
1714 \end{verbatim}
1715 Note that |\normalsize| is defined by the \LaTeX{} kernel to be an
1716 error message.
1718 \begin{decl}
1719 |\tiny| |\footnotesize| |\small| |\large|\\
1720 |\Large| |\LARGE| |\huge| |\Huge|
1721 \end{decl}
1722 None of these other `standard' size-changing commands are defined in
1723 the kernel: each needs to be defined in a class file if you need it.
1724 They are all defined by the standard classes.
1726 This means you should use |\renewcommand| for |\normalsize| and
1727 |\newcommand| for the other size-changing commands.
1730 \subsection{Obsolete commands}
1732 Some packages will not work with \LaTeXe{}, normally because they relied
1733 on an internal \LaTeX{} command which was never supported and has now
1734 changed, or been removed.
1736 In many cases there will now be a robust, high-level means of
1737 achieving what previously required low-level commands. Please consult
1738 Section~\ref{Sec:commands} to see if you can now use the \LaTeXe{}
1739 class and package writers commands.
1741 Also, of course, if your package or class redefined any of the kernel
1742 commands (i.e.~those defined in the files |latex.tex|, |slitex.tex|,
1743 |lfonts.tex|, |sfonts.tex|) then you will need to check it very
1744 carefully against the new kernel in case the implementation has
1745 changed or the command no longer exists in the \LaTeX2e{} kernel.
1747 Too many of the internal commands of \LaTeX~2.09 have been
1748 re-implemented or removed to be able to list them all here. You must
1749 check any that you have used or changed.
1751 We shall, however, list some of the more important commands which are
1752 no longer supported.
1754 \begin{decl}
1755 |\tenrm| |\elvrm| |\twlrm| \dots\\
1756 |\tenbf| |\elvbf| |\twlbf| \dots\\
1757 |\tensf| |\elvsf| |\twlsf| \dots\\
1758 \qquad$\vdots$
1759 \end{decl}
1760 The (approximately) seventy internal commands of this form are no longer
1761 defined. If your class or package uses them then \emph{please}
1762 replace them with new font commands described in \emph{\fntguide}.
1764 For example, the command |\twlsf| should be replaced by:
1765 \begin{verbatim}
1766 \fontsize{12}{14}\normalfont\sffamily\selectfont
1767 \end{verbatim}
1769 Another possibility is to use the |rawfonts| package, described in
1770 \emph{\usrguide}.
1772 Also, remember that many of the fonts preloaded by \LaTeX~2.09
1773 are no longer preloaded.
1775 \begin{decl}
1776 |\vpt| |\vipt| |\viipt| \dots
1777 \end{decl}
1778 These were the internal size-selecting commands in \LaTeX~2.09.
1779 (They can still be used in \LaTeX~2.09 compatibility mode.)
1780 Please use the command |\fontsize| instead: see \emph{\fntguide} for
1781 details.
1783 For example, |\vpt| should be replaced by:
1784 \begin{verbatim}
1785 \fontsize{5}{6}\normalfont\selectfont
1786 \end{verbatim}
1788 \begin{decl}
1789 |\prm|, |\pbf|, |\ppounds|, |\pLaTeX| \dots
1790 \end{decl}
1791 \LaTeX~2.09 used several commands beginning with |\p| in order to
1792 provide `protected' commands. For example, |\LaTeX| was defined to be
1793 |\protect\pLaTeX|, and |\pLaTeX| was defined to produce the \LaTeX{}
1794 logo. This made |\LaTeX| robust, even though |\pLaTeX| was not.
1796 These commands have now been reimplemented using
1797 |\DeclareRobustCommand|
1798 (described in Section~\ref{Sec:commands.define}).
1799 If your package redefined one of the |\p|-commands then you must
1800 remove the redefinition and use |\DeclareRobustCommand| to redefine the
1801 non-|\p| command.
1803 \begin{decl}
1804 |\footheight|\\
1805 |\@maxsep|\\
1806 |\@dblmaxsep|
1807 \end{decl}
1808 These parameters are not used by \LaTeXe{} so they have been removed,
1809 except in \LaTeX~2.09 compatibility mode. Classes should no longer
1810 set them.
1812 \begin{thebibliography}{1}
1814 \bibitem{A-W:DEK91}
1815 Donald~E. Knuth.
1816 \newblock {\em The \TeX book}.
1817 \newblock Addison-Wesley, Reading, Massachusetts, 1986.
1818 \newblock Revised to cover \TeX3, 1991.
1820 \bibitem{A-W:LLa94}
1821 Leslie Lamport.
1822 \newblock {\em {\LaTeX:} A Document Preparation System}.
1823 \newblock Addison-Wesley, Reading, Massachusetts, second edition, 1994.
1825 \bibitem{A-W:MG2004}
1826 Frank Mittelbach and Michel Goossens.
1827 \newblock {\em The {\LaTeX} Companion second edition}.
1828 \newblock With Johannes Braams, David Carlisle, and Chris Rowley.
1829 \newblock Addison-Wesley, Reading, Massachusetts, 2004.
1831 \end{thebibliography}
1833 \newpage
1834 \thispagestyle{empty}
1836 \section*{\LaTeXe{} Summary sheet: updating old styles}
1838 Section references below are to \emph{\clsguide}.
1840 \begin{enumerate}
1842 \item Should it become a class or a package?
1843 See Section~\ref{Sec:classorpkg} for how to answer this question.
1845 \item If it uses another style file, then you will need to obtain an
1846 updated version of this other file. Look at Section~\ref{Sec:loading}
1847 for information on how to load other class and package files.
1849 \item Try it: see Section~\ref{Sec:try-it}.
1851 \item It worked? Excellent, but there are probably still some things
1852 you should change in order to make your file into a well-structured
1853 \LaTeXe{} file that is both robust and portable. So you should now
1854 read Section~\ref{Sec:writing}, especially~\ref{Sec:general}. You
1855 will also find some useful examples in Section~\ref{Sec:structure}.
1857 If your file sets up new fonts, font-changing commands or symbols,
1858 you should also read \emph{\fntguide}.
1860 \item It did not work? There are three possibilities here:
1861 \begin{itemize}
1862 \item error messages are produced whilst reading your file;
1863 \item error messages are produced whilst processing test documents;
1864 \item there are no errors but the output is not as it should be.
1865 \end{itemize}
1866 Don't forget to check carefully for this last possibility.
1868 If you have got to this stage then you will need to read
1869 Section~\ref{Sec:upgrade} to find the solutions that will make your
1870 file work.
1871 \end{enumerate}
1873 \end{document}