2

I'm trying to run \foreach over a nested list. For each iteration the value of the loop variable will be a list separated by semi-colons. To split the values I'm using \set_seq_list. It works when not within \foreach, it does not otherwise.

\documentclass{article}
\usepackage{tikz}

\ExplSyntaxOn
\seq_new:N \l_ma_index_list 

\NewDocumentCommand{\remplielaliste}{+m}{%
  \seq_set_split:Nnn \l_ma_index_list {;} {#1}
}


\NewDocumentCommand{\cp}{m}{%
  \int_compare:nNnT {#1} < {\seq_count:N \l_ma_index_list +1} {%
    \seq_item:Nn \l_ma_index_list {#1}
  }
}

\ExplSyntaxOff
\setlength\parindent{0em}

\begin{document}
  \remplielaliste{janvier; février; mars; avril}

  \textbackslash{}cp{1} : \cp{1} 
  
  \textbackslash{}cp{2} : \cp{2} 

  \textbackslash{}cp{3} : \cp{3} 

  \textbackslash{}cp{4} : \cp{4} 

\vspace{2cm}
\foreach \i in {{lundi; mardi}, {mercredi; jeudi}}
  {
    \i
    
    \remplielaliste{\i}
    
    \textbackslash{}cp{1} : \cp{1}  % here I'm expecting only 
                                    % 'lundi' and 'mercredi'
                                    % but it is printing 
                                    % 'lundi; mardi' and 
                                    % 'mercredi; jeudi'
                                    %
                                    % splitting is not done

  }

\end{document}
0

1 Answer 1

6

You need to expand \i before \remplielaliste sees it:

\expandafter\remplielaliste\expandafter{\i}

However, I believe it's better if you do it with just expl3.

\documentclass{article}

\ExplSyntaxOn
\seq_new:N \l_ma_index_list_seq

\NewDocumentCommand{\lforeach}{m +m}
 {
  \clist_map_inline:nn { #1 } { #2 }
 }

\NewDocumentCommand{\remplielaliste}{+m}
 {
  \seq_set_split:Nnn \l_ma_index_list_seq {;} {#1}
 }

\NewDocumentCommand{\cp}{m}
 {
  \seq_item:Nn \l_ma_index_list_seq {#1}
 }

\ExplSyntaxOff

\setlength\parindent{0em}

\begin{document}

\remplielaliste{janvier; février; mars; avril}

\texttt{\string\cp\string{1\string}} : \cp{1} 
  
\texttt{\string\cp\string{2\string}} : \cp{2} 

\texttt{\string\cp\string{3\string}} : \cp{3} 

\texttt{\string\cp\string{4\string}} : \cp{4} 

\texttt{\string\cp\string{5\string}} : \cp{5} 

\vspace{2cm}

\lforeach{{lundi; mardi}, {mercredi; jeudi}}{%
  #1\par
  \remplielaliste{#1}%
  \texttt{\string\cp\string{1\string}} : \cp{1}\par
}

\end{document}

enter image description here

There is no need to check that the index is less than the count of the sequence, unless you want to add error checking. You see that \cp{5} in the first block prints nothing.

Finally, the name of a sequence should end with _seq.

2
  • probably you should explain why you replaced all concurrences of \textbackslash with `\string'. A backslash is not right for '{' and '}'? Commented Jun 20 at 11:04
  • @user1850133 The typesetting of \textbackslash{}cp is truly horrible… And \verb wouldn't work inside the loop.
    – egreg
    Commented Jun 20 at 12:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .