Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

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

2 Answers 2

up vote 7 down vote accepted

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

enter image description here

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}
\newtoggle{AlreadyInMyItem}

\let\OldItem\item
\def\item{%
    \global\togglefalse{AlreadyInMyItem}%
    \setcounter{MyCounter}{0}%
    \OldItem%
}

\makeatletter
\def\myitem{%
    \OldItem[%
        \iftoggle{AlreadyInMyItem}{}{%
            \stepcounter{enumi}%
            \global\toggletrue{AlreadyInMyItem}%
        }%
        \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}
share|improve this answer
1  
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} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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