[Tex/LaTex] Best practice for setting package options through class options

class-optionsdocumentclass-writingpackage-options

I want to process my class options so as to create a (potentially long) options list for package(s) I'm loading, e.g.

% set default package options here somehow
\DeclareOption{myvariant}{
    % set a bunch of alternative options overriding the default
}

\RequirePackage[some kind of magic should go here]{mypackage}

Specifically, I thought maybe I should be using PassOptionsToClass, but then – how do I prevent the default action?

Right now I'm doing something else, and kind of ugly, which relies on my specific package of interest (geometry) allowing doing everything with a command rather than a package option:

\newcommand{\iitcs@geometry{\geometry{key=val,key2=val2}}
\DeclareOption{myopt}{
 \renewcommand{\iitcs@geometry{\geometry{key=val3,key3=val4}}
}
%...
\RequirePackage{geometry}
\iitcs@geometry

(edit) I'll emphasize that I'd like a solution for the case I have no choice but to provide the right package options, with no flexibility like with `geometry'.

Best Answer

You can use \geometry and options therein multiple times. So for this application I suggest something like

\DeclareOption{myopt}{%
  \AtEndOfPackage{\geometry{paperwidth=3cm,paperheight=9cm}}}

\ProcessOptions\relax
\RequirePackage{geometry}
\geometry{paperwidth=12cm,paperheight=12cm} % default

In more general cases it may be more difficult, because options are executed in the order they are listed in the package, when it calls \ProcessOptions.

So if packageB.sty declares x and nox in this order (later using \ProcessOptions, not \ProcessOptions*) and you say in packageA.sty

\DeclareOption{x}{\PassOptionsToPackage{x}{packageB}}
\DeclareOption{nox}{\PassOptionsToPackage{nox}{packageB}}

\ExecuteOptions{nox}
\ProcessOptions\relax
\RequirePackage{packageB}

then nox will be executed last by packageB no matter if you call packageA with the x option.

So a more precise answer depends on "real world" situations.