6

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}
2
7

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

7

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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