2

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

3
  • \item does increase the counter, that is true, but it does not care about the following text afterwards!
    – user31729
    Jun 24, 2016 at 15:21
  • Will all items have both a number and a description?
    – egreg
    Jun 24, 2016 at 15:44
  • yes. all items will have a number and a description
    – Martin
    Jun 24, 2016 at 15:49

1 Answer 1

2

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

6
  • amazing. thank you. Would be also possible to integrate the label command with your new command and make use of the item counter? that way you won't have to maintain the labels and label counts by hand. the \label{} for reference would then be the same as the printed version in the list.so that if there is a list item T1 caption you could reference it with \ref{T1} without manually defining \label{T1}
    – Martin
    Jun 24, 2016 at 15:43
  • @Martin: Possible, but not recommended.
    – user31729
    Jun 24, 2016 at 15:53
  • @Martin: the problem with generating a label by T1 etc. is that the item label will be reset in a new \begin{enumerate} unless you use the resume option!
    – user31729
    Jun 24, 2016 at 15:59
  • I will provide a way that takes the level of enumeration also into account!
    – user31729
    Jun 24, 2016 at 16:02
  • @Martin: see the update at the bottom please!
    – user31729
    Jun 24, 2016 at 16:11

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.