# How to get a customized counter in enumerate environments and how to label and refer to the items?

In my enumerate environments, I want to have something like \myitem which automatically prints 3(a)., 3(b). etc. (and of course, the regular \items which still print the regular 1., 2., etc.)
BTW, I also would like to be able to label and refer (with clickable feature) to any kind of the two counters. In short, I want to type

‎\item‎‎ ‎foo‎‎
‎\item\label{itm1}‎‎ ‎bar‎‎
‎\myitem Something
‎\myitem\label{myitm3b} ‎Something‎‎ ‎else‎‎‎
‎\item‎ ‎foobar‎‎
Let's refer to \ref{itm1} and \ref{myitm3b} to ...


and get

     1. foo
2. ‎bar‎‎
3(a). Something‎‎
3(b). Something‎‎ ‎else‎
‎    ‎4.‎ foobar‎‎
Let's refer to 1. and 3(b). to ...


I found similar questions on this website, but none of them worked for me.

-
Would the answer to this question help you? tex.stackexchange.com/questions/60927/… –  T. Verron Nov 17 '12 at 19:26

You can define \myitem to use a custom counter for the second part of the label:

## Notes:

• Contiguous use of \myitem will result in subsequent alphabetic characters, and any use of the regular \item will reset the next use of \myitem to start at (a), as illustrated by 5(a) in the test case below.
• As per Heiko Oberdiek's suggestion I used \refstepcounter counter instead of \stepcounter so as to provide anchors for use with the hyperref package.
• Much thanks to T. Verron for providing a link to Custom enumeration for axioms or hypothesis for the missing piece I was missing which was how to set up the custom label when the \myitem was referenced via a \ref.
• I use the enumitem packagethe enumitem package for list processing, but this solution should be adaptable to not require that if so desired.
• I have used newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods.

## Code:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{enumitem}
\usepackage{hyperref}% For hyperref testing.

\newcounter{MyCounter}

\let\OldItem\item
\def\item{%
\setcounter{MyCounter}{0}%
\OldItem%
}

\makeatletter
\def\myitem{%
\OldItem[%
\stepcounter{enumi}%
}%
\refstepcounter{MyCounter}\theenumi%
(\alph{MyCounter}).]%
% Use "\protected@edef" here as per David Carlisle as it is safer
\protected@edef\@currentlabel{\theenumi(\alph{MyCounter})}%
}%
\makeatother

\begin{document}
\begin{enumerate}[align=right]
\item‎‎ ‎foo‎‎
‎\item\label{itm1}‎‎ ‎bar‎‎
‎\myitem Something
‎\myitem\label{myitm3b} ‎Something‎‎ ‎else‎‎‎
‎\item‎ ‎foobar‎‎
‎\myitem Something
‎\myitem\label{myitm5b} ‎Something‎‎ ‎else‎‎‎
\end{enumerate}
%\clearpage% For hyperref testing.
Let's refer to \ref{itm1} and \ref{myitm3b}.
\end{document}

-
For hyperref support: \refstepcounter instead of \stepcounter also generates an anchor name and sets an anchor at the location of \refstepcounter. Also \theH<counter> with unique values should be provided to get unique anchor names. –  Heiko Oberdiek Nov 17 '12 at 19:51
@HeikoOberdiek: Thanks. Have updated to use \refstepcounter. –  Peter Grill Nov 17 '12 at 20:07
I would put \refstepcounter{MyCounter} before \theenumi and using \stepcounter{enumi}. The latter avoids duplicate destination names and it cannot be references anyway, because of the next \refstepcounter. Also \theMyCounter could be redefined, making the definition of \@currentlabel obsolete. –  Heiko Oberdiek Nov 17 '12 at 20:22
@Peter: Thank you so much. This is exactly what I was looking for, but I'm currently curious to know how \newif can be replaced with the newtoggle from the etoolbox package. –  Vahid Damanafshan Nov 17 '12 at 21:11
@VahidDamanafshan: The toggle is just as boolean switch used to keep track of whether we are AlreadyInMyItem. It is set to true once we enter \myitem, and set to false once we enter a \item. Hence the sub enumeration can use the desired letters, and reset for a subsequent use. Please see Conditional typesetting / build for details of other conditionals you can use. If that does not answer your question I would suggest posting a follow up question: "How to replace a \newtoggle with a \newif"? –  Peter Grill Nov 17 '12 at 21:25

Here is the solution with \newif boolean.

\documentclass{article}
\usepackage{enumitem}
\usepackage{hyperref}
\makeatletter
\newcounter{vdcount}
\newif\ifvdam
\let\origitem\item
\def\item{%
\global\vdamfalse
\setcounter{vdcount}{0}%
\origitem
}
\def\fancyitem{%
\origitem[%
\ifvdam\else
\stepcounter{enumi}%
\global\vdamtrue
\fi
\refstepcounter{vdcount}%
\theenumi(\alph{vdcount}).%
]%
\protected@edef\@currentlabel{\theenumi(\alph{vdcount})}%
}
\makeatother

\begin{document}
\begin{enumerate}[align=right]
\item foo.
\item\label{itm1} bar.
\fancyitem Something.
\fancyitem\label{myitm3b} Something else.
\item foobar.
\fancyitem Something.
\fancyitem\label{myitm5b} Something else.
\end{enumerate}

Referring to items~\ref{itm1} and \ref{myitm3b}.
\end{document}

-