[Tex/LaTex] Mutually exclusive options in packages

package-optionspackage-writing

Is there a standard way to define mutually exclusive options in a package?

Could someone provide a simple example?

Currently I am mostly intrested in LaTeX2e but it would be nice to see a LaTeX3 implementation also.

Best Answer

The kvoptions package, which provides key-value type options, also has the possibility to define complementary options:

\DeclareBoolOption{draft}
\DeclareComplementaryOption{final}{draft}

This generates an if-switch which is set to true by draft but to false by final.


If you however mean otherwise mutually exclusive options, e.g. three options where only one can be used, then you best define three if-switches which are set to true by its corresponding option. The options then either set the if-switches of the other options to false or raise an error or warning if one of these if-switches are already set to true.

% package 'foo'
% Example: three mutually options alpha, beta, gamma; last one 
\newif\iffoo@alpha
\newif\iffoo@beta
\newif\iffoo@gamma

\DeclareOption{alpha}{\foo@alphatrue\foo@betafalse\foo@gammafalse}
\DeclareOption{beta}{\foo@alphafalse\foo@betatrue\foo@gammafalse}
\DeclareOption{gamma}{\foo@alphafalse\foo@betafalse\foo@gammatrue}

\ProcessOptions*% process options it the order the user provides them

% package body:
\iffoo@alpha
   % alpha code
\fi
\iffoo@beta
   % beta code
\fi
\iffoo@gamma
   % gamma code
\fi

or with error messages:

\def\foo@mutopterr{%
    \PackageError{foo}{Options 'alpha', 'beta' and 'gamma' are mutually exclusive.}{}%
}%
\DeclareOption{alpha}{%
    \iffoo@beta\foo@mutopterr\fi
    \iffoo@gamma\foo@mutopterr\fi
    \foo@alphatrue\foo@betafalse\foo@gammafalse
}
\DeclareOption{beta}{%
    \iffoo@alpha\foo@mutopterr\fi
    \iffoo@gamma\foo@mutopterr\fi
    \foo@alphafalse\foo@betatrue\foo@gammafalse
}
\DeclareOption{gamma}{%
    \iffoo@alpha\foo@mutopterr\fi
    \iffoo@beta\foo@mutopterr\fi
    \foo@alphafalse\foo@betafalse\foo@gammatrue
}