next up previous contents
Next: A minimal class file Up: The structure of a Previous: Using classes and packages

Declaring options

The other major difference between LaTeX 2.09 styles and LaTeX2e packages and classes is in option handling. Packages and classes can now declare options and these can be specified by authors; for example, the twocolumn option is declared by the article class.

An option is declared as follows:

   \DeclareOption{<option>}{<code>}
For example, the dvips option (slightly simplified) to the graphics package is implemented as:

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:
   \DeclareOption{a4paper}{%
      \setlength{\paperheight}{297mm}%
      \setlength{\paperwidth}{210mm}%
   }
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:
   \DeclareOption*{<code>}
For example, to make the package fred produce a warning rather than an error for unknown options, you could specify:
   \DeclareOption*{%
      \PackageWarning{fred}{Unknown option `\CurrentOption'}%
   }
Then, if an author writes \usepackage[foo]{fred}, they will get a warning Package fred Warning: Unknown option `foo'. As another example, the fontenc package tries to load a file <ENC>enc.def whenever the <ENC> option is used. This can be done by writing:
   \DeclareOption*{%
}%
   }
It is possible to pass options on to another package or class, using the command \PassOptionsToPackage or \PassOptionsToClass. For example, to pass every unknown option on to the article class, you can use:
   \DeclareOption*{%
      \PassOptionsToClass{\CurrentOption}{article}%
   }
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:

   \ProcessOptions\relax
This executes the <code> for each option that was both specified and declared (see Section 4.7 for details of how this is done).

For example, if the jane package file contains:

   \DeclareOption{foo}{\typeout{Saw foo.}}
   \DeclareOption{baz}{\typeout{Saw baz.}}
   \DeclareOption*{\typeout{What's \CurrentOption?}}
   \ProcessOptions\relax
and an author writes \usepackage[foo,bar]{jane}, then they will see the messages Saw foo. and What's bar?


next up previous contents
Next: A minimal class file Up: The structure of a Previous: Using classes and packages

Rainer Schoepf
Thu Jan 8 11:51:39 MET 1998