# Cross-referencing enumerate item

I need to cross-reference the item in enumerate. Here is what I have.

\documentclass[12pt]{article}
\begin{document}

\begin{enumerate}
\item Solve $x^2=1$.
\item Solve $x^2=4$.
\begin{enumerate}
\item \label{enum:1} Find all roots.
\item For each root in (\ref{enum:1}), subtract 2 from it.
\end{enumerate}
\end{enumerate}

\end{document}


It works fine but I want to have (a) instead of (2a) in the last line. In other words, I only want to have the label within the current enumerate. Thanks.

• @PhelypeOleinik: Why did you delete your answer? It gives valuable information. – GuM Aug 22 '19 at 14:03

Here is a different solution using the enumitem package. With it we can configure how references to items look. In this MWE I change the reference format of second level enumerate globally.

As an added bonus we do not have to mess with internals.

\documentclass[12pt]{article}
\usepackage{enumitem}
\setlist[enumerate,2]{
ref=(\alph*),
}
\begin{document}

\begin{enumerate}
\item Solve $x^2=1$.
\item Solve $x^2=4$.
\begin{enumerate}
\item \label{enum:1} Find all roots.
\item For each root in \ref{enum:1}, subtract 2 from it.
\end{enumerate}
\end{enumerate}

\end{document}


The key is to redefine the macro \p@enumii, which holds the prefix used in this situation:

\documentclass[12pt]{article}
\begin{document}

\begin{enumerate}
\item Solve $x^2=1$.
\item Solve $x^2=4$.
\begin{enumerate}
\makeatletter
\renewcommand*\p@enumii{}
\makeatother
\item \label{enum:1} Find all roots.
\item For each root in (\ref{enum:1}), subtract 2 from it.
\end{enumerate}
\item To show that the redefinition is local:
\begin{enumerate}
\item \label{enum:2} Find all roots.
\item For each root in (\ref{enum:2}), subtract 2 from it.
\end{enumerate}
\end{enumerate}

\end{document}

• or just use enumitem to split them apart. I never even realized that barebones enumerate behaved like that on inner lists. – daleif Aug 22 '19 at 13:52
• @daleif: Please, do post an answer with your solution, which is surely better than mine. – GuM Aug 22 '19 at 13:54
• @GuM :-):-). I have upvoted all :-( – Sebastiano Aug 22 '19 at 14:10