Is it possible to store the key value argument (a cvs list) to a macro (via \def) and split/further process the content of that macro with \forcsvlist command from etoolbox package?

I want to provide a list of index entries, separated by , such that each entry can be formatted and layout in a special way (as index/glossary, details will still have to be defined).

It is possible (see MWE) to use the \index command directly in the body definition of the \define@key - command within a a \forcvslist statement, leading to some kind of \immediate\write to the index file.

I rather want to process the entries of the cvs list later on, say, in a special command called \MyCmd, saying \forcvslist{\index}{\KVMacroCSVList}, where the \index command may be wrapped by another one expecting the list splitting input as last argument.

However, even the simplified form with \index as argument to \forcsvlist fails with the error message

! Undefined control sequence. \string \indexentry {\KVMacroCSVList }{\thepage } l.47 \MyCmd[IndexList={First,Second}]

Here is my non-working MWE:




%Fails to recognize the comma input, does not split!!!
% Generating the index for testing purposes only

  \forcsvlist{\index}{#1}% Works 


% Assume that \KVMacroCSVList is defined 
% Process and format the List%
% However this fails



\chapter{Einstein's theory of relativity}%




Here is an image that shows the behaviour, if one comments \forcsvlist in \MyCmd. The separate entries stem from the second call to \MyCmd, where as the single line originates from the first call to \MyCmd.

enter image description here

Is it an issue of \expandafter, \protected etc?


I am not sticking to \forcvslist, another approach will do also.

  • 1
    Probably \@xp\forcsvlist\@xp{\@xp\index\@xp}\@xp{\KVMacroCSVList} where \@xp stands for \expandafter.
    – egreg
    Commented Apr 25, 2014 at 12:48
  • @egreg: Yes, that works, but how complicated this looks ;-) I suspected that it has to do with \@xp, even with a double application of it, but not 5 of it ;-)
    – user31729
    Commented Apr 25, 2014 at 14:33
  • \begingroup\edef\x{\endgroup\noexpand\forcsvlist{\noexpand\index}{\unexpanded\expandafter{\KVMacroCSVList}}}\x is not easier.
    – egreg
    Commented Apr 25, 2014 at 14:37
  • @egreg: No, it does not look easier. Will you provide an full answer to 'close' this question?
    – user31729
    Commented Apr 25, 2014 at 14:39

1 Answer 1


You have to use


Yes, five of them, because you want to expand the list macro before \forcsvlist acts upon it.

A different approach with expl3:



\NewDocumentCommand{\MyCmd}{ O{} }
  \keys_set:nn { hupfer/index } { #1 }
  \clist_map_inline:Nn \l_hupfer_index_items_clist
    \hupfer_wrap_index:n { ##1 }

\cs_new_protected:Npn \hupfer_wrap_index:n #1
  \index{#1} % for demonstration purposes

\keys_define:nn { hupfer/index }
  IndexList .clist_set:N = \l_hupfer_index_items_clist,


\chapter{Einstein's theory of relativity}



You must log in to answer this question.