# \ref and \nameref with alpha enumerated items

Is there a way to keep the enumeration list with (a), (b), etc., and have references provide the complete context like 1.1.1(a)?

The section and sub-section references display as I’d like with 1.1 and 1.1.1 and the name-references works as well, “First” and “And..”. The enumerated item reference shows as 28 but I'd like 1.1.1(ab). And the enumerated item name-reference is wrong (“Also..”), though perhaps that’s intentional. So, the enumerated alpha item reference has two issues: it doesn’t include it’s context; and, it doesn’t honor the enumeration alpha display characteristics. For my use, a custom command for the references is fine but the command hopefully won't require both the subsection and the item labels, e.g., instead of writing the verbose and error-prone:

\myuglyitemref{subsec:first-and}{itm:faab}

is there some way to define a reference so I can write:

\myawesomeitemref{itm:faab}

which displays 1.1.1(ab)?

\documentclass{report}
\usepackage{alphalph}
\usepackage{enumitem}
\usepackage{nameref}
\makeatletter
\def\enumalphalphcnt#1{\expandafter\@enumalphalphcnt\csname c@#1\endcsname}
\def\@enumalphalphcnt#1{\alphalph{#1}}
\makeatother
\renewcommand{\labelenumi}{(\enumalphalphcnt{enumi})}
% my (ugly) item reference
\newcommand{\myuglyitemref}[2]{\ref{#1}\labelenumi}
\begin{document}
\chapter{SOMETHING}The chapter.
\section{First}\label{sec:first}
Section items show with (ab) but referenced with 1.1(ab)
\begin{enumerate}\setcounter{enumi}{27}
\item {First 28} \label{itm:fab} 28th element
\end{enumerate}
\subsection{And...}\label{subsec:first-and}
Subsection items show with (a) but referenced with 1.1.1(a)
\begin{enumerate}\setcounter{enumi}{27}
\item {And 28} \label{itm:faab} 28th element
\end{enumerate}
Correct: \ref{sec:first} and \ref{subsec:first-and}
Correct: \nameref{sec:first}'' and \nameref{subsec:first-and}''
Incorrect: \ref{itm:fab} and \ref{itm:faab} - should be 1.1(ab) and 1.1.1(ab)
Incorrect: \nameref{itm:faab}''
Ugly: \myuglyitemref{subsec:first-and}{itm:faab}

• You can put almost anything into a \ref (like pgfplot legends). See tex.stackexchange.com/questions/236626/… for example. – John Kormylo Jul 22 '15 at 1:36
• enumitem lets you specify the label and cross-reference separately for all ordered lists, custom ordered lists, this ordered list, this series of ordered lists, whatever. fancyref and/or cleverref might also be of interest. – cfr Jul 22 '15 at 1:47
• Why are you loading enumitem and not using it to make life easier?! – cfr Jul 22 '15 at 1:48
• I'm a big fan of easy, @cfr, especially if it solves my problem. ;-) I certainly meant to use the enumitem package in the preamble but I'm a beginner so perhaps I misused it -- how should the preamble be modified enable a document-wide change to enumerate to use alpha? – Jan Nielsen Jul 22 '15 at 2:04
• The \ref is very flexible, @JohnKormylo -- but can I, somehow, derive the environment within \ref so I could write something like \myref{itm:faab}? – Jan Nielsen Jul 22 '15 at 2:37

My answer addresses only the cross-referencing of enumerated items, where the appearance of the cross-referenced "numbers" depends on whether the items occur at the section level or the subsection level. (Aside: This site "works" best if postings contain only one major question at a time. That's why I'm not even trying to address the other major question you've raised, viz., how to use \nameref on the "titles" of enumerated items.)

The solution works by modifying the low-level LaTeX macros \theenumi, \labelenumi, and \p@enumi. Observe the use of an \ifnum conditional in the statement that modifies the \p@enumi macro.

In the example below, the hyperref package is loaded merley to highlight which parts of the typeset text are the actual cross-references generated by \ref instructions.

\documentclass{report}
\usepackage{alphalph}

\renewcommand\theenumi{(\alphalph{\value{enumi}})}
\renewcommand\labelenumi{\theenumi}
\makeatletter
\renewcommand\p@enumi{\ifnum\value{subsection}=0{\thesection}\else{\thesubsection}\fi}
\makeatother

\usepackage[colorlinks]{hyperref} %% just for this example

\begin{document}
\chapter{SOMETHING}
The chapter.

\section{First section}\label{sec:first}

Section-level enumerated items: Labels should show as (ab)'' and should be cross-referenced as 1.1(ab)''.

\begin{enumerate}
\setcounter{enumi}{27}
\item 28th element \label{itm:fab}
\end{enumerate}

\subsection{First subsection}\label{subsec:first-and}
Section-level enumerated items: Labels should show as (ba)'' and should be cross-referenced as 1.1.1(ba)''.

\begin{enumerate}
\setcounter{enumi}{52}
\item 53rd element \label{itm:faab}
\end{enumerate}

\subsection{Second subsection}

Correct: Section \ref{sec:first} and subsection \ref{subsec:first-and} \dots

\medskip\noindent
Now also correct: Items \ref{itm:fab} and \ref{itm:faab} \dots

\end{document}

• @JanNielsen - You're welcome! :-) Incidentally, while the nameref package extracts sectioning names for cross-referencing via \nameref, I do not believe its functionality applies to other objects such as items of an enumerated list. – Mico Jul 22 '15 at 13:36

This is a partial solution.

Some housekeeping:

Replace

\renewcommand{\labelenumi}{(\enumalphalphcnt{enumi})}


with

\setlist[enumerate,1]{label=(\enumalphalphcnt*)}


or

\setlist[enumerate,1]{label=(\enumalphalphcnt*), ref={\thesubsection(\enumalphalphcnt*)}}


Note that you can also say

\begin{enumerate}[start=27]


and

\begin{enumerate}[resume]


But this is not yet right because you do not want 0s when the reference is not within a section or subsection...