# \ref with ranges into enumerate environment

I would like to reference items from a list and get a compressed (not that important) and sorted output:

\documentclass{article}
\begin{document}
\begin{enumerate}
\item \label{a} Was.
\item \label{b} It.
\item \label{c} A.
\item \label{d} Cat.
\item \label{e} I.
\item \label{f} Saw.
\end{enumerate}

See points \ref{a, b, c, e, f}.
\end{document}


Obviously, this is not producing the desired result, which would be "See points 1, 2, 3, 5, 6." The compressed version "1-3" is not that important for me; but hyperref working would be nice. Compare to the cite package. Which even allows/removes blanks in the argument, very nice.

I know cleveref but I am not sure if it can help here, as I do not (want to) define a new environment - and I am not sure how to tell cleveref that it should track this new environment, anyways...

I want to use this inside another command, so "See points \ref{a}, \ref{b}" is not an option. And yucky, anyways.

Searched the Internet, found nil. Many thx.

• The reference to the cite package is somewhat confusing, since what you're trying to do is not to generate numeric (sorted, compressed) citation call-outs.
– Mico
Nov 29 '16 at 17:41

## 2 Answers

Cleveref provides a good format out of the box and allows for customisation to accommodate your request.

\documentclass{article}
\usepackage{cleveref}
\crefname{enumi}{point}{points} % default is "item"
\crefrangelabelformat{enumi}{#3#1#4--#5#2#6} % default uses "to" instead of "--"
\begin{document}
\begin{enumerate}
\item \label{a} Was.
\item \label{b} It.
\item \label{c} A.
\item \label{d} Cat.
\item \label{e} I.
\item \label{f} Saw.
\end{enumerate}

See \cref{a,b,c,e,f}.
\end{document}


Note that no spaces are allowed after the commas (spaces can be used in labels!).

\documentclass{article}

\usepackage{xparse,hyperref}

\ExplSyntaxOn
\AtBeginDocument{
\let\oldref\ref
\RenewDocumentCommand{\ref}{ m }{
\def\@csvsep{\def\@csvsep{, ~}}% https://tex.stackexchange.com/a/89187/5764
\clist_map_inline:nn { #1 } { \@csvsep \oldref{##1}}% https://tex.stackexchange.com/a/159132/5764
}
}
\ExplSyntaxOff

\begin{document}

\begin{enumerate}
\item \label{a} Was.
\item \label{b} It.
\item \label{c} A.
\item \label{d} Cat.
\item \label{e} I.
\item \label{f} Saw.
\end{enumerate}

See points \ref{a, b, c, e, f} or~\ref{d}.

\end{document}