3

I would like to do the following:

\mycmd{foo={a,b,c},baz={1,2,3}}

where {a,b,c} and {1,2,3} are arrays/sequences of data.

The MWE is this:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\begin{document}

\keys_define:nn { mycmd }
{
  \foo .tl_set:N = \l_mycmd_foo_tl,
  \baz .tl_set:N = \l_mycmd_baz_tl
}

\NewDocumentCommand\mycmd{m}{
  \keys_set:nn { mycmd } { #1 }

  % iterate through `\l_mycmd_foo_tl` comma-separated list
  % iterate through `\l_mycmd_bar_tl` comma-separated list
}

\mycmd{foo={a,b,c},baz={1,2,3}}

\end{document}
  • Neither foo nor baz are key names – user31729 Mar 25 '18 at 18:19
  • And what should the iteration do? – egreg Mar 25 '18 at 19:12
  • The iteration should just \typeout the value that's fine. In my case each iteration impl would be something custom. – Lance Pollard Mar 25 '18 at 19:15
5

If comma separated lists are mandatory, use foo .clist_set:N etc. in order to set expl3 clists and than iterate over them, e.g. with \clist_map_inline:Nn, where ##1 is replaced with the relevant entry of the list (assuming that \clist_map_inline:Nn is used within another macro).

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn



\begin{document}

\keys_define:nn { mycmd }
{
%  \foo .tl_set:N = \l_mycmd_foo_tl,
%  \baz .tl_set:N = \l_mycmd_baz_tl

  foo .clist_set:N = \l_mycmd_foo_clist,
  baz .clist_set:N = \l_mycmd_baz_clist
}

\NewDocumentCommand\mycmd{m}{
  \keys_set:nn { mycmd } { #1 }

  \clist_map_inline:Nn \l_mycmd_foo_clist {
    value\space##1\par
  }

  \clist_map_inline:Nn \l_mycmd_baz_clist {
    value\space##1\par
  }

  % iterate through `\l_mycmd_foo_tl` comma-separated list
  % iterate through `\l_mycmd_bar_tl` comma-separated list
}

\mycmd{foo={a,b,c},baz={1,2,3}}

\end{document}

enter image description here

  • “If comma separated lists are mandatory,” kind of reads as though it is implying “this is not the usual or best way to do this, but if you absolutely must do it this way due to constraints outside your control...” If that’s the case, it might be worth describing briefly at the end of the answer what the more typical/better approach to this would be. (If that’s not the case, it may be worth rewording.) – KRyan Mar 26 '18 at 0:28
  • @KRyan: No, I don't meant it that way. If the O.P. wants to have comma separated lists (i.e. he/she imposes this requirement) then \clist is the best way. – user31729 Mar 26 '18 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.