TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

\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?

\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):

{1--, 4-- ,...--, 10--}
{1pt, 4pt, ...pt, 10pt}


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:


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

<conventional.keys> will be the usual \foreach keys/variables with some few new ones.

Examples of when elements are macros:



\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.

share|improve this question
According to the linked answer you need another -- 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 5:57
@PeterGrill: I think my syntax is the natural one and the loop should handle it like that, provided it was meant to handle such a list. The syntax you're suggesting seems to me to be a game of salamander; or is it poker? – Ahmed Musa Jun 17 '12 at 8:08
I am not suggesting that that is good syntax, just that that is what the linked question shows, and does work. – Peter Grill Jun 17 '12 at 9:27
I agree that the proposed syntax is more natural. Whoever developed pgffor hadn'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
Thanks to Peter and Percusse, or is it all lowercase (percusse)? I still can't find in the user manual where the syntax {1--,2--,...--,10--} is suggested. But I think a natural syntax is to be preferred. I propose a key/variable called list unit, which can be assigned, e.g., pt or ---. – Ahmed Musa Jun 17 '12 at 23:48
up vote 9 down vote accepted

One has always to remember that \foreach processes the token list to be repeated inside a group, so redefinitions and assignments must be global:


*\foreach\x in {1pt,2pt,...pt,10pt}{%
  \global\advance\nra by1\relax
  \amspace\texttt{\x (\the\nra)}%
  \ifnum\nra>2 \breakforeach\fi

This avoids redefining \amspace many times as in percusse's answer.

About the syntax: one can think to something like

\foreach\x in {11,...,31}{...}


\foreach\x in {11,...1,31}{...}

In the former case 21 cycles will be performed, in the latter only three.

share|improve this answer
Hmm I forgot to put it inside the \ifnum to avoid redefinitions but still this is much better. – percusse Jun 17 '12 at 16:39

For the first version, though a little disappointing, you need, pgfmath module. But note that, if you remove the [count=\nx], you can also remove the pgfmath dependency. Also the detail Peter commented on, that extra -- is needed. The motivation behind that is, I guess, to strip off the non-increasing parts and then evaluate the initial, incremental and the final numbers from a given list.

\foreach\x in {1--,2--,...--,10--}{%
  \global\nra\numexpr\the\nra+1 %or \global\advance\nra by1
  \texttt{\x (\the\nra)}\space
    \ifnum\nra>2 \breakforeach\fi

For the second version, similarly

Some text\foreach\x[count=\xi]in{1pt,2pt,...pt,10pt}{%

\breakforeach command does not terminate the remaining code for that item but prevents the loop run again. Hence it might be a better idea to leave it as the last line in the \foreach. If I remember correctly there was a note about this in the manual.

I hope I got the space argument right otherwise please discard this.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.