Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The code below generates:

enter image description here

What I expect is:

enter image description here

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}
{
  \StrLeft{#1}{1}[\head]
  \StrBehind[1]{#1}{\head}[\tail]
  \IfInteger{\tail}{\head,\separate{\tail}}{\head}
}

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

\ExplSyntaxOff

\def\arg{3669887}

\arg

\separate{\arg}

\map{\separate{\arg}}

\end{document}
share|improve this question
1  
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
1  
Related: \foreach loop with macro-defined list –  Werner Jul 12 '13 at 17:04

2 Answers 2

up vote 8 down vote accepted

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

enter image description here

share|improve this answer

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.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.