add tag
निरंजन
Consider the following code:

```
\documentclass{article}

\begin{document}
\ExplSyntaxOn
\keys_define:nn { tmp _ keys } {
  foo     .tl_gset_e:c   = {
    g _ tmp _ \l_tmpa_tl _ foovar _ tl
  },
  bar     .tl_gset_e:c   = {
    g _ tmp _ \l_tmpa_tl _ barvar _ tl
  }
}

\cs_new:Nn \tmp_keys:nn {
  \tl_set:Ne \l_tmpa_tl { #1 }
  \keys_set:nn { tmp _ keys } { #2 }
  \tl_clear:N \l_tmpa_tl
}

\cs_new:Nn \tmp_use_keys:n {
  \clist_map_inline:nn {
    foo,
    bar
  } {
    \tl_if_empty:cF { g _ tmp _ #1 _ ##1 var _ tl } {
      \tl_use:c { g _ tmp _ #1 _ ##1 var _ tl }
    }
  }
}

\tmp_keys:nn { tmp } { foo = bar, bar = foo }

\tmp_use_keys:n { tmp }
\ExplSyntaxOff
\end{document}
```

It results in the following errors:

```
Use of \??? doesn't match its definition.
<argument> \???  
                 ! LaTeX Error: Erroneous variable \g_tmp_tmp_foovar_tl used!
l.33 \tmp_use_keys:n { tmp }
Use of \??? doesn't match its definition.
<argument> \???  
                 ! LaTeX Error: Erroneous variable \g_tmp_tmp_barvar_tl used!
l.33 \tmp_use_keys:n { tmp }
```

Why? I expect `\l_tmpa_tl` to expand at the appropriate place, but it seems that it doesn't expand. What am I exactly missing? How can I achieve this type of on-the-fly tl-generation using l3keys?
Top Answer
Skillmon
The effect of `.tl_gset_e:c` (and all other key handlers using `c`) is applied at define-time, not at use-time. So you defined your `foo` and `bar` keys to define variables depending on whatever value `\l_tmpa_tl` had when you called `\keys_define:nn`.

To get dynamically named variables at your use time you'll have to use custom code, that's nothing that `l3keys` supports.


```
\documentclass{article}

\begin{document}
\ExplSyntaxOn
\cs_new_protected:Npn \__tmp_tl_gset_e_handler:nn #1
  {
    \tl_gclear_new:c {#1}
    \tl_gset:ce {#1}
  }
\keys_define:nn { tmp _ keys } {
   foo .code:n =
    \__tmp_tl_gset_e_handler:nn { g _ tmp _ \l_tmpa_tl _ foovar _ tl } {#1}
  ,bar .code:n =
    \__tmp_tl_gset_e_handler:nn { g _ tmp _ \l_tmpa_tl _ barvar _ tl } {#1}
}

\cs_new_protected:Nn \tmp_keys:nn {
  \tl_set:Ne \l_tmpa_tl { #1 }
  \keys_set:nn { tmp _ keys } { #2 }
  \tl_clear:N \l_tmpa_tl
}

\cs_new_protected:Nn \tmp_use_keys:n {
  \clist_map_inline:nn {
    foo,
    bar
  } {
    \tl_if_empty:cF { g _ tmp _ #1 _ ##1 var _ tl } {
      \tl_use:c { g _ tmp _ #1 _ ##1 var _ tl }
    }
  }
}

\tmp_keys:nn { tmp } { foo = bar, bar = foo }

\tmp_use_keys:n { tmp }
\ExplSyntaxOff
\end{document}
```

(Aside: You're missing `protected` for your macros, I added that)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.