l3keys 和递归命令

l3keys 和递归命令

我正在尝试创建一个l3keys名为 的列表mykeys,用于控制命令的行为mycommand:n。最初,它只是身份。设置command=\textit应该将其更改为\textit(由身份组成)。通常,设置command=\foo应该重新定义mycommand:n#1\foo{mycommand:n{#1}}。但是,就目前而言,它失败得很惨。这能行得通吗?

\documentclass{article}

\usepackage{expl3}

\ExplSyntaxOn

\cs_new:Npn \identity:n #1 { #1 }


\keys_define:nn { mykeys }
{
    command .multichoice:,
    command / none .code:n = {
        \cs_set_eq:NN \mycommand:n \identity:n %this resets \mycommand:n
                                               %to become the identity
    },
    command / unknown .code:n = {
        \cs_set:Npx \mycommand:n ##1
        {
            #1 { \mycommand:n { ##1 } }
        }
    }
}

\keys_set:nn { mykeys } { command=none }

\ExplSyntaxOff

\begin{document}

\ExplSyntaxOn

\mycommand:n{test1} % should produce test1

\keys_set:nn { mykeys } { command=\textit }

\mycommand:n{test2} % should produce \textit{test2}

\keys_set:nn { mykeys } { command=\textbf }

\mycommand:n{test3} % should produce \textbf{\textit{test3}}

\ExplSyntaxOff

\end{document}

答案1

我对界面不是特别感兴趣,但在这里我会按顺序存储应用程序的命令

\documentclass{article}
\usepackage{expl3,xparse}
\ExplSyntaxOn

\seq_new:N \l__mycommand_cmds_seq
\tl_new:N \l__mycommand_tmp_tl

\keys_define:nn { mykeys }
  {
    command .code:n = 
      {
        \str_if_eq:nnTF {#1} { none }
          { \seq_clear:N \l__mycommand_cmds_seq }
          { \seq_put_right:Nn \l__mycommand_cmds_seq {#1} }
      }
  }
\keys_set:nn { mykeys } { command = none }

\cs_new_protected:Npn \mycommand_main:n #1
  {
    \group_begin:
      \tl_set:Nn \l__mycommand_tmp_tl {#1}
      \seq_map_inline:Nn \l__mycommand_cmds_seq
        {
          \tl_set:Nx \l__mycommand_tmp_tl
            { \exp_not:n {##1} { \exp_not:V \l__mycommand_tmp_tl } }
        }
    \exp_args:NV \group_end:
    \l__mycommand_tmp_tl
  }

\NewDocumentCommand \mysetup { +m } { \keys_set:nn { mykeys } {#1} }
\NewDocumentCommand \mycommand { m }
  { \mycommand_main:n {#1} }
\ExplSyntaxOff

\begin{document}

\mycommand{test1} % should produce test1

\mysetup{command = \textit}

\mycommand{test2} % should produce \textit{test2}

\mysetup{command = \textbf}

\mycommand{test3} % should produce \textbf{\textit{test3}}

\end{document}

人们可能会将单标记/宏测试作为 keyval 设置的一部分,但这可能有点过度。

人们可能会在 keyval 设置期间构建“功能性”标记列表,但我怀疑在调试中检查该序列可能会很有用。

答案2

在此处输入图片描述

只要所涉及的命令在一次扩展后公开其实现,您就可以执行以下操作

\documentclass{article}

\usepackage{expl3}

\ExplSyntaxOn

\cs_new:Npn \identity:n #1 { #1 }

\cs_new:Npn\wrap_def:NN#1#2{
  \exp_after:wN\cs_set:Npn
   \exp_after:wN#2\exp_after:wN##\exp_after:wN1\exp_after:wN{
   \exp_after:wN#1\exp_after:wN{#2{##1}}}
  }





\keys_define:nn { mykeys }
{
    command .multichoice:,
    command / none .code:n = {
        \cs_set_eq:NN \mycommand:n \identity:n %this resets \mycommand:n
                                               %to become the identity
    },
    command / unknown .code:n = {
        \wrap_def:NN #1 \mycommand:n
    }
}

\keys_set:nn { mykeys } { command=none }

\ExplSyntaxOff

\begin{document}

\ExplSyntaxOn

\mycommand:n{test1} % should produce test1

\keys_set:nn { mykeys } { command=\textit }


\mycommand:n{test2} % should produce \textit{test2}

\keys_set:nn { mykeys } { command=\textbf }

\mycommand:n{test3} % should produce \textbf{\textit{test3}}

\ExplSyntaxOff

\end{document}

答案3

实施David的建议:

\documentclass{article}

\usepackage{expl3}

\ExplSyntaxOn

\tl_new:N \l_gaussler_command_start_tl
\tl_new:N \l_gaussler_command_end_tl

\keys_define:nn { mykeys }
 {
  command .multichoice:,
  command / none .code:n =
   {
    \tl_clear:N \l_gaussler_command_start_tl
    \tl_clear:N \l_gaussler_command_end_tl
    \cs_set_protected:Nn \gaussler_thecommand:n { ##1 }
   },
  command / unknown .code:n =
   {
    \tl_put_left:Nn \l_gaussler_command_start_tl { \exp_not:N #1 \gaussler_command_open: }
    \tl_put_right:Nn \l_gaussler_command_end_tl { \gaussler_command_close: }
    \cs_set_protected:Nx \gaussler_thecommand:n
     {
      \tl_use:N \l_gaussler_command_start_tl
      ##1 
      \tl_use:N \l_gaussler_command_end_tl
     }
   }
 }
\cs_new:Nn \gaussler_command_open: { \if_true: { \else: } \fi: }
\cs_new:Nn \gaussler_command_close: { \if_false: { \else: } \fi: }

\keys_set:nn { mykeys } { command=none }

\ExplSyntaxOff

\begin{document}

\ExplSyntaxOn

\gaussler_thecommand:n{test1} % should produce test1

\keys_set:nn { mykeys } { command=\textit }

\gaussler_thecommand:n{test2} % should produce \textit{test2}

\keys_set:nn { mykeys } { command=\textbf }

\gaussler_thecommand:n{test3} % should produce \textbf{\textit{test3}}

\keys_set:nn { mykeys } { command=none }

\gaussler_thecommand:n{test4}

\ExplSyntaxOff

\end{document}

在此处输入图片描述

相关内容