如何从模板类中删除 keyval 选项?

如何从模板类中删除 keyval 选项?

我想创建一个基于scrreprt.cls但选项较少的模板类。具体来说,我需要将字体大小限制为 11pt 或 12pt(同时仍以scrreprt.cls通常的方式传递其他选项)。如果用户尝试选择其他大小,该类应该发出警告并恢复为默认设置。如果选项以“传统”格式给出,我知道如何做到这一点,即

\documentclass[9pt]{scrreprt}

但是我该如何处理 keyval 语法中给出的选项呢?

\documentclass[fontsize=9pt]{scrreprt}

论文.cls:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{thesis}[2015/12/06 \space\space Ian Thompson]
%Need to restrict font size options here. 
\DeclareOption*{% 
  \PassOptionsToClass{\CurrentOption}{scrreprt}
\ProcessOptions\relax
\LoadClass{scrreprt}

测试.tex:

\documentclass[fontsize=9pt]{thesis} % <--- Option should generate a warning  
\begin{document}
Hello.
\end{document}

答案1

这里有一个建议,使改变禁止的字体大小值变得更加困难。

\begin{filecontents*}{MyClass.cls}
\ProvidesClass{MyClass}[2017/05/18]
\RequirePackage{scrbase}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
%\providecommand*\Ifisdimension{\Ifisdimension}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\DefineFamily{MyClass}
\DefineFamilyMember{MyClass}
\DefineFamilyKey{MyClass}{fontsize}{%
  \Ifstr{#1}{12pt}{\PassOptionsToClass{fontsize=12pt}{scrreprt}%
    }{%
      \PassOptionsToClass{fontsize=11pt}{scrreprt}%
      \Ifstr{#1}{11pt}{}{\ClassWarning{MyClass}{font size `#1' is not supported}}%
    }%
  \FamilyKeyStateProcessed%
}
\DeclareOption*{%
  \Ifisdimension{\CurrentOption}{\OptionNotUsed}{%
  \PassOptionsToClass{\CurrentOption}{scrreprt}}}
\FamilyProcessOptions{MyClass}
\LoadClass{scrreprt}
\let\@KOMAoption\KOMAoption
\renewcommand\KOMAoption[2]{%
  \ifstr{#1}{fontsize}{%
    \ifdim #2=12pt
      \@KOMAoption{fontsize}{12pt}%
    \else
      \ifdim #2=11pt
        \@KOMAoption{fontsize}{11pt}%
      \else
        \ClassWarning{MyClass}{font size `#2' is not supported}%
      \fi
    \fi
  }{%
    \@KOMAoption{#1}{#2}%
  }%
}
% disable \KOMAoptions
\renewcommand*\KOMAoptions[1]{%
  \ClassError{MyClass}{`\string\KOMAoptions` is not supported}{%
    Command `\string\KOMAoptions` is not supported by MyClass%
  }%
}
\endinput
\end{filecontents*}

\documentclass[fontsize=9pt,a5paper,DIV=calc]{MyClass}
\begin{document}
Hello. \KOMAScriptVersion

\KOMAoption{fontsize}{10pt}
Hello. \KOMAScriptVersion

\KOMAoption{fontsize}{12pt}
Hello. \KOMAScriptVersion
\end{document}

请注意,仍然可以使用或或使用命令或将字体大小更改为其他值,11pt而不是使用或...12pt\usepackage[9pt]{extsizes}\FamilyOptions{KOMA}{fontsize=9pt}\fontsize


这是 Markus Kohm 的额外建议,至少需要 3.18.2144 版本。当前版本可在KOMA-Script 网站

\begin{filecontents*}{mythesis.cls}
\ProvidesClass{mythesis}[2015/06/18]
\RequirePackage{scrbase}[2015/06/17]
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
%\providecommand*\Ifisdimension{\Ifisdimension}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\BeforeFamilyProcessOptions[.scrreprt.cls]{KOMA}{%
  \expandafter\let\csname [email protected]@font@size\expandafter\endcsname
                  \csname [email protected]@fontsize\endcsname
  \DefineFamilyKey[.scrreprt.cls]{KOMA}{fontsize}{%
    \Ifstr{##1}{12pt}{%
      \FamilyExecuteOptions[.scrreprt.cls]{KOMA}{font@size=12pt}%
      \FamilyKeyStateProcessed
    }{%
      \Ifstr{##1}{11pt}{%
        \FamilyExecuteOptions[.scrreprt.cls]{KOMA}{font@size=11pt}%
        \FamilyKeyStateProcessed
      }{%
        \FamilyKeyStateUnknownValue
      }%
    }%
  }%
}
\DeclareOption*{%
  \Ifisdimension{\CurrentOption}{\OptionNotUsed}{%
  \PassOptionsToClass{\CurrentOption}{scrreprt}}}
\ProcessOptions*
\LoadClass{scrreprt}
\endinput
\end{filecontents*}

\documentclass[fontsize=9pt,a5paper,DIV=calc]{mythesis}
\begin{document}
Hello. \KOMAScriptVersion
\KOMAoption{fontsize}{12pt}
Hello. \KOMAScriptVersion
\KOMAoptions{fontsize=10pt}
Hello. \KOMAScriptVersion
\end{document}

相关内容