Skip to content

Commit

Permalink
pgffor: new expand list option
Browse files Browse the repository at this point in the history
  • Loading branch information
hmenke committed Nov 21, 2020
1 parent 67295ec commit 5d1f4e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 21 additions & 0 deletions doc/generic/pgf/text-en/pgfmanual-en-pgffor.tex
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,27 @@ \section{Repeating Things: The Foreach Statement}
\end{codeexample}
%
\end{key}

\begin{key}{/pgf/foreach/expand list=\marg{boolean} (default false)}
If this key is set to true the contents of the list are fully expanded
with |\edef| before further processing. This allows using complex
macros which generate a list upon expansion without having to use an
intermediate macro.
%
\begin{codeexample}[]
\def\Iota#1#2{%
\ifnum\numexpr#1\relax<\numexpr#2\relax
\the\numexpr#1\relax,%
\expandafter\Iota\expandafter{\the\numexpr(#1)+1\relax}{#2}%
\else
\the\numexpr#2\relax
\fi}
\foreach [expand list=true] \x in {\Iota{1}{5}} {
\x
}
\end{codeexample}
%
\end{key}
\end{command}

\begin{command}{\breakforeach}
Expand Down
8 changes: 7 additions & 1 deletion tex/generic/pgf/utilities/pgffor.code.tex
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@
\fi%
}

\def\pgffor@expand@list@true{\let\pgffor@expand@list\edef}
\def\pgffor@expand@list@false{\let\pgffor@expand@list\def}
\def\pgffor@macro@list#1{%
\expandafter\pgffor@normal@list\expandafter{#1}}
\def\pgffor@normal@list#1{%
\def\pgffor@values{#1, \pgffor@stop,}%
\pgffor@expand@list\pgffor@values{#1}%
\expandafter\def\expandafter\pgffor@values\expandafter{\pgffor@values, \pgffor@stop,}%
\ifx\pgffor@values\pgffor@emptyvalues
\def\pgffor@values{\pgffor@stop,}%
\fi%
Expand Down Expand Up @@ -619,6 +622,9 @@
count/.code=\pgffor@count@parse#1\pgffor@stop,
parse/.is if=pgffor@assign@parse,
parse/.default=false,
expand list/.is if=pgffor@expand@list@,
expand list/.default=true,
expand list=false,
}

\def\pgffor@assign@parse#1{%
Expand Down

0 comments on commit 5d1f4e0

Please sign in to comment.