Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
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
1  
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

2 Answers 2

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:

\documentclass{article}
\usepackage{pgffor}
\begin{document}

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

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}{...}

and

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

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\newcount\nra
\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
}
\end{document}

For the second version, similarly

\documentclass{article}
\usepackage{pgffor,pgfmath}
\def\amspace{}
\begin{document}
Some text\foreach\x[count=\xi]in{1pt,2pt,...pt,10pt}{%
         \amspace\texttt{\x(\xi)}%
         \gdef\amspace{\space}
         \ifnum\xi>2\unskip\breakforeach\fi
}continues.
\end{document}

\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

 
discard

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.