A claim is made at pgffor: Special treatment for last item in \foreach-list that the following works. But I can't get it to work. What have I missed?
\newcount\nra
\foreach[count=\nx]\x in {1--,2--,...,10--}{%
\advance\nra by1
\ifnum\nra>2 \breakforeach\fi
\texttt{\x (\the\nra)}\space
}
Also, please how do I get the following to work with \foreach?
\def\amspace{\let\amspace\space}
\newcount\nra
\foreach\x in {1pt,2pt,...,10pt}{%
\advance\nra by1\relax
\ifnum\nra>2 \breakforeach\fi
\amspace\texttt{\x (\the\nra)}%
}
EDIT (2012/06/19)
This is now a community question: everyone can edit it or provide his/her suggestions below
I have reopened this question because I want to seek the opinion of users of \foreach on which of the following format is to be preferred for an ellipsis list, i.e., a list to be completed. Suggestions of other syntaxes are welcome.
\foreach currently supports the rather queer syntax, which is given on p.505 of the PGF-2.10 manual (just before the section titled special handling of pairs):
{0\pi,0.5\pi,...\pi,3\pi}
{A_1,..._1,H_1}
{1--, 4-- ,...--, 10--}
{1pt, 4pt, ...pt, 10pt}
Proposals
For incomplete (ellipsis) lists, the user should include optional key values after the loop list, as follows. This will significantly speed up list completion and reduce the possibility of mistaken identity.
Target list:
{a,B,1,5,A^1,A^3,A^5,A^7,A^9}
Syntax for \newforeach:
\newforeach [<conventional.keys>] \x in {a,B,1,5,...}[
start = A^1,
end = A^9,
step = 2,
coupler = ^,
grow right, % increment right of <coupler>
grow left, % increment left of <coupler>
macro elements, % elements are macros
... more keys
]
{<loop.callback>}
<conventional.keys> will be the usual \foreach keys/variables with some few new ones.
Examples of when elements are macros:
\pgfmathsetmacro\start{<expr>}
\pgfmathsetmacro\stopp{<expr>}
{\start,...,\stopp}
Reminder
\newforeach will work for arbitrary list separators and subseparators (including active list separators) and will remove the group scoping of computations by \foreach. New user variables will include
*parser = <value> (e.g., {,})
*subparser = <value> (e.g., /)
list is macro = <true> | <false> % the given list is a macro
terminator = <value> (e.g., {;}) % callback terminator
evaluate \x as \y using <formula>
count in \nr all \x satisfying <condition>
store in \cmd all \x satisfying <condition>
apply to all \x satisfying <condition> = <code>
apply to all \x in serial range [a,b] = <code>
at begin loop = <code>
at end loop = <code>
The star sign (*) on parsers will mean "remember and use this parser subsequently, until I change it".
A new boolean \iflastitem will be introduced, which can be toggled independently, on different nesting depths. Item counters (e.g., \currentitemnumber) will also be introduced on all nesting depths without collision between nesting depths. The loop can also look ahead for \nextitem. And in the case of premature termination of the loop with \breaknewforeach, or with a user-inserted list item (\listbreaker), the remainder of the list on the current nesting depth will be available in \listremainder. The \newforeach loop can also be paused with the user-inserted list item \listpauser.
--at the end of...:{1--,2--,...--,10--}. But not sure that is the result you are looking for. Same for the second case:{1pt,2pt,...pt,10pt}will make it compile. Seems a bit "hackish" -- I know coming from me it sounds hypocritical. :-) – Peter Grill Jun 17 '12 at 05:57pgfforhadn't the same idea, though. I can think of a reason why: how\foreach\x in {11,...,91}should be interpreted? If you try{11,...1,91}only nine cycles are performed. – egreg Jun 17 '12 at 10:24{1--,2--,...--,10--}is suggested. But I think a natural syntax is to be preferred. I propose a key/variable calledlist unit, which can be assigned, e.g.,ptor---. – Ahmed Musa Jun 17 '12 at 23:48