忽略参数中未定义的键(它在使用之前在宏中定义)

忽略参数中未定义的键(它在使用之前在宏中定义)

我怎样才能让这个宏最初忽略未定义的键fn-special-text?它是在宏中定义的(因为名称是从第一个参数的文本生成的;这一切都发生在前两个步骤中),然后仅使用该参数该定义已生成。

\documentclass{article}
\usepackage{xstring}
\usepackage{keyval}
\begin{document}

% The command:
\newcommand\annMessageParseParse[2]{%
  % first step, search for the instance of `FN...` in arg #1
    \StrCut{#1}{FN}\partA\partB% take unique name starting after "FN"
    \StrCut{\partB}{ }\partC\partD% stop at the first space
  % generate a new key using that unique name:
    \makeatletter
    \define@key{mykeys}{fn-\partC-text}{\expandafter\def\csname fn\partC text\endcsname{#1}}
    \makeatother
  % set the key using arg #2
    \setkeys{mykeys}{#2}
  % create a command using the new name, which employs our new key (which is just a footnote command)
    \expandafter\def\csname FN\partC\endcsname{\expandarg\footnote{\expandafter\csname fn\partC text\endcsname}}
  % combine `FN` and `\partC` in order to use the so-named command we just made
    \edef\combinethem{FN\partC}
  % replace the old `FN...` with the new command
    \expandarg\def\thenewmessage{
      \StrSubstitute[0]{#1}{\combinethem}
      {\csname FN\partC\endcsname}
    }
  % spit out the message
    \thenewmessage
}

% use case:
  \annMessageParseParse
    {This is myFNspecial new message.}% will turn into `This is my^[1] new message`
    {fn-special-text={This is the special footnote}}
\end{document}

问题是,当调用宏时,keyval 立即看到未定义的键并出错。有什么方法可以告诉 keyval(或 xkeyval?)忽略未定义的键?消息和尚未定义键需要在同一个命令中的参数中进行。

答案1

\KV@errx如果未定义键,可以重新定义将扩展哪个键。例如:

\documentclass{article}
\usepackage{keyval}

\begin{document}

%\setkeys{test}{unknown=something}

\makeatletter
\let\KV@errx@ORI\KV@errx   % Save original error handling

\let\KV@errx\@gobble       % Ignore unknown keys
\setkeys{test}{unknown=something}

\renewcommand\KV@errx[1]{% Ignore unknown keys, but print a warning
  \PackageWarning{test}{#1}}
\setkeys{test}{unknown=something}

\let\KV@errx\KV@errx@ORI   % Restore original error handling
\makeatother

\end{document}

相关内容