# TikZ: \foreach in macro with argument as list

The code below generates:

What I expect is:

Why does the \foreach seem to treat the entire list as one value? How do I fix it?

\documentclass{article}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{expl3}
\usepackage{tikz}

\begin{document}

\ExplSyntaxOn

\DeclareDocumentCommand{\separate}{m}
{
}

\DeclareDocumentCommand{\map}{m}
{
\def\list{#1}
\foreach \i in \list
{
[\i]
}
}

\ExplSyntaxOff

\def\arg{3669887}

\arg

\separate{\arg}

\map{\separate{\arg}}

\end{document}

-
The argument to \map is \separate{\arg}, so what you get is \foreach \i in \separate{\arg} which is not what you want. – egreg Jul 12 '13 at 16:58
– Werner Jul 12 '13 at 17:04

You already use xstring to poke around in the string, so you could also do it like this:

## Code

\documentclass{article}
\usepackage{xstring}
\usepackage{tikz}

\begin{document}

\def\arg{3669887}

\StrLen{\arg}[\strlen]

\foreach \x in {1,...,\strlen}
{[\StrChar{\arg}{\x}]%
}

\end{document}


## Output

-

With your code you're essentially doing the same as

\foreach \i in {\separate{\arg}}{[\i]}


so there is only one cycle to do.

You have to first separate the string and then call \foreach, which can't do that job.

Here's how I would do it:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\separate}{sm}
{
\IfBooleanTF #1
{
\linus_separate:o { #2 }
}
{
\linus_separate:n { #2 }
}
}

\NewDocumentCommand{\map}{sm}
{
\IfBooleanTF #1
{
\linus_map:o { #2 }
}
{
\linus_map:n { #2 }
}
}

\cs_new_protected:Npn \linus_separate:n #1
{
% split the input into parts "at nothing"
\seq_set_split:Nnn \l_linus_list_seq { } { #1 }
% deliver the input separated by commas
\seq_use:Nnnn \l_linus_list_seq { ,~ } { ,~ } { ,~ }
}
% a variant for a macro input
\cs_generate_variant:Nn \linus_separate:n { o }
\cs_new_protected:Npn \linus_map:n #1
{
% split the input into parts "at nothing"
\seq_set_split:Nnn \l_linus_list_seq { } { #1 }
% deliver the input by enclosing the parts in brackets
\seq_map_inline:Nn \l_linus_list_seq { [##1] }
}
% a variant for a macro input
\cs_generate_variant:Nn \linus_map:n { o }

\ExplSyntaxOff

\begin{document}
\def\arg{3669887}

\arg

\separate*{\arg}

\map*{\arg}

\separate{1234}

\map{1234}

\end{document}


However you probably don't need the \separate function any more. I'd keep distinct the macros when the input is "symbolic", such as \arg, from when it is "explicit"; thus the two calls \map* and \map.

-