Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to add an option that sets a flag to a document class. That is, I want to be able to type

\documentclass[foo]{class}

and get one type of behavior, and type

\documentclass{class}

to get a different type of behavior. I'm thinking I should do it like this in the class file:

\newif\if@foo
\@foofalse
...
\DeclareOption{foo}{\@footrue}
...
\if@foo
...
\else
...
\fi

Will this work? Could this cause a problem if a package I use in the document also has a \newif\if@foo in it?

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

The LaTeX2e for class and package writers contains some examples of this within section 3.3 Declaring options (p 12). It pertains to the declaration of package and class options. The following snippets are taken from there:

An option is declared as follows:

\DeclareOption{<option>}{<code>}

For example, the dvips option (slightly simplied) to the graphics package is implemented as:

\DeclareOption{dvips}{\input{dvips.def}}

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'.

Subsequent sections contain some examples of classes constructed from others, which would be good to review.

Using options that are common to other packages could be problematic. However, since you have a choice over the boolean (or macros) associated with the option, choose it to be unique to your class. Authors typically include some form of package reference in the macros. For example, the tufte-latex bundle prepends most commands and booleans with @tufte to avoid possible clashes.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.