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.

  • 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 01 '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 01 '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 01 '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 01 '16 at 16:42
  • Hm... I have no clue what is actually asked here and how the question fits the answer (or vice versa). Maybe you can rephrase your question with the intended output? Even if your problem is solved now, it would help others with a similar problem. – Daniel Jan 01 '16 at 16:50
  • 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 01 '16 at 21:02
  • @TorbjørnT.: Yes that would have been an accepted answer on the question. Now I removed that context of pandoc. It seems if I accepted this as an answer, it would be pandoc specific. If there was a pandoc stack exchange, I'd have asked the question there and accepted your solution as an answer : ) However, I thought I easily do it with plain latex as well. Turned out to be a bit more difficult, but here we are. – Zelphir Kaltstahl Jan 01 '16 at 21:37

2 Answers2

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

egreg
  • 1,121,712
  • So it's rather a foreach and an added counter. That makes sense. – Zelphir Kaltstahl Jan 01 '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. \par l.32 No pages of output.` – Zelphir Kaltstahl Jan 01 '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 01 '16 at 17:21
  • @Alenanno I can't read the OP's mind and there is no workable example in the question. – egreg Jan 01 '16 at 17:26
  • @Zelphir People can't read your mind. – egreg Jan 01 '16 at 17:26
  • @egreg I meant, if there's a foreach list like a/{1,2,3}, b/{4,5,6,7}, can you make it tell you: a has 3 elements, b has 4? – Alenanno Jan 01 '16 at 17:27
  • @Alenanno Yes, of course you can, by doing \xdef\something{\xi} at each step, so when the \foreach ends, you can examine the meaning of \something. You can't do it in advance, though, so a double \foreach is needed. – egreg Jan 01 '16 at 17:30
  • @egreg Is that command supposed to be inside the \foreach? Ah ok. :) – Alenanno Jan 01 '16 at 17:31
  • @egreg: So you are saying, that the language construct of a foreach loop in Latex is not usable everywhere in the Latex code the same way? Or what did you mean by "people can't read your mind"? A little bit of explanation please! – Zelphir Kaltstahl Jan 01 '16 at 17:49
  • @Zelphir You give no sensible use case; and I'm afraid the edit doesn't clarify. – egreg Jan 01 '16 at 18:08
  • @egreg removed the use case, because everyone got hung up at the word pandoc and overlooked (or didn't care) that this can be done in pure Latex, not minding what kind of tool I used later on. So I made the question more general, only talking about Latex foreach. Now you're telling me you want the use case. This could in its current form serve well as some kind of "wiki" or "howto" post, becaues it should be use case independent. – Zelphir Kaltstahl Jan 01 '16 at 18:17
  • @Zelphir LaTeX is a document syntax. Much of the programming stuff is either done in TeX or by using add-on packages. The latter tend to have particular specialisms. You've picked \foreach from pgf so the answer here quite reasonably uses it. If you want to do something different you need to explain. In particular, it sounds like you've tried to use \foreach in an expansion context, which if you provide a full example will be obvious. – Joseph Wright Jan 01 '16 at 19:30
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.

Werner
  • 603,163