5

I'm trying to set up a macro \MyCommand{#1}{#2} which takes two inputs, one being a key-val list and then creates a token list which is named based on the input and the key name, having the value given to that key as the contents of the token list.

My attempt so far is to use l3keys as follows, with the contents of #1 stored in some \l__dai_temp_tl along with .tl_set:c = { l_dai_ \tl_use:N \l__dai_temp_tl _tl } however this appears to use the value of \l__dai_temp_tl at the point of \keys_define:nn.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\tl_new:N \l__dai_temp_tl

\keys_define:nn { daikeys } {
    foo .tl_set:c = { l_dai_ \tl_use:N \l__dai_temp_tl _tl },
}

\NewDocumentCommand \MyCommand { m  m } {
    \tl_set:Nn \l__dai_temp_tl { #1 }
    \keys_set:nn { daikeys } { #2 }
}
\ExplSyntaxOff

\begin{document}
\MyCommand{bar}{foo=foobar}

\ExplSyntaxOn
\l_dai_bar_tl
\l_dai__tl
\ExplSyntaxOff

\end{document}

In the above code \l_dai__tl has the value foobar while \l_dai_bar_tl is undefined, when I had hoped that this would (define and) set \l_dai_bar_tl to a value of foobar.

0

1 Answer 1

3

You are misunderstanding .tl_set:c. This is a variant of .tl_set:N which is therefore converted to a name at the point of definition rather than at the point of use. At definition, \l__dai_temp_tl is empty and so you get \l_dai__tl as you've observed.

If you want to create variables on-the-fly you will need to do it yourself

\keys_define:nn { daikeys } {
    foo .code:n =
     {
       \tl_clear_new:c { l_dai_ \tl_use:N \l__dai_temp_tl _tl }
       \tl_set:cn { l_dai_ \tl_use:N \l__dai_temp_tl _tl } {#1}
     },
}

You must log in to answer this question.

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