0

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?

8
  • you use \@swapnumbersoption but you define \@numberswapoption Mar 10, 2018 at 10:09
  • and ask you are using k=v syntax you don't want to use the standard \ProcessOptions Mar 10, 2018 at 10:12
  • @DavidCarlisle Thank you, I corrected the use of \@swapnumbersoption. Regarding k=v syntax I'm not sure I don't want to use \ProvessOptions at all, since I also have standard options. In the updated question I split standard options and k=v options into two blocks.
    – user157036
    Mar 10, 2018 at 10:26
  • no just use the X form for all of them \processOptions doesn't know theoremlevel=subsection is related to theoremlevel it just takes the entire string as an unknown option name Mar 10, 2018 at 10:28
  • @DavidCarlisle If I comment out the line \ProcessOptions\relax then the babel and the swapnumbers option no longer have an effect.
    – user157036
    Mar 10, 2018 at 10:30

1 Answer 1

0

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}

You must log in to answer this question.