2

In my book, the exercise section is defined as follows:

\newlist{exercise}{enumerate}{5}
\setlist[exercise]{
    label*=\thechapter.\arabic*.,
    ref=\thechapter.\arabic*, 
    before={%
        \section*{\hfil{Exercises -- \thechapter}}%
        \markright{\S Exercises -- \thechapter}%
        \addcontentsline{toc}{section}{Exercises -- \thechapter}%
        \setlist*[enumerate,1]{ref=\theexercisei.~\arabic*}%
        \setlist*[enumerate,2]{ref=\theenumi.(\alph*)}%
    },
}
\setlist*[enumerate,2]{ref=\theenumi.(\alph*)}

When cross-referencing an item from the Exercise using \cref, taking a cue from the answer in this earlier question of mine, I added the following snippet to my code:

\crefname{exercisei}{exercise}{exercises}
\crefname{exerciseii}{exercise}{exercises}
\crefname{exerciseiii}{exercise}{exercises}

The output is half-baked. It produces "exercise 1.1 in page #" properly but it fails for lower-level items. There it shows the prefix string "item" for the subitem and subsubitem. How to fix the later parts? Please help.

Below is an MWE:

\documentclass{book}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage{etoolbox}

\usepackage{varioref}
\usepackage[colorlinks=true]{hyperref}
\usepackage{cleveref}

\newlist{exercise}{enumerate}{5}
\setlist[exercise]{
    label*=\thechapter.\arabic*.,
    ref=\thechapter.\arabic*, 
    before={%
        \section*{\hfil{Exercises -- \thechapter}}%
        \markright{\S Exercises -- \thechapter}%
        \addcontentsline{toc}{section}{Exercises -- \thechapter}%
        \setlist*[enumerate,1]{ref=\theexercisei.~\arabic*}%
        \setlist*[enumerate,2]{ref=\theenumi.(\alph*)}%
    },
}
\setlist*[enumerate,2]{ref=\theenumi.(\alph*)}

\crefname{exercisei}{exercise}{exercises}
\crefname{exerciseii}{exercise}{exercises}
\crefname{exerciseiii}{exercise}{exercises}

\begin{document}
    \chapter{One}
    \begin{exercise}
        \item \label{1}
                First exercise.
        \item 
                \begin{enumerate}[label=(\alph*)]
                    \item \label{1a}
                            Question
                            \begin{enumerate}[label=(\roman*), font=\itshape, noitemsep, topsep=0pt]
                                \item \label{1ai}
                                        Qn
                            \end{enumerate}
                \end{enumerate}
    \end{exercise}
    \newpage
    \vref{1}, \vref{1a}, \vref{1ai}
\end{document}
  • Overall, I can't say I understand what you are trying to achieve. If you employenumerate (rather than exercise) environments to create the lists, don't be surprised if \cref and \vref use the label "item" in their cross-referencing call-outs. It might be a good idea if you explained in words what exactly you're trying to achieve. – Mico May 1 at 9:29
  • To answer your first query, I tried that option without the desired result. What I want to achieve is the code \vref{1a} should produce exercise 1.(a) in page # and \vref{1ai} should produce exercise 1.(a)(i) in page#. – Subhajit Paul May 1 at 9:34
  • A little typo in the last comment: \vref{1a} should produce exercise 1.2.(a) in page # and \vref{1ai} should produce exercise 1.2.(a)(i) in page # according to the MWE. – Subhajit Paul May 1 at 9:42
  • In my answer to an earlier question of yours, I wrote that it was a good idea to (a) not modify the default properties of LaTeX's basic enumerate list environments and (b) create separate, bespoke enumerate-like list environments. Your present code satisfies criterion (b) but violates criterion (a) massively. Is this deliberate? – Mico May 1 at 11:19
  • Honestly speaking, I do not have much idea about that. I researched in this site (mostly) to get my desired output and came up with the above-mentioned code. I was ok-ishly happy with it and with \ref. After you introduced me with the option of \vref I am trying to switch to the automated cross-reference mode. So it would be perfectly alright for me to accept any alteration in the code you want to make as long as the exercise produces the current output and \cref produces my desired output. – Subhajit Paul May 1 at 11:29
2

As noted in an earlier comment, I think it's a poor idea to overwrite the default properties of the basic enumerate list environment -- unless you're absolutely sure that you will never, ever use enumerate environments elsewhere in your document.

I strongly recommend that you use the machinery of the enumitem package -- specifically, its \newlist and \setlist macros -- to define 3 levels (or more, if you wish) of a custom enumerate-like list environment. In the code below, I've chosen the name exlist for this new list environment; naturally, you're free to come up with a different name -- as long as it's not enumerate... The counters associated with level-1, level-2, and level-3 exlist lists are called exlisti, exlistii, and exlistiii. To inform cleveref (and, indirectly, varioref) how such objects should be cross-referenced, issue suitable \crefname directives as well; see below.

enter image description here

enter image description here

\documentclass{book}
\usepackage{enumitem}
\newlist{exlist}{enumerate}{3}
\setlist[exlist,1]{left=0pt,
         label=\thechapter.\arabic*.,
         ref=  \thechapter.\arabic*, 
         before={%
             \section*{\centering Exercises -- \chaptername\ \thechapter}
             \markright{\S\  Exercises -- \chaptername\ \thechapter}
             \addcontentsline{toc}{section}{Exercises -- \thechapter}}
        }
\setlist[exlist,2]{left=0pt,
         label=(\alph*),
         ref=  \theexlisti.(\alph*)
        }
\setlist[exlist,3]{left=0pt,nosep,
         label=(\textit{\roman*}),
         ref=  \theexlistii(\textit{\roman*})
        }

% Load 'varioref' first, 'hyperref' second, 'cleveref' third:
\usepackage{varioref}
\usepackage[colorlinks=true]{hyperref}
\usepackage{cleveref}
\crefname{exlisti}{exercise}{exercises}
\crefname{exlistii}{exercise}{exercises}
\crefname{exlistiii}{exercise}{exercises}

\begin{document}
\setcounter{chapter}{1}

\begin{exlist} % start a level-1 list
\item \label{1} First exercise.
\item \begin{exlist} % start a level-2 list
      \item \label{12a} First part of the second exercise.
            \begin{exlist} % start a level-3 list
            \item \label{12ai}  Qn 1
            \item \label{12aii} Qn 2
            \end{exlist}
      \item \label{12b} Second part of the second exercise.
      \end{exlist}
\end{exlist}


\newpage
\vref{1}

\Vref{12a,12b}

\vref{12ai,12aii}
\end{document}
| improve this answer | |

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.