[Tex/LaTex] ‘Unknown option’ errors in custom package

errorspackage-optionspackage-writing

I am playing around a little with creating a custom package for theorem environments and related functionality. Consider the following MWE:

\RequirePackage{filecontents}
\begin{filecontents}{test.sty}
    \NeedsTeXFormat{LaTeX2e}
    \ProvidesPackage{test}

    %Required packages
    \RequirePackage{xkeyval} % For key-value syntax, \DeclareOptionX
    \RequirePackage{amsthm} % For theorem environments

    %Initialize some variables
    \newcommand{\theoremlevel}{}
    \newcommand{\@swapnumbersoption}{}

    %Default english theorem names
    \newcommand{\theoremname}{Theorem}

    %Declaring package options
    \DeclareOption{babel}{ %Babel Option to adjust theorem names if babel is used
        \addto\captionsenglish{
            \renewcommand{\theoremname}{Theorem}
        }
        \addto\captionsngerman{
            \renewcommand{\theoremname}{Satz}
        }
    }
    \DeclareOption{swapnumbers}{\renewcommand{\@swapnumbersoption}{\swapnumbers}} %Option to enable number swaps i.e. '1.1 Definition' rather than 'Definition 1.1'
    \ProcessOptions\relax

    \DeclareOptionX{theoremlevel}{\renewcommand{\theoremlevel}{#1}} %Key value option to specify the level theorem environments are numbered within
    \ExecuteOptionsX{theoremlevel=section}
    \ProcessOptionsX\relax

    %Creating theorem environments
    \@swapnumbersoption %Swaps numbers if option 'swapnumbers' is enabled, does nothing otherwise
    \theoremstyle{definition}
    \newtheorem{theorem}{\protect\theoremname}[\theoremlevel]
\end{filecontents}

\documentclass{article}

\usepackage[ngerman]{babel}

\usepackage[swapnumbers,theoremlevel=subsection,babel]{test}

\begin{document}
    \begin{theorem}
        Inhalt...
    \end{theorem}
\end{document}

The output is:

enter image description here

So indeed, all three options do what they should: The numbers are swapped, the captions are german and the theorem is numbered within subsubsection. However I get the following errors:

Unknown option `theoremlevel=subsection' for package `test'. \ProcessOptions\relax
Unknown option `swapnumbers' for package `test'. \ProcessOptionsX\relax
Unknown option `babel' for package `test'. \ProcessOptionsX\relax
Writing text ` ' before \end{filecontents} as last line of test.sty

So clearly, once again I am doing something stupid. But what?

Best Answer

You could define a default option for the standard option processor to let unknown options pass through but it is simpler to just use the X form for all of them if you use it at all

\RequirePackage{filecontents}
\begin{filecontents}{test.sty}
    \NeedsTeXFormat{LaTeX2e}
    \ProvidesPackage{test}

    %Required packages
    \RequirePackage{xkeyval} % For key-value syntax, \DeclareOptionX
    \RequirePackage{amsthm} % For theorem environments

    %Initialize some variables
    \newcommand{\theoremlevel}{}
    \newcommand{\@swapnumbersoption}{}

    %Default english theorem names
    \newcommand{\theoremname}{Theorem}

    %Declaring package options
    \DeclareOptionX{babel}{%missing % %Babel Option to adjust theorem names if babel is used
        \addto\captionsenglish{%missing %
            \renewcommand{\theoremname}{Theorem}%missing %
        }%missing %
        \addto\captionsngerman{%missing %
            \renewcommand{\theoremname}{Satz}%missing %
        }%missing %
    }
    \DeclareOptionX{swapnumbers}{\renewcommand{\@swapnumbersoption}{\swapnumbers}} %Option to enable number swaps i.e. '1.1 Definition' rather than 'Definition 1.1'
    \DeclareOptionX{theoremlevel}{\renewcommand{\theoremlevel}{#1}} %Key value option to specify the level theorem environments are numbered within
    \ExecuteOptionsX{theoremlevel=section}

% you only want the X form    \ProcessOptions\relax
    \ProcessOptionsX\relax

    %Creating theorem environments
    \@swapnumbersoption %Swaps numbers if option 'swapnumbers' is enabled, does nothing otherwise
    \theoremstyle{definition}
    \newtheorem{theorem}{\protect\theoremname}[\theoremlevel]
\end{filecontents}

\documentclass{article}

\usepackage[ngerman]{babel}

\usepackage[swapnumbers,theoremlevel=subsection,babel]{test}

\begin{document}
    \begin{theorem}
        Inhalt...
    \end{theorem}
\end{document}