jbezos / enumitem

Customize enumerate, itemize and description
MIT License
48 stars 5 forks source link

Cross-refs to items in enumerate with reverse counting are wrong #27

Open golivax opened 3 years ago

golivax commented 3 years ago

Hi. The package documentation contains an interesting example that uses \EnumitemId to implement reverse counting. I implemented the proposed solution and the labels are correctly produced. However, cross-refs are not working (produce wrong values). Am I doing anything wrong? Please see the MWE below.

\documentclass{article}

\usepackage{enumitem}
\usepackage{calc,cleveref,crossreftools}

\crtrefundefinedtext{0}

\newcounter{revcount}
\newcommand\revcounter[1]{%
  \setcounter{revcount}{1+\crtcrefnumber{enum-\EnumitemId}-\value{#1}}}
\AddEnumerateCounter\revcounter\revcounter{} % the 2nd is dummy

\SetEnumitemKey{revarabic}
  {label = \revcounter*(\arabic{revcount}),
   ref   = (\arabic{revcount}),
   after = \label{enum-\EnumitemId}}

\begin{document}

A reverse counting enumerate:

    \begin{enumerate}[revarabic]
        \item \label{item4} A 
        \item \label{item3} B
        \item \label{item2} C 
        \item \label{item1} D
    \end{enumerate}

The items \ref{item4} \ref{item3} \ref{item2} \ref{item1}

\end{document}

The output I'm getting is as follows:

Reverse counting enumerate:
(4) A
(3) B
(2) C
(1) D

The items (0) (4) (3) (2)

Any idea of what's going on? Thanks!

muzimuzhi commented 3 years ago

It seems in \item \label{...} the value of counter revcount, before being updated by \setcounter{revcount}{...}, is stored in label.

An ugly workaround:

ref   = (\protect\the\numexpr1+\crtcrefnumber{enum-\EnumitemId}-\arabic*),
jbezos commented 2 years ago

Thank you @muzimuzhi, and sorry for the late answer. Although I always test all examples, for some reason this one doesn’t work. Either I didn’t do the tests well, or I’ve changed something in enumitem. As I explain in the manual, the solution is not the best (an fancy way of saying it’s actually poor), and I’ll try to find an alternative.