扩展包选项,使包含的字符数学活跃

扩展包选项,使包含的字符数学活跃

对我另一个问题的回答,egreg发布的代码,在数学模式下激活一个字符并赋予其功能\operatorname

现在我想将其放入包中,并在包选项中设置应激活的角色。只需将反引号替换为\shortoperator@char,它就会像这样开始:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{shortoperator}[2014/02/09]

\RequirePackage{kvoptions}
\DeclareStringOption[`]{char}
\ProcessKeyvalOptions*

\RequirePackage{xparse,amsmath}

\ExplSyntaxOn
\tl_new:N \l__shortoperator_name_tl

\cs_new_protected:Npn \shortoperator_bq_mathop:
 {
  % clear the container
  \tl_clear:N \l__shortoperator_name_tl
  % start the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \shortoperator_absorb:
 {
  \peek_catcode:NTF a
   {% if the next token is a letter absorb it
    \__shortoperator_absorb_next:n
   }
   {% otherwise produce the operator name
    \__shortoperator_deliver:
   }
 }

\cs_new_protected:Npn \__shortoperator_absorb_next:n #1
 {
  % add the next letter to the container
  \tl_put_right:Nn \l__shortoperator_name_tl { #1 }
  % restart the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \__shortoperator_deliver:
 {
  % produce the operator name
  \operatorname{\l__shortoperator_name_tl}
 }

并连续:

% define the active back quote
\group_begin:
\char_set_catcode_active:N \shortoperator@char
\cs_gset_eq:NN \shortoperator@char \shortoperator_bq_mathop:
\group_end:

\ExplSyntaxOff

\AtBeginDocument{\mathcode`\shortoperator@char=\string"8000 }

使用该选项的位置。

但当然这行不通,因为\shortoperator@char需要扩展。我尝试使用\expandafter它来做到这一点,但无法让它工作。该怎么做?

使用示例(带有西拉特克斯) 将会:

\documentclass{article}
\usepackage[char=¡]{shortoperator}
\begin{document}
    \[e^{ix} = ¡cos x + i¡sin x\]
\end{document}

答案1

您不能传递\shortoperator@char数字的上下文。

这是一个解决方法。在包的开头写

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{shortoperator}[2014/02/09]

\RequirePackage{kvoptions}
\DeclareStringOption[`]{char}
\ProcessKeyvalOptions*
\edef\shortoperator@char{`\shortoperator@char}

这使得\shortoperator@char之后可以使用\mathcode;然后,最后一部分应该是

% define the active back quote
\group_begin:
\char_set_catcode_active:N \^^A
\char_set_lccode:nn { `\^^A } { \shortoperator@char }
\tl_to_lowercase:n
 {
  \cs_gset_eq:NN ^^A \shortoperator_bq_mathop:
 }
\group_end:

\ExplSyntaxOff

\AtBeginDocument{\mathcode\shortoperator@char=\string"8000 }

这个\lowercase技巧让 TeX 在正确的位置“看到”字符(变量)。

你可以将整个软件包制作成 LaTeX3:

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3,l3keys2e,amsmath}
\ProvidesExplPackage{shortoperator}{2014/02/09}
  {v. 0.1}{Shorthand for operator names}

\keys_define:nn { shortoperator }
 {
  char .tl_gset:N = \g_shortoperator_char_tl,
  char .initial:n = { ` }, % default is the back quote
 }

\ProcessKeysPackageOptions{ shortoperator }

% normalize the token list
\tl_gput_left:Nn \g_shortoperator_char_tl { ` }

\tl_new:N \l__shortoperator_name_tl

\cs_new_protected:Npn \shortoperator_bq_mathop:
 {
  % clear the container
  \tl_clear:N \l__shortoperator_name_tl
  % start the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \shortoperator_absorb:
 {
  \peek_catcode:NTF a
   {% if the next token is a letter absorb it
    \__shortoperator_absorb_next:n
   }
   {% otherwise produce the operator name
    \__shortoperator_deliver:
   }
 }

\cs_new_protected:Npn \__shortoperator_absorb_next:n #1
 {
  % add the next letter to the container
  \tl_put_right:Nn \l__shortoperator_name_tl { #1 }
  % restart the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \__shortoperator_deliver:
 {
  % produce the operator name
  \operatorname{\l__shortoperator_name_tl}
 }

% define the active back quote
\group_begin:
\char_set_catcode_active:N \^^A
\char_set_lccode:nn { `\^^A } { \g_shortoperator_char_tl }
\tl_to_lowercase:n 
 {
  \cs_gset_eq:NN ^^A \shortoperator_bq_mathop:
 }
\group_end:

% at begin document, make the character math active
\AtBeginDocument
 {
  \char_set_mathcode:nn { \g_shortoperator_char_tl }{ "8000 }
 }

相关内容