2

I want to reference the item number of a two-level enumerate environment. The code below gives the correct item numbering, but when I reference it in the text, something goes wrong - see below the code. It should say "I prefer option 2.1" of course. Thanks for any help.

\documentclass[11pt]{article}

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

\begin{document}

    \begin{enumerate}

    \item Hallo
    \item Hi

        \begin{enumerate}

        \item \label{item-one} Good By
        \item \label{item-two} Have a nice time

        \end{enumerate}
\end{enumerate}


I prefer option \ref{item-one}

\end{document}

Output of the above code

3

I recommend using the enumitem package as it makes things like this really easy. For your example, you can produce

enter image description here

using the code:

\documentclass[11pt]{article}
\usepackage{enumitem}

\begin{document}

  \begin{enumerate}

    \item hallo
    \item hi

    \begin{enumerate}[label=\arabic{enumi}.\arabic*]

      \item \label{item-one} good by
      \item \label{item-two} have a nice time

    \end{enumerate}
  \end{enumerate}

  I prefer option \ref{item-one}

\end{document}

There are many other bells and whistles provided by enumitem for customising enumerate, itemize and description environments, including defining your own variants of each of these environments. See the manual for details.

2

You're almost there. Instead of

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

do try

\renewcommand{\theenumii}{\arabic{enumii}}
\renewcommand{\labelenumii}{\theenumi.\theenumii}
\makeatletter
\renewcommand{\p@enumii}{\theenumi.} % for cross-references
\makeatletter

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.