1

I redefined \theenumii, which is fairly simple, but when I reference it in the text, the result of \ref{enumi21} below is 22.1, instead of 2.1.

If I redefine \renewcommand{\theenumii}{\theenumi.\arabic{enumii}} as \renewcommand{\theenumii}{.\arabic{enumii}}, \ref{enumi21} outputs correcly as 2.1, but then of course the label is messy.

If, on the other hand, this solves the problem:

\renewcommand{\labelenumii}{\arabic{enumi}.\arabic{enumii}}
\renewcommand{\theenumii}{.\arabic{enumii}}

The text is OK, but \theenumii returns .1 in the text. Of course I'll never need to use \theenumii, but I'd just like to understand the logic behind this.

\documentclass{article}


\renewcommand{\labelenumii}{\theenumi.\arabic{enumii}}
\renewcommand{\theenumii}{\theenumi.\arabic{enumii}}

\begin{document}
\begin{enumerate}
\item \label{enumi1}
\item Number 2
\begin{enumerate}
    \item Number 2.1 \label{enumi21}
\end{enumerate}
\end{enumerate}

\ref{enumi1}, \ref{enumi21}.

\theenumii \verb|\theenumii|

\theenumi \verb|\theenumi|
\end{document}
3
  • Did you try loading enumitem and adding to your preamble: \setlist[enumerate,2]{label=\theenumi.\arabic*}?
    – Bernard
    Nov 17, 2016 at 13:31
  • @Bernard, It also works, but there's the same caveat: try \theenumii and the value returned will be a.
    – user9424
    Nov 17, 2016 at 13:34
  • Without your modifications (only my suggestion)?
    – Bernard
    Nov 17, 2016 at 13:41

2 Answers 2

1

This seems to work:

\documentclass{article}

\usepackage{enumitem}
\setlist[enumerate,2]{label=\theenumi.\arabic*}
\usepackage{chngcntr}
\counterwithin{enumii}{enumi}

\begin{document}

\begin{enumerate}
\item \label{enumi1}
\item Number 2
\begin{enumerate}
    \item Number 2.1 \label{enumi21}
\end{enumerate}
\end{enumerate}
See \ref{enumi1}, \ref{enumi21}.

\theenumii \verb|\theenumii|

\theenumi \verb|\theenumi|

\end{document} 

enter image description here

Alternatively, if you don't want to load chngcntr, replace \counterwithin{…} with this line:

\renewcommand{\theenumii}{\theenumi.\arabic{enumii}}
2
  • Bernard, it makes sense to add \counterwithin{enumii}{enumi}, but isn't this already defined in the main classes? It seems that setting \labelenumii and \theenumii disrupts something inside the usual \ref argument, that's what I wanted to know.
    – user9424
    Nov 17, 2016 at 20:04
  • What is defined in the standard classes is resetting enumii when enumi is increased. \counterwithin, additionally, prepends the value of enumi to the value of enumii and add a dot in between.
    – Bernard
    Nov 17, 2016 at 20:46
0

If you ask for the meaning of \labelenumii and \theenumii, you get

\labelenumii:
\long macro:->(\theenumii )

\theenumii:
\long macro:->\@alph \c@enumii 

(I used texdef). If you ask for \p@enumii, you get

\p@enumii:
\long macro:->\theenumi 

and this is the key to your problem. The macro \p@enumii is prepended to the value given by \theenumii when the label is being processed.

\documentclass{article}

\renewcommand{\labelenumii}{\theenumi.\arabic{enumii}}
\renewcommand{\theenumii}{\arabic{enumii}}
\makeatletter
\renewcommand{\p@enumii}{\theenumi.}
\makeatother

\begin{document}

\begin{enumerate}
\item \label{enumi1}
\item Number 2
\begin{enumerate}
    \item Number 2.1 \label{enumi21}
\end{enumerate}
\end{enumerate}

\verb|\ref{enumi1}| $\to$ \ref{enumi1}.

\verb|\ref{enumi21}| $\to$ \ref{enumi21}.

\end{document}

enter image description here

You must log in to answer this question.