3

I'm defining a class that extends article that loads a package inside (automultiplechoice). I'm trying to configure the package and the class using the parameters of the \documentclass command.

When I test for other packages (for example hyperref) it works (see code below).

\RequirePackage{filecontents}
\begin{filecontents*}{test.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2012/02/10 Mini class for testing proposal]

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}

% toggle this
\PassOptionsToPackage{linkcolor=blue}{hyperref}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{hyperref}}
\ProcessOptions\relax
\RequirePackage{hyperref}

\end{filecontents*}
\documentclass[colorlinks=true,legalpaper]{test}

\begin{document}
\section{Test}\label{foo}

\ref{foo}

\end{document}

However, when I change it to the automultiplechoice package, it doesn't (see code below)

\RequirePackage{filecontents}
\begin{filecontents*}{test.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2012/02/10 Mini class for testing proposal]

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}

% with this
\PassOptionsToPackage{lang=EN}{automultiplechoice}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{automultiplechoice}}
\ProcessOptions\relax
\RequirePackage{automultiplechoice}

\end{filecontents*}


\documentclass[legalpaper,lang=ES]{test}

\begin{document}
\section{Test}\label{foo}

\ref{foo}

\end{document}

In the latter, the unknown parameters for the article class (in this case legalpaper) trigger an error in the package, but I would like them to be ignored. I got the error

! LaTeX Error: Unknown option legalpaper for package automultiplechoice.

What I am missing? It has something to do with the xkeyvalue definitions?

I would like to be able to pass parameters to the package and to the class in the same definition in \documentclass. How can I make that happen?

4
  • You need to specify which options are supposed to be passed to the article class and which are supposed to be passed to the automultiplechoice package. I think you're misusing the starred \DeclareOption, here.
    – jub0bs
    May 2, 2014 at 16:44
  • I think that, but didn't work. I tried something in the lines of \DeclareOption{lang=EN}{\PassOptionsToPackage{\CurrentOption}{automultiplechoice}}. Also, that solution has the limitation that I have to manually define all the available options, and if in the future there are more or less options I need to change my code. So, is not maintanable in time.
    – adn
    May 2, 2014 at 16:48
  • It is maintainable if you define a macro to automate option passing to one thing or the other. Also, I don't think you're supposed to invoke \ProcessOptions more than once.
    – jub0bs
    May 2, 2014 at 16:50
  • Can you show an example or provide information on how to do such automation?
    – adn
    May 2, 2014 at 16:51

1 Answer 1

3

Unless the packages/classes you use have some mechanism to cope with options they don't know (by either ignoring them or by merely issuing a warning), you're going to be in trouble if you pass options around indiscriminately (by using the starred \DeclareOption as you do). You need to take care of "which option goes where" yourself.

  • All your options should be declared before you invoke \ProcessOptions.
  • Only invoke \ProcessOptions once in your class file.
  • Specify to which class/package each option should be passed. For more automation, you can define a macro (called \batchPassOptionsToClass below) using LaTeX's for-loop construct.

Have a look at LaTeX2e for class and package writers for a good tutorial on class/package writing.

enter image description here

\RequirePackage{filecontents}
\begin{filecontents*}{test.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2012/02/10 Mini class for testing proposal]

% helper macros (for more automation)
\makeatletter
\newcommand\batchPassOptionsToClass[2]
{%
  \edef\@tempa{#1} % option list
  \@for\@tempb:=\@tempa\do{
    \DeclareOption{\@tempb}{\PassOptionsToClass{\CurrentOption}{#2}}
  }
}

\newcommand\batchPassOptionsToPackage[2]
{%
  \edef\@tempa{#1}% option list
  \@for\@tempb:=\@tempa\do{%
    \DeclareOption{\@tempb}{\PassOptionsToPackage{\CurrentOption}{#2}}%
  }%
}
\makeatother

% declare all options that should be passed to article
\batchPassOptionsToClass
  {a4paper,legalpaper} % list all options that should be passed to article here
  {article}

% pass all other options to the automultiplechoice package
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{automultiplechoice}}
% (or use \batchPassOptionsToPackage, if need to pass some options to other packages)

% process options (only once in your class file)
\ProcessOptions\relax

\LoadClass{article}

\RequirePackage[lang=EN]{automultiplechoice}
\end{filecontents*}


\documentclass[legalpaper,lang=ES]{test}

\begin{document}
\section{Test}\label{foo}

\ref{foo}


\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .