[Tex/LaTex] enumerate with description and label for reference

#enumeratecross-referencingdescriptionlists

I want to create an enumerated list with description of items and a label assigned so that each item can individually be referenced by \ref or \nameref

It is obvious how to do this manually but I would like to save myself a bit of work and create some commands to have latex do the job

This is the LaTeX code I want to atomise

\begin{enumerate}[label=\textbf{T\,\arabic*},leftmargin=1cm]
\item \textit{caption} \label{itm:1}
\end{enumerate}

now I would like to reference the label like this

\ref{itm:1}
>> T1
\nameref{itm:1}
>> (T1) caption

referencing subsequent items accordingly by increasing the counter

any idea / help is highly appreciated

Best Answer

This uses a wrapper command \myitem to grab the item text and stores it into \@currentlabelname, which is needed by \nameref to use the 'name' properly!

\documentclass{article}

\usepackage{enumitem}

\usepackage{etoolbox}

\usepackage{hyperref}


\makeatletter
\newcommand{\myitem}[2][]{%
  \ifblank{#1}{%
  \item #2%
  }{%
  \item[#1] #2%
  }%
  \protected@edef\@currentlabelname{(\theenumi) #2}
}
\makeatother


\begin{document}
\begin{enumerate}[label=\textbf{T\,\arabic*},leftmargin=1cm]
\myitem{\textit{caption}} \label{itm:1}
\myitem{Some other important text} \label{itm:2}
\end{enumerate}

Now, we use \nameref{itm:1} and \ref{itm:1}

And \nameref{itm:2}
\end{document}

enter image description here

Update -- With automatic labelling, be careful on reset on the enumerate list!

The Ti1 is the first label of the first level of an enumerate, the Tii1, Tiii1 and Tiv1 will be the relevant labels of deeper levels.

\documentclass{article}

\usepackage{enumitem}

\usepackage{etoolbox}

\usepackage{hyperref}




\makeatletter
\newcommand{\myitem}[2][]{%
  \ifblank{#1}{%
  \item #2%
  }{%
  \item[#1] #2%
  }%
  \protected@edef\@currentlabelname{(\csname the\@enumctr\endcsname) #2}
  \label{T\romannumeral\@enumdepth\arabic{\@enumctr}}%
}
\makeatother


\begin{document}
\begin{enumerate}[label=\textbf{T\,\arabic*},leftmargin=1cm]
\myitem{\textit{caption}}
\myitem{Some other important text}
\begin{enumerate}[label=\textbf{Foooo\arabic*}]
  \myitem{\textbf{Subitem}}
\end{enumerate}
\end{enumerate}

\clearpage
Now, we use \nameref{Ti1} and \ref{Ti1}

And \nameref{Ti2} or \nameref{Tii1}

\end{document}

enter image description here

Related Question