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:
\documentclass{book}
\usepackage{etoolbox}
\usepackage{makeidx}
\usepackage{xkeyval}
\makeatletter
\def\KeyFamilyName{SplitListFamily}
\define@key{\KeyFamilyName}{IndexList}[]{%
%Fails to recognize the comma input, does not split!!!
\def\KVMacroIndexList{#1}%
% Generating the index for testing purposes only
\index{#1}%
}%
\define@key{\KeyFamilyName}{CSVList}[]{%
\def\KVMacroCSVList{#1}%
\forcsvlist{\index}{#1}% Works
}%
\newcommand*{\MyCmd}[1][]{%
\setkeys{\KeyFamilyName}{#1}%
% Assume that \KVMacroCSVList is defined
% Process and format the List%
% However this fails
\forcsvlist{\index}{\KVMacroCSVList}%
}%
\makeatother
\makeindex%
\begin{document}
\chapter{Einstein's theory of relativity}%
\MyCmd[IndexList={First,Second}]
\MyCmd[CSVList={First,Second}]
\printindex
\end{document}
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
.
Is it an issue of \expandafter
, \protected
etc?
Edit
I am not sticking to \forcvslist
, another approach will do also.
\@xp\forcsvlist\@xp{\@xp\index\@xp}\@xp{\KVMacroCSVList}
where\@xp
stands for\expandafter
.\@xp
, even with a double application of it, but not 5 of it ;-)\begingroup\edef\x{\endgroup\noexpand\forcsvlist{\noexpand\index}{\unexpanded\expandafter{\KVMacroCSVList}}}\x
is not easier.