dimanche 30 novembre 2014

Setting the enumi counter under custom enumeration with enumitem


I have a several items in an 'enumitem' enumerate environment with custom label styles, and I would like to repeat them at various points later on.


Something like this:



(a) Unicorns don't exist.


(b) Trees exist.


As I have said before:


(a) Unicorns don't exist.



However, I ran into trouble because \ref{somelabel} (where somelabel is the label of an item in the enumerate environment) gives the stylised counter rather than the actual number.


So I tried using the 'refcount' package. But that doesn't seem to work either, and \getrefnumber{somelabel} gives the same result as \ref{somelabel}.


Here is a simplified version of my markup:



\documentclass{article}

\usepackage{enumitem}
\usepackage{refcount}

\begin{document}

\begin{enumerate}[label=(\alph*)]
\item Thing 1\label{first}
\item Thing 2
\setcounter{enumi}{\ref{first}}
\item Thing 3
\setcounter{enumi}{\getrefnumber{first}}
\item Thing 4
\end{enumerate}

\begin{enumerate}[resume*]
\setcounter{enumi}{\ref{first}}
\item Thing 5
\setcounter{enumi}{\getrefnumber{first}}
\item Thing 6
\end{enumerate}

Reference: \ref{first}. Number: \getrefnumber{first}.

\end{document}


I get ! Missing number, treated as zero. <to be read again> errors for each \setcounter (but only on the second run through).


The result looks like:


Result of the code


So despite latex's protestations, it does seem to be setting the counter right in the first enumerate (though in fact it should be (b), since I haven't decremented the counter by one; and it also prints out extra (a)'s).


However, in the second enumerate, it fails to even form a valid list (I get ! LaTeX Error: Something's wrong--perhaps a missing \item. errors too).


Note: I remove [label=(\alph*)] and [resume*], this all works perfectly.


I know this could be achieved by defining a counter for each item, or even hard-coding the values, but I'd rather do this with labels and references if possible.


Thank you for your help!





Aucun commentaire:

Enregistrer un commentaire