Iterate through list to generate references

I would like to create a new command, in which I pass a comma-separated list, which then will be iterated to create refs.

Example:

\iteratecommand{ref1, ref2, ref3}


And within the iteratecommand it should go through the list and create \ref{ref1}, \ref{ref2}, \ref{ref3}.

EDIT:

I am very sorry. I was in a rush and was too vague with my explanation. What I want to achieve is to have a macro that reduces my typing. Currently I have this:

\begin{description}
\item[Static Title] \hfill \\
\ref{ref1}, \ref{ref2}
\end{description}


I would like to have a macro in which I only pass the refs and it will output the description with the references. So instead of having:

\documentclass{article}
\begin{document}
\begin{description}
\item[Static Title] \hfill \\
\ref{ref1}, \ref{ref2}
\end{description}

\begin{description}
\item[Static Title] \hfill \\
\ref{ref1}, \ref{ref2}
\end{description}

\begin{description}
\item[Static Title] \hfill \\
\ref{ref1}, \ref{ref2}
\end{description}
\end{document}


I would like to have something like this:

\documentclass{article}
\begin{document}
\reff{ref1, ref2}

\reff{ref1, ref2}

\reff{ref1, ref2}
\end{description}
\end{document}


I hope it's clearer now. It just came to me that I probably could use one of the answers given and put it inside another macro that handles the description part?

• The package cleveref offers such possibility. – Marco Daniel Jun 11 '12 at 9:39

Here's a \reff command (that works also with one reference):

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\reff}{m}
{
\seq_set_split:N \l_cherrung_reff_seq { , }{ #1 }
\seq_pop_right:NN \l_cherrung_reff_seq \l_cherrung_reff_tl
\seq_map_inline:Nn \l_cherrung_reff_seq { \ref{##1},~ }
\ref{\tl_use:N \l_cherrung_reff_tl}
}
\seq_new:N \l_cherrung_reff_seq
\tl_new:N \l_cherrung_reff_tl
\ExplSyntaxOff
\newcounter{test}
\begin{document}
\refstepcounter{test}\label{a}
\refstepcounter{test}\label{b}
\refstepcounter{test}\label{c}

\reff{a,b,c}

\end{document}


The output is

1, 2, 3

as expected.

You can use this command for other purposes: add

\newcommand{\staticdesc}[1]{%
\begin{description}
\item[Static Title] \hfill \\
\reff{#1}
\end{description}}


after \ExplSyntaxOff and \staticdesc{ref1,ref2} will be equivalent to typing

\begin{description}
\item[Static Title] \hfill \\
\ref{ref1}, \ref{ref2}
\end{description}

• Yes, that was what I had in mind after updating my initial post and actually did it before I checked on here again. Works like a charm. Thanks. – cherrung Jun 11 '12 at 13:43

You can also use the package xparse with the internal function SplitList:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\NewDocumentCommand \iteratecommand {> { \SplitList { , } } m }
{%
\ProcessList {#1} {\myref }
}
\NewDocumentCommand{\myref}{m}{\ref{#1},~}
\begin{document}
\begin{align}
x\label{ref1}\\y\label{ref2}\\z\label{ref3}
\end{align}
\iteratecommand{ref1, ref2, ref3}

\end{document}


Of course with the current method the last reference isn't handled separate.

However the package cleveref has some internal functions to handle such input.

\makeatletter
\@for\f:=ref1, ref2, ref3\do{\ref{\f}}


\documentclass{article}

\makeatletter
\def\reff#1{\@for\f:=#1\do{\ref{\f}}}
\makeatother

\newcounter{test}
\begin{document}
\refstepcounter{test}\label{a}
\refstepcounter{test}\label{b}
\refstepcounter{test}\label{c}

\reff{a,b,c}

\end{document}

• Could you elaborate a little further, maybe with a MWE? I'm not quite sure how to call this command. – cherrung Jun 11 '12 at 9:11
• @cherrung: \@for is a loop. You can use it inside a macro. – Marco Daniel Jun 11 '12 at 9:40
• It's the job of the person asking the question to provide the MWE:-) If your question had has a document that worked with three explicit \ref I'd have demonstrated how to use the \@for loop on that example. But basically putting the code in my answer into your document should work (although normally you'd hide that syntax behind another macro – David Carlisle Jun 11 '12 at 11:09
• Oh I see @egreg was kinder and made a MWE, so here's his MWE adapted to use the loop I'd suggested. – David Carlisle Jun 11 '12 at 11:13

You can also do it with the etoolbox package

\documentclass{article}
\usepackage{etoolbox}
\newcounter{iref}
\newcommand{\reff}[1]{\def\do##1{\stepcounter{iref}\ifnumgreater{\value{iref}}{1}{, }{}\ref{##1}}\docsvlist{#1}}

\newcounter{test}
\begin{document}
\refstepcounter{test}\label{a}
\refstepcounter{test}\label{b}
\refstepcounter{test}\label{c}

\reff{a,b,c}
\end{document}