0

Hope the title is not too confusing and describes what I meant. Here is the thing: I need to have a list where the items have an incremental counter but also have some customized words attached. I.e., I want to have a list like:

Case 1 (a<b): XXX

Case 2 (b<c): YYY

Case 3 (c<d): ZZZ

where the '1', '2', and '3' are counters and I would like to reference them with proper hyperref. E.g, in other places, I may have something like:

In Case 1, we only need to do XXX.

where the '1' is not hardcoded and is derived from '\ref{SomeLabel}'. Furthermore, I would like the hyperref to work with the labels/refs.

Currently, I figured out a solution which displays correctly but the hyperref is not working properly. My solution is:

\documentclass{article}
\usepackage{hyperref}

\newcounter{desccounter}
\newcommand{\descitem}[1]{\refstepcounter{desccounter}\item[Case \thedesccounter{} #1]}

\begin{document}

The first list:

\setcounter{desccounter}{0}
\begin{description}

\descitem{$(a<b)$}:\label{Label1} XXX

\descitem{$(b<c)$}:\label{Label2} YYY

\descitem{$(c<d)$}:\label{Label3} ZZZ

\end{description}

Refs to the first list: Case \ref{Label1}, Case \ref{Label2}, Case \ref{Label3}.

\bigskip

The second list:

\setcounter{desccounter}{0}
\begin{description}

\descitem{$(x=y)$}:\label{Label4} AAA

\descitem{$(y=z)$}:\label{Label5} BBB

\descitem{$(z=w)$}:\label{Label6} CCC

\end{description}

Refs to the second list: Case \ref{Label4}, Case \ref{Label5}, Case \ref{Label6}.

\end{document}

And the rendered result is:

enter image description here

You can see that display is correct, but hyperref is not: If you click the hyper links for the second list, you will be directed to the first list.

Since I am not an expert in the counter/label/ref thing in Latex, any experts can help on this issue? Thanks!

1 Answer 1

1

You need to give \theHdesccounter and unique value across the document. E.g. (Item is used by hyperref in various places, you can increase it but you shouldn't reset it to zero):

\documentclass{article}
\usepackage{hyperref}

\newcounter{desccounter}
\renewcommand\theHdesccounter{\theItem.\arabic{desccounter}}
\newcommand{\descitem}[1]{\stepcounter{Item}\refstepcounter{desccounter}\item[Case \thedesccounter{} #1]}

\begin{document}

The first list:

\setcounter{desccounter}{0}
\begin{description}

\descitem{$(a<b)$}:\label{Label1} XXX

\descitem{$(b<c)$}:\label{Label2} YYY

\descitem{$(c<d)$}:\label{Label3} ZZZ

\end{description}

Refs to the first list: Case \ref{Label1}, Case \ref{Label2}, Case \ref{Label3}.

\bigskip

The second list:

\setcounter{desccounter}{0}
\begin{description}

\descitem{$(x=y)$}:\label{Label4} AAA

\descitem{$(y=z)$}:\label{Label5} BBB

\descitem{$(z=w)$}:\label{Label6} CCC

\end{description}

Refs to the second list: Case \ref{Label4}, Case \ref{Label5}, Case \ref{Label6}.

\end{document}
2
  • The solution works. But can you please explain it? Is 'Item' a counter defined by hyperref? Also, what is `theHdesccounter'? Why redefining the macro makes it work? Thanks!
    – waytofall
    Sep 13, 2021 at 18:49
  • 1
    hyperref uses internally \theHdesccounter to create the anchor name, by default this is defined as \arabic{desccounter} but this doesn't give unique names in your document. Item is a counter from hyperref that is uses also in other lists. Sep 13, 2021 at 18:53

You must log in to answer this question.

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