0

I would like to define a command that references the first and last items from a \begin{enumerate}[resume] ... \end{enumerate} environment.

Here is what I have done so far:

\documentclass{book}

\usepackage{enumitem}

\newcounter{groupcounter}

\newenvironment{enumgroup}{
  \stepcounter{groupcounter}
  \enumerate[resume]
  \refstepcounter{enumi}
  \label{groupstart:\thegroupcounter}
  \addtocounter{enumi}{-1}
  \addtocounter{groupcounter}{-1}
  \refstepcounter{groupcounter}
}{
  \label{groupend:\thegroupcounter}
  \endenumerate
}

\newcommand{\grouplabel}[1]{\label{group:#1}}
\newcommand{\groupref}[1]{\ref{groupstart:\ref{group:#1}}~--~\ref{groupend:\ref{group:#1}}}

\begin{document}

\begin{enumgroup} \grouplabel{A}
\item lorem
\item ipsum
\item dolor
\item sit
\end{enumgroup}

\begin{enumgroup} \grouplabel{B}
\item amet
\item etc.
\end{enumgroup}

% Uncomment to switch from MWE to MNWE
%First group : \groupref{A} % Should print First group : 1 -- 4
%Second group : \groupref{B} % 5 -- 6

\end{document}

Actually the only interesting line is the one defining the \groupref macro. I believe everything else is alright because of the generated aux file:

\relax 
\newlabel{groupstart:1}{{1}{1}}
\newlabel{group:A}{{1}{1}}
\newlabel{groupend:1}{{4}{1}}
\newlabel{groupstart:2}{{5}{1}}
\newlabel{group:B}{{2}{1}}
\newlabel{groupend:2}{{6}{1}}

So my only problem is that it seems I can't use \ref inside of \ref. I guess I have to protect the inner call somehow but I have no experience with such things ... Can someone tell me how to fix this macro?

3
  • Have you had a look at the ctan.org/pkg/enumitem-zref package? enumitem-zref is a com­pan­ion for the en­u­mitem pack­age that you already use and specializes in references. Mar 15, 2017 at 22:02
  • why do you use the number not the label key you are passing in? \label{groupstart:#1} would seem to be a lot simpler and could be directly accessed with `\ref Mar 15, 2017 at 22:39
  • I just had a look at enumitem-zref but I've seen nothing about first and last item of a list ... Maybe I should look with more attention but David Carlisle's suggestion is very simple and I'll just use his solution :-) Mar 16, 2017 at 6:31

1 Answer 1

1

I don't think you need the indirection via numbered groups which is causing your nested\ref` issue.

enter image description here

\documentclass{book}

\usepackage{enumitem}


\newenvironment{enumgroup}[1]{%
  \enumerate[resume]%
  \refstepcounter{enumi}%
  \def\thisgroup{#1}%
  \label{groupstart:\thisgroup}%
  \addtocounter{enumi}{-1}%
}{%
  \label{groupend:\thisgroup}%
  \endenumerate
}


\newcommand{\groupref}[1]{\ref{groupstart:#1}~--~\ref{groupend:#1}}

\begin{document}

\begin{enumgroup}{A}
\item lorem
\item ipsum
\item dolor
\item sit
\end{enumgroup}

\begin{enumgroup}{B}
\item amet
\item etc.
\end{enumgroup}

% Uncomment to switch from MWE to MNWE

First group : \groupref{A} % Should print First group : 1 -- 4

Second group : \groupref{B} % 5 -- 6

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .