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

I have a macro like this:

    Animals in the farm: #1.

So using \listanimals{pig / sheep / cow / duck / goat} will print:

Animals in the farm: pig / sheep / cow / duck / goat.

I want to know, can I have other macros display only certain information within that parameter?

For example:

\listfirsttwoanimals{pig / sheep / cow / duck / goat}

First two animals: pig / sheep.

\listanimalsafterfirsttwo{pig / sheep / cow / duck / goat}

Animals after the first two: cow / duck / goat.

share|improve this question
up vote 8 down vote accepted
\newcommand\listfirsttwoanimals[1]{First two animals:
\newcommand\listanimalsafterfirsttwo[1]{Animals after first two:
\listfirsttwoanimals{pig / sheep / cow / duck / goat}

\listanimalsafterfirsttwo{pig / sheep / cow / duck / goat}

This assumes that the agrument always has more than two animals, so, for example,\listfirsttwoanimals{pig / sheep } returns no animals, while \listfirsttwoanimals{pig / sheep /} gives pig and sheep. xstring has functions like \IfSubStr which may be used to test this and return, for example, "I have only N animals" if N<3.

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.