%%%
% Itération
%%%
\def\filedateIteration{2025/07/30}%
\def\fileversionIteration{0.1a}%
\message{-- \filedateIteration\space v\fileversionIteration}%
%
\setKVdefault[Iteration]{Depart=4,Repetitions=3,Images=false,Ratio=false,Fleches=false,Debord=1,Completer=false,Solution=false,CouleurSol=Evidence,Ecart={ ; }}%

\makeatletter
\newlength{\PfC@lengthiteration}
\newlength{\PfC@diffiteration}

\NewDocumentCommand\PfC@TikzMark{m}{%
  \tikz[remember picture,baseline,inner sep=0pt]{%
    \node[name=Iteration-\theNbDistri,anchor=base] {$#1$};}%
  \stepcounter{NbDistri}%
}%

\NewDocumentCommand\Iteration{om}{%
  \setcounter{NbDistri}{0}%
  \setlength{\PfC@lengthiteration}{0pt}%
  \setlength{\PfC@diffiteration}{0pt}%
  \useKVdefault[Iteration]%
  \setKV[Iteration]{#1}%
  \ifPfCDeuxColonnes\relax\setKV[Iteration]{Ecart=\par}\fi%
  \setsepchar{ }\ignoreemptyitems%
  \readlist*\PfM@ListeOp{#2}%
  \reademptyitems%
%  Lecture Itération : \showitems\PfM@ListeOp[]\par
  \ifboolKV[Iteration]{Ratio}{%
    \edef\PfM@ListeCompoAMelanger{}%
    \edef\PfM@RetiensLesIterations{0}%
    \xintFor* ##1 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
      \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOp[\fpeval{##1*2-1}]*\useKV[Iteration]{Repetitions}}}}\do{%
        \edef\PfM@ListeCompoAMelanger{\PfM@ListeCompoAMelanger,##1}%
        \edef\PfM@RetiensLesIterations{\fpeval{\PfM@RetiensLesIterations+1}}%
      }%
    }%
    % Ajout d'éléments par la clé Debord.
    \ifnum\useKV[Iteration]{Debord}>0\relax%
      \edef\PfM@NbElements{0}%
      \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
        \xintFor* ##3 in{\xintSeq{1}{\PfM@ListeOp[\fpeval{##2*2-1}]}}\do{%
          \edef\PfM@NbElements{\fpeval{\PfM@NbElements+1}}%
          \ifnum\useKV[Iteration]{Debord}<\PfM@NbElements\relax%
          \else%
            % \PfM@ListeOp[\fpeval{2*##2}]%
            \edef\PfM@ListeCompoAMelanger{\PfM@ListeCompoAMelanger,##2}%
            \edef\PfM@RetiensLesIterations{\fpeval{\PfM@RetiensLesIterations+1}}%
          \fi%
        }%
      }%
    \fi%
    \MelangeListe{\PfM@ListeCompoAMelanger}{\PfM@RetiensLesIterations}%
    \setsepchar{,}\ignoreemptyitems%
    \readlist*\PfM@ListeRatioAAfficher{\faa}%
    \reademptyitems%
    \xintFor* ##1 in{\xintSeq{1}{\PfM@RetiensLesIterations}}\do{%
      \itemtomacro\PfM@ListeRatioAAfficher[##1]\PfM@RatioTiti%
      \PfM@ListeOp[\fpeval{2*\PfM@RatioTiti}]%
    }%
  }{%
    \ifboolKV[Iteration]{Images}{%
      \xintFor* ##1 in{\xintSeq{1}{\useKV[Iteration]{Repetitions}}}\do{%
        \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
          \xintFor* ##3 in{\xintSeq{1}{\PfM@ListeOp[\fpeval{##2*2-1}]}}\do{%
            \setbox1=\hbox{\PfM@ListeOp[\fpeval{2*##2}] }%
            \addtolength{\PfC@lengthiteration}{\wd1}%
              \setlength{\PfC@diffiteration}{\fpeval{\linewidth-\PfC@lengthiteration}pt}%
            \xintifboolexpr{\fpeval{\PfC@diffiteration}>10}{}{\par\setlength{\PfC@lengthiteration}{0pt}}%
            \PfM@ListeOp[\fpeval{2*##2}]\if@twocolumn\relax{} \fi%
          }%
        }%
      }%
      % Ajout d'éléments par la clé Debord.
      \ifnum\useKV[Iteration]{Debord}>0\relax%
        \edef\PfM@NbElements{0}%
        \xintFor* ##2 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen/2}}}\do{%
          \xintFor* ##3 in{\xintSeq{1}{\PfM@ListeOp[\fpeval{##2*2-1}]}}\do{%
            \edef\PfM@NbElements{\fpeval{\PfM@NbElements+1}}%
            \ifnum\useKV[Iteration]{Debord}<\PfM@NbElements\relax%
            \else%
              \PfM@ListeOp[\fpeval{2*##2}]\if@twocolumn\relax{} \fi%
            \fi%
          }%
        }%
      \fi%
    }{%
      \ifboolKV[Iteration]{Completer}{\edef\PfM@Repetitions{\fpeval{\useKV[Iteration]{Repetitions}-1}}}{\edef\PfM@Repetitions{\fpeval{\useKV[Iteration]{Repetitions}}}}%
      \edef\PfM@ValeurIntermediaire{\useKV[Iteration]{Depart}}%
      \edef\PfM@ListeSuiteNb{\useKV[Iteration]{Depart}}%
      \xintFor* ##1 in{\xintSeq{1}{\PfM@Repetitions}}\do{%
        \edef\PfMk{##1}%
        \xintFor* ##2 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
          \edef\PfM@ValeurIntermediaire{\fpeval{\PfM@ValeurIntermediaire\PfM@ListeOp[##2]}}%
          \edef\PfM@ListeSuiteNb{\PfM@ListeSuiteNb,\PfM@ValeurIntermediaire}%
        }%
      }%
      \ifboolKV[Iteration]{Completer}{%
        \xintFor* ##3 in{\xintSeq{1}{\useKV[Iteration]{Debord}}}\do{%
          \xintFor* ##2 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
            \edef\PfMk{\fpeval{\PfMk+1}}%
            \edef\PfM@ValeurIntermediaire{\fpeval{\PfM@ValeurIntermediaire\PfM@ListeOp[##2]}}%
            \edef\PfM@ListeSuiteNb{\PfM@ListeSuiteNb,\PfM@ValeurIntermediaire}%
          }%
        }%
      }{}%
      \setsepchar{,}\ignoreemptyitems%
      \readlist*\PfM@ListeRecupere{\PfM@ListeSuiteNb}%
      \reademptyitems%
      %\ifPfCDeuxColonnes\relax%
      %  \xintFor* ##1 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen*\PfM@Repetitions+1}}}\do{%
      %    \PfC@TikzMark{\num{\PfM@ListeRecupere[##1]}}\xintifForLast{}{\par}%
      %  }%
      %\else%
        \xintFor* ##1 in{\xintSeq{1}{\fpeval{\PfM@ListeOplen*\PfM@Repetitions+1}}}\do{%
          \PfC@TikzMark{\num{\PfM@ListeRecupere[##1]}}\xintifForLast{}{\useKV[Iteration]{Ecart}}%
        }%
      %\fi%
      \ifboolKV[Iteration]{Completer}{%
        \ifboolKV[Iteration]{Solution}{%
          %\ifPfCDeuxColonnes\relax%
          %  \xintFor* ##3 in{\xintSeq{1}{\useKV[Iteration]{Debord}}}\do{%
          %    \xintFor* ##1 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
          %      \edef\PfM@Titi{\fpeval{\PfM@ListeOplen*\PfM@Repetitions+1+##1+(##3-1)*\PfM@ListeOplen}}%
          %      \par\PfC@TikzMark{\textcolor{\useKV[Iteration]{CouleurSol}}{\num{\PfM@ListeRecupere[\PfM@Titi]}}}%
          %    }%
          %  }%
          %\else%
            \xintFor* ##3 in{\xintSeq{1}{\useKV[Iteration]{Debord}}}\do{%
              \xintFor* ##1 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
                \edef\PfM@Titi{\fpeval{\PfM@ListeOplen*\PfM@Repetitions+1+##1+(##3-1)*\PfM@ListeOplen}}%
                \useKV[Iteration]{Ecart}\PfC@TikzMark{\textcolor{\useKV[Iteration]{CouleurSol}}{\num{\PfM@ListeRecupere[\PfM@Titi]}}}%
              }%
            }%
          %\fi%
        }{%
          \xintFor* ##3 in{\xintSeq{1}{\useKV[Iteration]{Debord}}}\do{%
            \xintFor* ##1 in{\xintSeq{1}{\PfM@ListeOplen}}\do{%
             \useKV[Iteration]{Ecart}\pointilles[25pt]{}%
            }%
          }%
        }%
      }{}%
      \ifPfCDeuxColonnes\relax\else.\fi%
      \ifboolKV[Iteration]{Fleches}{%
        \ifnum\PfM@ListeOplen=1\relax%
          %Fleches avec une opération !
          \IfSubStr{\PfM@ListeOp[1]}{*}{%
            \IfSubStr{\PfM@ListeOp[1]}{+}{%
%              Une multiplication et une addition :)
              \StrBefore{\PfM@ListeOp[1]}{+}[\PfMPartieMul]%
%              PartieMul : \PfMPartieMul\par
              \StrBehind{\PfM@ListeOp[1]}{+}[\PfMPartiePlus]%
%              PartiePlus : \PfMPartiePlus\par
              \StrSubstitute{\PfMPartieMul}{*}{\times}[\PfMPartieMul]%
%              PartieMul : \PfMPartieMul\par
              \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
                \ifPfCDeuxColonnes\relax%
                  \begin{tikzpicture}[remember picture,overlay]%
                  \coordinate[xshift=2em,yshift=0em] (IterationA) at ($(Iteration-##4.east)!0.5!(Iteration-\fpeval{##4+1}.east)$);%
                  \draw[-stealth,transform canvas={xshift=0.5em},DarkGreen] (Iteration-##4.east) to node[pos=0.85,above,inner sep=1pt]{\tiny$\PfMPartieMul$}(IterationA);%
                  \draw[-stealth,transform canvas={xshift=0.5em},DarkGreen] (IterationA) to node[pos=0.85,above,inner sep=2pt]{\tiny$+\num{\PfMPartiePlus}$}(Iteration-\fpeval{##4+1}.east);%
                \end{tikzpicture}%
              \else%
                \begin{tikzpicture}[remember picture,overlay]%
                  \coordinate[xshift=0em,yshift=2em] (IterationA) at ($(Iteration-##4)!0.5!(Iteration-\fpeval{##4+1})$);%
                  \draw[-stealth,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[pos=0.65,below]{\footnotesize$\PfMPartieMul$}(IterationA);%
                  \draw[-stealth,transform canvas={yshift=0.5em}] (IterationA) to node[pos=0.65,above]{\footnotesize$+\num{\PfMPartiePlus}$}(Iteration-\fpeval{##4+1}.north);%
                \end{tikzpicture}%
              \fi%
              }%
            }{\IfSubStr{\PfM@ListeOp[1]}{-}{%
%                une multiplication et une soustraction :)
                \StrBefore{\PfM@ListeOp[1]}{-}[\PfMPartieMul]%
                \StrBehind{\PfM@ListeOp[1]}{-}[\PfMPartieMoins]%
                \StrSubstitute{\PfMPartieMul}{*}{\times}[\PfMPartieMul]%
                \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
                  \ifPfCDeuxColonnes\relax%
                    \begin{tikzpicture}[remember picture,overlay]%
                      \coordinate[xshift=2em,yshift=0em] (IterationA) at ($(Iteration-##4.east)!0.5!(Iteration-\fpeval{##4+1}.east)$);%
                      \draw[-stealth,transform canvas={xshift=0.5em}] (Iteration-##4.east) to node[pos=0.7,above,inner sep=1pt]{\tiny$\PfMPartieMul$}(IterationA);%
                      \draw[-stealth,transform canvas={xshift=0.5em}] (IterationA) to node[pos=0.9,above,inner sep=2pt]{\tiny$-\num{\PfMPartieMoins}$}(Iteration-\fpeval{##4+1}.east);%
                    \end{tikzpicture}%
                  \else%
                    \begin{tikzpicture}[remember picture,overlay]%
                      \coordinate[xshift=0em,yshift=2em] (IterationA) at ($(Iteration-##4)!0.5!(Iteration-\fpeval{##4+1})$);%
                      \draw[-stealth,transform canvas={yshift=0.5em},Purple] (Iteration-##4.north) to node[pos=0.65,below]{\footnotesize$\PfMPartieMul$}(IterationA);%
                      \draw[-stealth,transform canvas={yshift=0.5em},Purple] (IterationA) to node[pos=0.65,above]{\footnotesize$-\num{\PfMPartieMoins}$}(Iteration-\fpeval{##4+1}.north);%
                    \end{tikzpicture}%
                  \fi%
                }%
              }{%
%                une seule et unique opération : \PfM@ListeOp[1]
                \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
                  \IfSubStr{\PfM@ListeOp[1]}{*}{\StrBehind{\PfM@ListeOp[1]}{*}[\PfMPartieMul]\edef\PfMTiti{\times\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{/}{\StrBehind{\PfM@ListeOp[1]}{/}[\PfMPartieMul]\edef\PfMTiti{\div\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{+}{\StrBehind{\PfM@ListeOp[1]}{+}[\PfMPartieMul]\edef\PfMTiti{+\num{\PfMPartieMul}}}{}%
                  \IfSubStr{\PfM@ListeOp[1]}{-}{\StrBehind{\PfM@ListeOp[1]}{-}[\PfMPartieMul]\edef\PfMTiti{-\num{\PfMPartieMul}}}{}%
                  \ifPfCDeuxColonnes\relax%
                    \begin{tikzpicture}[remember picture,overlay]%
                    \draw[-stealth,out=-30,in=30,transform canvas={xshift=0.5em}] (Iteration-##4.east) to node[midway,right]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.east);%
                  \end{tikzpicture}%
                  \else%
                    \begin{tikzpicture}[remember picture,overlay]%
                      \draw[-stealth,out=30,in=150,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[midway,above]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.north);%
                    \end{tikzpicture}%
                  \fi%
                }%
              }%
            }%
          }{%
%            une seule et unique opération : \PfM@ListeOp[1]
            \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
              \IfSubStr{\PfM@ListeOp[1]}{*}{\StrBehind{\PfM@ListeOp[1]}{*}[\PfMPartieMul]\edef\PfMTiti{\times\num{\PfMPartieMul}}}{}%
              \IfSubStr{\PfM@ListeOp[1]}{/}{\StrBehind{\PfM@ListeOp[1]}{/}[\PfMPartieMul]\edef\PfMTiti{\div\num{\PfMPartieMul}}}{}%
              \IfSubStr{\PfM@ListeOp[1]}{+}{\StrBehind{\PfM@ListeOp[1]}{+}[\PfMPartieMul]\edef\PfMTiti{+\num{\PfMPartieMul}}}{}%
              \IfSubStr{\PfM@ListeOp[1]}{-}{\StrBehind{\PfM@ListeOp[1]}{-}[\PfMPartieMul]\edef\PfMTiti{-\num{\PfMPartieMul}}}{}%
              \ifPfCDeuxColonnes\relax%
                \begin{tikzpicture}[remember picture,overlay]%
                  \draw[-stealth,out=-30,in=30,transform canvas={xshift=0.5em}] (Iteration-##4.east) to node[midway,right]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.east);%
                \end{tikzpicture}%
              \else%
                \begin{tikzpicture}[remember picture,overlay]%
                  \draw[-stealth,out=30,in=150,transform canvas={yshift=0.5em}] (Iteration-##4.north) to node[midway,above]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.north);%
                \end{tikzpicture}%
              \fi%
            }%
          }%
        \else%
          \xintFor* ##4 in{\xintSeq{0}{\fpeval{\theNbDistri-2}}}\do{%
            \modulo{##4}{\PfM@ListeOplen}%
            \edef\PfM@Test{\PfM@ListeOp[\fpeval{\the\remainder+1}]}%
            \IfSubStr{\PfM@Test}{*}{\StrBehind{\PfM@Test}{*}[\PfMPartieMul]\edef\PfMTiti{\times\num{\PfMPartieMul}}}{}%
            \IfSubStr{\PfM@Test}{/}{\StrBehind{\PfM@Test}{/}[\PfMPartieMul]\edef\PfMTiti{\div\num{\PfMPartieMul}}}{}%
            \IfSubStr{\PfM@Test}{+}{\StrBehind{\PfM@Test}{+}[\PfMPartieMul]\edef\PfMTiti{+\num{\PfMPartieMul}}}{}%
            \IfSubStr{\PfM@Test}{-}{\StrBehind{\PfM@Test}{-}[\PfMPartieMul]\edef\PfMTiti{-\num{\PfMPartieMul}}}{}%
            \ifPfCDeuxColonnes\relax%
              \begin{tikzpicture}[remember picture,overlay]%
              \draw[-stealth,out=-30,in=30,transform canvas={xshift=0.5em}] (Iteration-##4.east) to node[midway,right]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.east);%
            \end{tikzpicture}%
            \else%
              \begin{tikzpicture}[remember picture,overlay]%
              \draw[-stealth,out=30,in=150,transform canvas={yshift=0.5em},red] (Iteration-##4.north) to node[midway,above]{\footnotesize$\PfMTiti$}(Iteration-\fpeval{##4+1}.north);%
            \end{tikzpicture}%
            \fi%
          }%
        \fi%
      }{}%
    }%
  }%
}%
\makeatother