我正在尝试为定理环境和相关功能创建自定义包。考虑以下 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}
输出为:
因此,这三个选项确实都发挥了应有的作用:数字互换、标题为德语、定理在小节内编号。但是我得到了以下错误:
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
显然,我又做了一件蠢事。但是什么呢?
答案1
您可以为标准选项处理器定义一个默认选项,以让未知选项通过,但如果要使用 X 形式,则更为简单
\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}