2

I want something as follow:

\begin{enumerate}
  \item[(A)] \label{hyp:a} foo 
  \item[(X)] \label{hyp:x} bar
  \item[(G)] \label{hyp:g} foo bar
\end{enumerate}

Now referencing to hypothesis \ref{hyp:a}, \ref{hyp:x} and \ref{hyp:g}.

The last line should produce:

Now referencing to (A), (X) and (G).

For now, it produces:

Now referencing to , and .


I used random letters A, X and G so that you understand that a enumerated \alph* list is not what I want.

(Also, if it has any importance, I actually want that the list with custom label inside an item of another normally enumerated list.)

  • The \item[foo] does not set \@currentlabel so there is nothing to reference there – user31729 Dec 11 '17 at 17:15
  • I think you are looking for this answer here tex.stackexchange.com/questions/365322/… – koleygr Dec 11 '17 at 17:22
  • @ChristianHupfer Perfect, that is exactly it. – Pece Dec 12 '17 at 12:08
  • @koleygr Thanks, but the answer is way more to cumbersome for what I want. I have only one list that I want to do this on, so I'll just change \@currentlabel by hand on those item instead of redefining the behaviour of \item. – Pece Dec 12 '17 at 12:10
  • Nice... Glad that you found your way... (Not deleting the comment because it may help other users)... I think there was an answer here that deleted (not sure) and was using \href... It is an option too if you do manual all the work needed – koleygr Dec 12 '17 at 12:15
2

\item[foo]\label{foo} will not place a correct label, neither for the reference itself nor the linking if hyperref is used, since no counter is involved with the \item[] version of \item.

A possible solution is define an explicit \@currentlabel, then apply \label, thereby faking the usage of some counter.

The package crossreftools provides \crtcrossreflabel for such occurences. The optional value is the label name. However, since \item[...] is used, {\crtcrossreflabel{}[]} has to be applied in order to protect the []. The correct hyper anchor is ensured with \phantomsection inside already.

I've provided a simple inline method with \optionaldesc with \phantomsection which basically the same behavior as \crtcrossreflabel,

\documentclass{article}
\usepackage{enumitem}
\usepackage{hyperref}

\usepackage{crossreftools}

\makeatletter
\newcommand{\optionaldesc}[2]{%
  \phantomsection
  #1\protected@edef\@currentlabel{#1}\label{#2}%
}
\makeatother

\begin{document}



\section{Foo} 
\clearpage
\begin{enumerate}
\item[{\crtcrossreflabel{(A)}[hyp:a]}] foo 
\item[\optionaldesc{(X)}{hyp:x}] bar
\item[{\crtcrossreflabel{(G)}[hyp:g]}] foo bar
\end{enumerate}
\clearpage
Now referencing to hypothesis \ref{hyp:a}, \ref{hyp:x}
and \ref{hyp:g}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.