1

I would like to redefine the enumerate environment so that each \item automatically sets \label{en:\arabic{page}\alph{enumi}}. I already tried

\usepackage{etoolbox}
\gappto{\item}{\label{en:\arabic{page}\alph{enumi}}}

but this also triggers in the center environement, where I don't want it.

How can I do this, avoiding conflict with enumitem.sty if possible.

(Don't worry about non-uniqueness of labels - the document layout assures that.)

  • 1
    Are you sure this is a good idea? What will happen to your \ref commands if you have to insert an item before the end of a list? – Ian Thompson Apr 28 '14 at 19:42
  • @IanThompson Won't happen. I'm re-typesetting an old document, so content is fixed - but style of cross-references in the new edition isn't yet. – arney Apr 28 '14 at 19:47
  • 1
    What about page breaks? – Ian Thompson Apr 28 '14 at 19:50
  • @IanThompson Trust me, in the section where I need this, there will be no dynamic typesetting at all. It's all static, drilled to look like the original - and I'm too lazy to type all the \label commands by hand, as one involuntary mishap might mess that up. – arney Apr 28 '14 at 19:55
  • Do you need to worry about nested lists (i.e. enumerates inside other enumerates)? – Ian Thompson Apr 28 '14 at 20:06
5

Use the internal \@item:

\documentclass{article}
\usepackage{xpatch}

\makeatletter
\xapptocmd{\@item}{\label{en:\arabic{page}\alph{enumi}}}{}{}
\makeatother

\begin{document}

\begin{enumerate}
\item First.
\item Second.
\item Third.
\end{enumerate}
\ref{en:1c}, \ref{en:1b} and~\ref{en:1a}

\end{document}

enter image description here

And an example with the enumitem package and nested environments:

\documentclass{article}
\usepackage{xpatch}
\usepackage{enumitem}

\makeatletter
\xapptocmd{\@item}{
  \ifnum\@enumdepth=1\relax
    \label{en:\arabic{page}\alph{enumi}}
  \else 
  \ifnum\@enumdepth=2\relax
    \label{en:\arabic{page}\arabic{enumii}}
  \else 
  \ifnum\@enumdepth=3\relax
    \label{en:\arabic{page}\roman{enumiii}}
  \else 
  \ifnum\@enumdepth=4\relax
    \label{en:\arabic{page}\Roman{enumiv}}
  \fi\fi\fi\fi
  }
  {}{}
\makeatother

\setlist[enumerate,1]{label=\alph*)}
\setlist[enumerate,2]{label=\arabic*.,ref=\arabic*}
\setlist[enumerate,3]{label=(\roman*)}
\setlist[enumerate,4]{label=\Roman*.,label=\Roman*}

\begin{document}

\begin{enumerate}
\item First.
  \begin{enumerate}
  \item First subitem.
  \item Second subitem.
  \item Third subitem.
  \item Fourth subitem.
  \end{enumerate}
\item Second.
\item Third.
\end{enumerate}
Some cross-references for the first level: \ref{en:1c}, \ref{en:1b} and~\ref{en:1a} and some cross-references for the second level: \ref{en:14}, \ref{en:13}, \ref{en:12} and~\ref{en:11}.

\end{document}

enter image description here

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.