From 5d1f4e0ea6ccbfab94571fce8588096cce12d626 Mon Sep 17 00:00:00 2001 From: Henri Menke Date: Sat, 21 Nov 2020 19:43:00 +0100 Subject: [PATCH] pgffor: new expand list option --- .../pgf/text-en/pgfmanual-en-pgffor.tex | 21 +++++++++++++++++++ tex/generic/pgf/utilities/pgffor.code.tex | 8 ++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/doc/generic/pgf/text-en/pgfmanual-en-pgffor.tex b/doc/generic/pgf/text-en/pgfmanual-en-pgffor.tex index 34f3aab73..94a41fbfe 100644 --- a/doc/generic/pgf/text-en/pgfmanual-en-pgffor.tex +++ b/doc/generic/pgf/text-en/pgfmanual-en-pgffor.tex @@ -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} diff --git a/tex/generic/pgf/utilities/pgffor.code.tex b/tex/generic/pgf/utilities/pgffor.code.tex index cdb77b313..65b62b8cf 100644 --- a/tex/generic/pgf/utilities/pgffor.code.tex +++ b/tex/generic/pgf/utilities/pgffor.code.tex @@ -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% @@ -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{%