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:

\newcommand{\listanimals}[1]{
    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
\documentclass{article}
\usepackage{xstring}
\newcommand\listfirsttwoanimals[1]{First two animals:
  \StrBefore[2]{#1}{/}}
\newcommand\listanimalsafterfirsttwo[1]{Animals after first two:
  \StrBehind[2]{#1}{/}} 
\begin{document}     
\listfirsttwoanimals{pig / sheep / cow / duck / goat}

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

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

 
discard

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.