2

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) }

EDIT#1

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.

7
  • 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. Jan 1, 2016 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, 2016 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. Jan 1, 2016 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, 2016 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. Jan 1, 2016 at 21:02

2 Answers 2

5

You can use the count feature of \foreach:

\documentclass{article}

\usepackage{pgffor}

\begin{document}

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

\end{document}

enter image description here

12
  • So it's rather a foreach and an added counter. That makes sense. Jan 1, 2016 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. Jan 1, 2016 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, 2016 at 17:21
  • @Alenanno I can't read the OP's mind and there is no workable example in the question.
    – egreg
    Jan 1, 2016 at 17:26
  • @Zelphir People can't read your mind.
    – egreg
    Jan 1, 2016 at 17:26
1

You could use a cunning (La)TeX trick:

enter image description here

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\printlist}[2][,]{%
  \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

\begin{document}

\printlist{a,b,c}

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

\printlist{}

\printlist{a}

\end{document}

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.