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

The following command of the forarray package

\ForEach{,}{\thislevelitem(\the\thislevelcount) : }}{x,y,z}

produces the output

x(1) : y(2) : z(3) :

I want to have the same output without the last ":".

The forarray package does not provide any command like \maxount to test if \thislevelcount == \maxcount. Surly I could test for the first item \thislevelcount == 0 but what to do if this cannot be done for some reason?

How can I process the last item separately if the number of items is variable/unknown?

share|improve this question
up vote 1 down vote accepted

You can cheat and prepend : to all the items except for the first

  \ForEach{,}{\ifnum\thislevelcount=1\else : \fi\thislevelitem(\the\thislevelcount)}{x,y,z}

or you can use the totcount package to get the number of items in the array

  \ForEach{,}{\stepcounter{myarray}\thislevelitem(\the\thislevelcount)\ifnum\thislevelcount=\totvalue{myarray}\else : \fi}{x,y,z}    
share|improve this answer
The first is okay but it’s not the last item. However, I used the first item solution (as mentioned in my question). I do not understand the second solution. How does the totcount package know the number of total items in your example? – Ronny Jun 12 '12 at 12:06
@Ronny knowing the total number of items is the magic of the totcount package. Basically the package requires two passes. On the first pass it writes the final value of the counter to the aux file and on the second pass it reads the value. – StrongBad Jun 12 '12 at 13:09
Good to know, thanks! – Ronny Jun 12 '12 at 13:54

You can also use the \indrisloop macro of the catoptions package.


enter image description here

share|improve this answer

Similar to my comments in pgffor: Special treatment for last item in \foreach-list the other answers here provide solutions to accomplish the task at hand, but don't address the problem in the title of the question.

One way to detect the last list member and treat it differently is to count the number of members at the start of the list processing and compare that to \the\thislevelcount to know when we have come to the last one:

enter image description here


  • I am using the xstring package for the numerical comparison as I prefer it's syntax, but this could easily be adapted to not require that package.





    \IfEq{\the\thislevelcount}{\the\value{TotalNumberOfListMembers}}{}{: }% 

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.