# Automatically label every item in lists

This is somewhat a follow-up question to "Vertical spacing in enumitem list".

Note that the question is incorporated into the example below.

\documentclass{article}

\usepackage{enumitem}

\makeatletter
\def\myitem{%
\@ifnextchar[ \@myitem{\@noitemargtrue\@myitem[\@itemlabel]}}
\def\@myitem[#1]{\item[#1]\mbox{}}
\makeatother
\setlist[enumerate,1]{
wide = 0\parindent,
listparindent = 0pt,
label = \textbf{Exercise~\arabic*}
}
\setlist[enumerate,2]{
wide = 0\parindent,
listparindent = 0pt,
label = \textbf{\alph*)}
}

\begin{document}

\noindent \underline{Example code:}
\begin{enumerate}
\myitem
\begin{enumerate}
\item Something.
\item Something.
\end{enumerate}
\myitem
\begin{enumerate}
\item Something.
\item Something.
\end{enumerate}
\myitem
\begin{enumerate}
\item Something.
\item This is exercise~\textbf{3b)}.
\end{enumerate}

\underline{Question:} Can I avoid manually labeling every single \verb|\item| and \verb|\myitem| and then -- somehow -- combine these labels to get the number of both \verb|\item| and \verb|\myitem| in a single reference?

Example: If I write \verb|This is exercise~\ref{3b}| I would like to get the output This is exercise~\textbf{3b)}'' without having to put a \verb|\label| after the \verb|\item|.
\end{enumerate}

\end{document}


• Automatic labels doesn't make sense. What if you insert a new exercise between 2 and 3? Do you want to change all \ref? And if you have 100 exercises, how will you get to the correct label name? The main point of a label is that it is independent from the number. If you know the number you don't need \ref. You can simply write This is exercise~3b). – Ulrike Fischer Jun 17 '15 at 14:18
• @UlrikeFischer What I mean is something like incorporating \label{\myitem-number\item-number-within-the-\myitem}. Then the reference should change automatically when I insert a new exercise or a new question within the exercise. (I hope it makes sense but I'm not sure it does...) P.S. I have to go for tonight (Danish time.) – Svend Tveskæg Jun 17 '15 at 14:21
• As @UlrikeFischer was trying to explain, the value of \labels is that they don't change when you add a new item. It does no good for me to use \label{equation1} if I subsequently add an equation before that one. If I know that there will never be an equation before the one I labeled equation1 then I may as well not use the label ref paradigm altogether and just write "Equation 1" in the text of the document. The label ref functionality of LaTeX allows us to define what a thing is based on its context, rather than its location and that is a strength of the system. – Paul Stiverson Jun 17 '15 at 14:43
• I really like this question, my perspective would be that the references you want to insert may be as part of the list, thereby allowing you to reference a nearby item, i.e. item (n-1) where n is your current item. Definitely above my ability to implement, but if relative positions could be navigated this would be amazingly dynamic and powerful for cross referencing. – EngBIRD Jun 17 '15 at 17:36
• @EngBIRD: As I wrote in my answer: automatic labels are easy. But they are only useful if the references are automatic too and never need user intervention. You can't use them if the \ref{item.n-1) has to change to \ref{item.n-2) when an item is inserted. – Ulrike Fischer Jun 18 '15 at 8:33

Automatic labels are easy. But they are completly useless for manual references.

Try out this document and then uncomment the item and compile again:

\documentclass{article}
\usepackage{enumitem}
\newcommand\automaticlabel{\label{\theenumi.\theenumii}}
\begin{document}
\begin{enumerate}
\item
\begin{enumerate}
%\item I forgot Yellow \automaticlabel
\item Green \automaticlabel
\item Red   \automaticlabel
\end{enumerate}
\end{enumerate}

\ref{1.a} refers to Green. \ref{1.b} refers to Red.
\end{document}