3

I am writing a list of questions using enumerate package.

Something like this :

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{enumerate}
\begin{document}

\begin{enumerate}
\item \label{itm:1} This is the question 1
\item \begin{enumerate}[(a)]
\item \label{itm:2a} This is the question 2.(a)
\item This is the question 2.(b)
\item \begin{enumerate}[-i)]
\item \label{itm:2ci}This is the question 2.(c)-i)
\item This is the question 2.(c)-ii)
\end{enumerate}
\end{enumerate}
\end{enumerate}

\ref{itm:1},\ref{itm:2a} and \ref{itm:2ci} 

\end{document}

\ref{itm:1},\ref{itm:2a} and \ref{itm:2ci} gives respectively 1, 2a and 2(c)i

how can I change the format of \ref to get

  1. , 2.(a) and 2.(c)-i)

if possible show how can I apply the solution locally and globally for all enumerate of the document.

  • related/duplicate tex.stackexchange.com/questions/163069/… – touhami Jul 17 '15 at 23:32
  • @touhami - While the two postings are obviously related to each other, by the fact that they're about the formatting of labels of and cross-references to enumerated items, the issues raised by this posting are sufficiently distinct so that it's not a duplicate. – Mico Jul 18 '15 at 1:42
5

I don't know how your objective may be accomplished with the tools of the enumerate package (though David Carlisle probably could!), but I know how to accomplish it with the enumitem package.

One of the many nice features of the enumitem package is that labels of enumerated items and the format of cross-references to these items can be set in a very straightforward way. For first-level items, only the ref part needs to be modified (by affixing a "." to \theenumi); the second- and third-level items both the label and the ref parts are modified.

(The code below uses the hyperref package to make obvious what the cross-references look like.)

enter image description here

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{enumitem}
\usepackage[colorlinks]{hyperref} %% just for this example
\begin{document}

\begin{enumerate}[ref=\theenumi.]
\item This is question 1 \label{itm:1} 
\item \begin{enumerate}[label=(\alph*),
                        ref=\theenumi(\theenumii)]
      \item This is question 2.(a) \label{itm:2a} 
      \item This is question 2.(b)
      \item \begin{enumerate}[label=-\roman*),
                              ref=\theenumii-\theenumiii)]
            \item This is question 2.(c)-i) \label{itm:2ci}
            \item This is question 2.(c)-ii)
            \end{enumerate}
      \end{enumerate}
\end{enumerate}

Questions \ref{itm:1}, \ref{itm:2a}, and \ref{itm:2ci} \dots

\end{document}

Your formatting goals, for both the labels and the cross-references, could also be achieved without the aid of any package, by using the following low-level LaTeX instructions instead. Note, though, that these instructions apply to all subsequent enumerated environments.

\makeatletter
\renewcommand\labelenumi{\arabic{enumi}.}
\renewcommand\theenumi{\arabic{enumi}.}
\renewcommand\labelenumii{(\alph{enumii})}
\renewcommand\theenumii{(\alph{enumii})}
\renewcommand\labelenumiii{-\roman{enumiii})}
\renewcommand\theenumiii{-\roman{enumiii})}
\renewcommand\p@enumiii{\theenumi\theenumii}
\makeatother

If you have several enumerated lists of this type, it is handy to create a dedicated enumerate-like environment that sets up all the formatting instructions, using the \newlist and \setlist instructions of the enumitem package. For instance, you might set up the following code (no new screenshot, as the output is the same as in the preceding example):

\documentclass[a4paper,10pt]{scrreprt}

\usepackage{enumitem}    
\newlist{qenum}{enumerate}{3}  % create a new enumerate-like environment
\setlist[qenum,1]{label=\arabic*.,
                  ref=\arabic*.}
\setlist[qenum,2]{label=(\alph*),
                  ref=\theqenumi(\alph*)}
\setlist[qenum,3]{label=-\roman*),
                  ref=\theqenumii-\roman*)}

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

\begin{document}

\begin{qenum}
\item This is question 1 \label{itm:1} 
\item \begin{qenum}
      \item This is question 2.(a) \label{itm:2a} 
      \item This is question 2.(b)
      \item \begin{qenum}
            \item This is question 2.(c)-i) \label{itm:2ci}
            \item This is question 2.(c)-ii)
            \end{qenum}
      \end{qenum}
\end{qenum}

Questions \ref{itm:1}, \ref{itm:2a}, and \ref{itm:2ci} \dots

\end{document}
| improve this answer | |
  • Thank you for your detailed answer. I will wait a little bit for Carlisle before accepting this answer. I have an additional question concerning enumitem package : how can I redefine the default format of label and ref ? – Aymane Fihadi Jul 18 '15 at 9:39
  • 1
    @AymaneFihadi - When working with enumitem, there are basically few defaults: The package makes it very straightforward to change just about anything, but the downside (if it's even a downside) is that there are very few "default" values: You can (and must) specify the settings for \label and \ref. Does this answer your follow-up query? – Mico Jul 18 '15 at 9:45
  • Just for clarification, do you mean I have to specify the values of ref and label in every list of question in the same document ? – Aymane Fihadi Jul 18 '15 at 9:52
  • @AymaneFihadi - Thanks for the clarification. I'll post an addendum that shows how to set up a custom enumeration environment. – Mico Jul 18 '15 at 9:58
  • Perfect! Thank you very much for all your help. – Aymane Fihadi Jul 18 '15 at 11:19

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.