# TiKz for each iteration over spaced string argument has an extra }

This is an example of the functionality of a macro, that should the following: iterate over a spaced string and print the first letter of each part.

\newcommand\text{\StrSubstitute{a b c}{ }{,}}
\foreach \i in \text{
\StrLeft{\i}{1} ,
}


But apparently, I cannot use \StrLeft or any other when the iteration is over something I have done over a StrSubstitute. Why is that? Error messages:

! Paragraph ended before \reserved@a was complete.<to be read again>\par }
! Paragraph ended before \reserved@a was complete.<to be read again>\par }
! Missing control sequence inserted.<inserted text>\inaccessible }


The following do work though:

\newcommand\text{\StrSubstitute{a b c}{ }{,}}
\foreach \i in \text{
\i
}

\newcommand\text{a,b,c}
\foreach \i in \text{
\StrLeft{\i}{1} ,
}

• \text is not a,b,c, but the set of instructions necessary to produce a,b,c. – egreg Oct 10 '15 at 10:37

First of all, the choice of \text as a command name is not really good, because amsmath uses it. However, this is not the main point.

The big problem is that \text is not the list a,b,c, but rather a set of instructions for producing that list.

You're luckier with

\StrSubstitute{a b c}{ }{,}[\mytext]
\foreach\i in \mytext{%
\StrLeft{\i}{1}, %
}


Now \mytext contains the result of the substitution.

Full example, although it's not clear what's the use case:

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

\begin{document}

\StrSubstitute{a b c}{ }{,}[\mytext]

\foreach\i in \mytext{%
\StrLeft{\i}{1}, %
}

\end{document}