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}
Lance
  • 1,799

1 Answers1

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 00: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. –  Mar 26 '18 at 15:47