3

In learning how to properly distinguish between the different function signatures in LaTeX3 (and other features), I want to try a more complex example of dynamically generating l3keys, since it requires dynamically defining several different things in different contexts.

I have the following MWE that tries to dynamically generate keys:

\documentclass[a4paper]{article}

\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\begin{document}

\NewDocumentCommand\definekey{mm}{
  \tl_new_local:N \l_examplea_mod_tl
  \tl_new_local:N \l_examplea_key_tl

  \tl_set:Nn \l_examplea_mod_tl {#1}
  \tl_set:Nn \l_examplea_key_tl {#2}

  \keys_define:nn { \l_examplea_mod_tl }
  {
    \l_examplea_key_tl .tl_set:N = \l_ \l_examplea_mod_tl _ \l_examplea_key_tl _tl
  }
}

\NewDocumentCommand\examplea{m}
  {
    \keys_set:nn { examplea } { #1 }
    \typeout{\l_examplea_foo_tl}
    \typeout{\l_examplea_baz_tl}
    \prop_put:NnV \l_examplea_main_prop {foo} \l_examplea_foo_tl
    \prop_put:NnV \l_examplea_main_prop {baz} \l_examplea_baz_tl
  }

\prop_new:N \l_examplea_main_prop

\definekey{examplea}{foo}
\definekey{examplea}{baz}

\examplea{foo=bar,baz=asdf}

\prop_show:N \l_examplea_main_prop

\end{document}

The goal is essentially this. Define a new key like this:

\definekey{mymodule}{mykeya}
\definekey{mymodule}{mykeyb}

which should become:

\keys_define:nn { mymodule } {
  mykeya .tl_set:N = \l_mymodule_mykeya_tl
}
\keys_define:nn { mymodule } {
  mykeyb .tl_set:N = \l_mymodule_mykeyb_tl
}

which effectively becomes:

\keys_define:nn { mymodule } {
  mykeya .tl_set:N = \l_mymodule_mykeya_tl
  mykeyb .tl_set:N = \l_mymodule_mykeyb_tl
}

I am not close to getting this to work I don't think, probably making a few mistakes. The error I'm getting is this:

! Undefined control sequence.
\definekey #1#2->\tl_new_local:N
                                 \l_examplea_mod_tl \tl_new_local:N \l_examp...
l.34 \definekey{examplea}{foo}

The goal is to dynamically define keys in \keys_define:nn.

3

1 Answer 1

6

You can use the argument placeholders and .tl_set:c for building the token list name.

\documentclass[a4paper]{article}

\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\begin{document}

\NewDocumentCommand\definekey{mm}
 {
  \keys_define:nn { #1 }
   {
    #2 .tl_set:c = { l_#1_#2_tl },
   }
 }

\NewDocumentCommand\examplea{m}
  {
    \keys_set:nn { examplea } { #1 }
    \typeout{\l_examplea_foo_tl}
    \typeout{\l_examplea_baz_tl}
    \prop_put:NnV \l_examplea_main_prop {foo} \l_examplea_foo_tl
    \prop_put:NnV \l_examplea_main_prop {baz} \l_examplea_baz_tl
  }

\prop_new:N \l_examplea_main_prop

\definekey{examplea}{foo}
\definekey{examplea}{baz}

\examplea{foo=bar,baz=asdf}

\prop_show:N \l_examplea_main_prop

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .