I want to check if there are more elements in a foreach loop. How can this be done?

This is an example for loop:

\foreach \p/\q in {a/{1,2,3}, b/{4,5,6}}
    \foreach \r in \q { (\r) }


An example of code in a programming language for what I want to achieve would be:

a = [3,4,5,6]
for i, val in enumerate(a):
    print(val, sep='', end='')
    if i < len(a)-1:
        print(',', sep='', end='')

Hope that helps to clarify.

  • Since it seems such a big issue, when I mention that I want to use the template for pandoc and that there is pandoc's extending syntax in it, I'll reword the whole thing into something much simpler. – Zelphir Kaltstahl Jan 1 '16 at 15:15
  • 1
    The question here seems rather different to the initial one. I've therefore reopened and removed what are now 'stale' comments. – Joseph Wright Jan 1 '16 at 15:33
  • @JosephWright: With all due respect, an answer to this question would've been an answer to the other question as well. Ofc everyone is entitled to their own opinion. Thanks for taking care of things. – Zelphir Kaltstahl Jan 1 '16 at 16:30
  • 1
    I don't see that (the earlier version did not use \foreach or even mention pgf), but I am pleased you now have a useful answer. – Joseph Wright Jan 1 '16 at 16:42
  • 1
    Not sure if you're still interested, but as mentioned in a previous comment of mine, adding $sep$ just before the \and in your Pandoc template seems to do the job. At least, that worked with my simple test. – Torbjørn T. Jan 1 '16 at 21:02

You can use the count feature of \foreach:




\foreach \i [count=\xi] in {a,b,c}
  \ifnum\xi>1 and \fi \i

\foreach \i [count=\xi] in {a}
  \ifnum\xi>1 and \fi \i


enter image description here

  • So it's rather a foreach and an added counter. That makes sense. – Zelphir Kaltstahl Jan 1 '16 at 16:32
  • Wouldn't it have to be \and? Seems I can't use your code within the \author{} braces, it causes a warning: Runaway argument? { \foreach \i [count=\xi ] in {AuthorName, Intitute, Germa\ETC. ! Paragraph ended before \author was complete. <to be read again> \par l.32 No pages of output. – Zelphir Kaltstahl Jan 1 '16 at 16:49
  • This answer works with a simple foreach, but will it work with the example from the question? As in, a list within the elements of the foreach? – Alenanno Jan 1 '16 at 17:21
  • @Alenanno I can't read the OP's mind and there is no workable example in the question. – egreg Jan 1 '16 at 17:26
  • @Zelphir People can't read your mind. – egreg Jan 1 '16 at 17:26

You could use a cunning (La)TeX trick:

enter image description here



  \def\itemdelim{\def\itemdelim{#1}}% Item delimiter delayed by one cycle
  \renewcommand*{\do}[1]{\itemdelim##1}% How each item is processed
  \docsvlist{#2}}% Process CSV list



\printlist[ and ]{a,b,c}




The approach is to always print a separator, but the first is only a redefinition of the separator, actually printing nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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