# Having multiple contradictory options, how to handle them?

Lets say you are writing a package, which has the usual pair of draft and final-mode.

But how should I handle, if this package gets the global option final and the local draft?

Consider the following ex.sty:

\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{ex}[2014/07/26 v1.00 This is just an example]
\DeclareOption{draft}{\newcommand\@draftmode{true}}
\DeclareOption{final}{\let\@draftmode=\@undefined}
\ProcessOptions\relax
\ifdefined\@draftmode
\newcommand\example{%
an example text, only visible in draft mode}
\else
\let\example=\relax
\fi


Using that in the following main.tex:

\documentclass[final]{article}
\usepackage[draft]{ex}
\begin{document}
some example text and eventually some more text: \example.
\end{document}


Results in the output “some example text and eventually some more text: .”. If I swap the \DeclareOption (final first, then draft), then at least I have the output of \example. But then it wouldn't work if I swap the options in the main.tex…

So what is the LaTeX way to handle this?

• Check if both options are set and then fire an error? (and how to check for both options?)
• Check if both options are set and then fire a warning?
• Prefer global option over local option? (how to check if a given option is global or local?)
• Prefer local option over global option?
• Just do it as I have and live with its problems? (how to decide which order might be more sens?)
• Use a package that I haven't found yet but really helps with such stuff?
• From a user's perspective I'd expect that local options win Jul 26, 2014 at 8:59
• I think so too, but I really don't know how to check if an option was locally or globally set :( Jul 26, 2014 at 9:04
• There is a package ifdraft. With it you can test the class option. ctan.org/pkg/ifdraft. Jul 26, 2014 at 9:49

From my perspective as a user I expect that local options take precedence over global options if they contradict each other. If you want your package to behave like this then here's what I would do:

I would define a conditional, \newif\ifex@draft, say, and let the options either set this conditional to true or false:

\DeclareOption{draft}{\ex@drafttrue}
\DeclareOption{final}{\ex@draftfalse}


Then all that's left to do is to change \ProcessOptions\relax into \ProcessOptions*. The clsguide says about the starred version:

This is like \ProcessOptions but it executes the options in the order specified in the calling commands, rather than in the order of declaration in the class or package. For a package this means that the global options are processed first.

So a modified version of the example package could look like this:

\RequirePackage{filecontents}
\begin{filecontents*}{ex.sty}
\ProvidesPackage{ex}[2014/07/26 v0.0 This is just an example]
\newif\ifex@draft % implicitly false. Set to true if you want a different default behaviour

\DeclareOption{draft}{\ex@drafttrue}
\DeclareOption{final}{\ex@draftfalse}
\ProcessOptions*

\ifex@draft
\newcommand*\example{Package ex' in draft mode}
\else
\newcommand*\example{Package ex' in final mode}
\fi
\end{filecontents*}

\documentclass[final]{article}
\usepackage[draft]{ex}
\begin{document}
\example
\end{document}