1

I'm puzzled as to why the following doesn't work:

\documentclass{article}

\usepackage{xparse}

 \ExplSyntaxOn
 \clist_new:N \l_test
 \clist_set:Nn \l_test {a,b,c}
\NewDocumentCommand{\putitem}{m}{
  \clist_put_right:Nn \l_test {#1}}
\NewDocumentEnvironment{foobar}{mm}{ FOO }{ BAR \putitem{##1}}
 \NewDocumentCommand{\giveitems}{}{\clist_map_inline:Nn \l_test {\fbox{##1}}}
 \ExplSyntaxOff

\begin{document}

 \putitem{xyz}
 \putitem{asdf}
 \giveitems

\begin{foobar}{abab}{baba}
  XXX
\end{foobar}

\giveitems % output is the same as the earlier call
\end{document}

So as part of making the environment, an additional string should be added to the clist. But it doesn't work. I would expect the second \giveitems to have an additional item, namely abab. Replacing \putitem with its definition inside the new environment doesn't make any difference. What am I missing here?

1 Answer 1

3

Environments form groups, which means you'll need a global variable to see the result outside. At the same time, you need #1 not ##1 when using the argument from your environment.

\ExplSyntaxOn
\clist_new:N \g__seamus_test_clist
\clist_set:Nn \g__seamus_test_clist {a,b,c}
\NewDocumentCommand{\putitem}{m}{
  \clist_gput_right:Nn \g__seamus_test_clist {#1}
}
\NewDocumentEnvironment{foobar}{mm}
  { FOO }
  { BAR \putitem{#1}}
\NewDocumentCommand{\giveitems}{}
  {\clist_map_inline:Nn \g__seamus_test_clist {\fbox{##1}}}
\ExplSyntaxOff
3
  • I've tided up the variable name here to follow standard expl3 conventions
    – Joseph Wright
    Commented Apr 30, 2021 at 15:00
  • Thanks for the quick answer! what convention requires double underscores after the g in the clist?
    – Seamus
    Commented Apr 30, 2021 at 15:09
  • @Seamus Double underscores mean “private variable”, so not to be used by third parties.
    – egreg
    Commented Apr 30, 2021 at 15:35

You must log in to answer this question.

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