3

This must be easy, but my TeX knowledge is insufficient.

How can I make \label{key} in an enumerate environment reference the item's enumi instead of the item's \theenumi (which seems to be the default).

Problem: I have used \renewcommand\theenumi{\footnotesize{\emph{Element~\arabic{enumi}}}}, and therefore \ref{key} (eg. in the third item) returns "Element 3" and not simply "3"---which is what I am looking for.

Will be grateful for any suggestion.

  • 2
    while using enumitem package as in the answer below is probably easier the problem is that your definition is incorrect. you should have left \theenumi unchanged and redefined \labelenumi which is what is used for the item label. – David Carlisle Jan 10 '16 at 15:48
  • I think he changed the value of \theenumi because he wanted to change the way the items were numbered, and \labelenumi by default expands to \theenumi, so he got both. But you have a good point. – Hood Chatham Jan 10 '16 at 18:26
5

This is actually a breeze with enumitem as it allows you to specify separate label and reference keys for your lists:

enter image description here

\documentclass{article}
\usepackage{enumitem}
\begin{document}

\begin{enumerate}[label={\footnotesize\emph{Element~\arabic*}},ref=\arabic*]
  \item First item\label{item1}
  \item[(b)] Second item
  \item Third item\label{item3}
\end{enumerate}

This should be one: \ref{item1}

This should be two: \ref{item3}

\end{document}

A special *-form is used to identify the actual counter formatting.

1

By doing some \show's, you can see that \label uses the command \@currentlabel to determine what the reference should be. So using etoolbox, we can say:

\newif\if@mynoitemarg
\@mynoitemargfalse
\def\item{\@inmatherr\item\@ifnextchar[%
    \@item
    {\@noitemargtrue\@mynoitemargtrue\@item[\@itemlabel]}%
}%]
\apptocmd\@item{
    \xdef\@currentlabel{\if@mynoitemarg\arabic{enumi}\else #1\fi}
    \if@mynoitemarg\@mynoitemargfalse\fi\ignorespaces
}

and it will behave in the way you want.

Here's a complete example:

\documentclass{article}
\usepackage{enumerate}
\usepackage{etoolbox}
\makeatletter
\newif\if@mynoitemarg
\@mynoitemargfalse
\def\item{\@inmatherr\item\@ifnextchar[%
    \@item
    {\@noitemargtrue\@mynoitemargtrue\@item[\@itemlabel]}%
}%]
\apptocmd\@item{
    \xdef\@currentlabel{\if@mynoitemarg\arabic{enumi}\else #1\fi}
    \if@mynoitemarg\@mynoitemargfalse\fi\ignorespaces
}
\makeatother
\begin{enumerate}[(a)]
\item this \label{a}
\item[hi]  \label{hi}
\item again  \label{b}
\item[there] \label{there}

\end{enumerate}

This should be one: \ref{a}

This should be two: \ref{b}

This should be hi: \ref{hi}

This should be there: \ref{there}
\end{document}

Edit: Werner pointed out that my previous solution didn't work with optional arguments to \item, so I fixed that and defined the label text in that situation to be the optional argument provided.

  • 1
    Your approach would fail if the user specifies an optional argument for \item. – Werner Jan 10 '16 at 7:55

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.