4

For a book project, I want to define exercises in each chapter, having printed labels like Exercise 1.1 and TeX labels like lab:1.1 that can be referenced as "see Exercise \ref{lab:1.1}". [I've read the discussion of some similar questions, but haven't been able to adapt them to my needs.]

A lot of this has already been written using ordinary, nested enumerate lists. I'd like to use the following sort of markup to make conversion easier:

\begin{Exercises}
  \exercise Here's the first exercise: Make it longer to see line wrap. Now the
   question: How many angels can dance on the head of a pin?
   \begin{enumerate}
      \item Why are they laughing?
      \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
   \end{enumerate}
\end{Exercises}

to produce output like this:

Exercise 1.1: Here's the first exercise: Make it longer to see line wrap. Now the
    question: How many angels can dance on the head of a pin?

    (a) Why are they laughing?
    (b) In a random sample of $n=30$ pins, how many angels do you expect, on average?

I tried to do this using the enumitem package as shown in the example test file below, and I've gotten the labels and references to work, but the parts of an exercise are labeled 1., 2., etc., and the formatting I get looks like this:

Exercise 1.1: Here's the first exercise: Make it longer to see line wrap. Now the
              question: How many angels can dance on the head of a pin?

              1. Why are they laughing?
              2. In a random sample of $n=30$ pins, how many angels do you expect, on average?

I can get the parts labeled (a), (b), ... by explicitly using \begin{enumerate}[label=(\alph*)] for the parts of each exercise, but I'd prefer to have that happen automatically.

Here is my current test file:

\documentclass{report}
\usepackage{enumitem}
\usepackage{lipsum}

% Test a list style for exercises in chapters, allowing references.

\newlist{exercises}{enumerate}{2}
\setlist[exercises]{label=\textbf{Exercise \thechapter.\arabic*:},leftmargin=*}
% why doesn't this work for exercise parts?
\setenumerate[2]{label=(\alph*)}

\newcounter{exercise}[chapter]
\renewcommand{\theexercise}{\thechapter.\arabic{exercise}}
\newenvironment{\Exercises}{%
  \begin{exercises}[leftmargin=*]
  }%
  {%
  \end{exercises}
  }

\newcommand{\exercise}{%
  \refstepcounter{exercise}%
  \label{lab:\theexercise}%
  \item%
    }
\newcommand{\labref}[1]{Exercise~\ref{#1}}

\begin{document}

\chapter{One}\label{ch:one}
This is chapter \ref{ch:one}.
\lipsum[2]

\begin{\Exercises}

\exercise Here's the first exercise: Make it longer to see line wrap. Now the question: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?
    \begin{enumerate}[label=(\alph*)]
        \item Discuss: Can a woodchuck really chuck wood?
        \item If a woodchuck \emph{can} chuck 3 cubits per hour, how many hours would it take
        to chuck a mega-cubit forest?
    \end{enumerate}

\exercise Why can't I make the parts of exercises labeled as (a), (b), ... without setting [label=] manually?

\end{\Exercises}

We can reference Exercise \ref{lab:1.1}.
What about Exercise \ref{lab:1.2}?
And also using the shorthand, \labref{lab:1.2}?

\chapter{Two}\label{ch:two}
This is chapter \ref{ch:two}. That's all there is.

\begin{\Exercises}

\exercise Here's the first exercise: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?  Refer to \labref{lab:1.2} for details.
\end{\Exercises}

Can we reference Exercise \ref{lab:2.1}?
Why not \labref{lab:2.2}?

\end{document}
3

Preamble

You have got an error in you current document: You define an environment, which name is an command (\newenvironment{\Exercises}).

I have two ideas how to realize this, which are basically what you have done. But somehow, you mix them up, I think.

First idea: Manual counter

You create a manual counter for the exercises an selfmade label for them, which also creates the label. This is basically what you have done. For the labeling ((a), (b), ...), I just use \setlist[enumerate]{label=(\alph*)}, which works fine for me.

\documentclass{report}
\usepackage{enumitem}
\usepackage{lipsum}

\newcounter{exercisecounter}[chapter]
\renewcommand{\theexercisecounter}{\thechapter.\arabic{exercisecounter}}

\newcommand{\exercise}{%
  \medskip
  \refstepcounter{exercisecounter}%
  \noindent \textbf{Exercise~\theexercisecounter:}~
  \label{lab:\theexercisecounter}%
}

\newcommand{\labref}[1]{Exercise~\ref{#1}}

\setlist[enumerate]{label=(\alph*)}


\begin{document}

\chapter{One}\label{ch:one}
This is chapter \ref{ch:one}.
\lipsum[2]

\exercise Here's the first exercise: Make it longer to see line wrap. Now the question: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?
    \begin{enumerate}
        \item Discuss: Can a woodchuck really chuck wood?
        \item If a woodchuck \emph{can} chuck 3 cubits per hour, how many hours would it take
        to chuck a mega-cubit forest?
    \end{enumerate}

\exercise Why can't I make the parts of exercises labeled as (a), (b), ... without setting [label=] manually?

We can reference Exercise \ref{lab:1.1}.
What about Exercise \ref{lab:1.2}?
And also using the shorthand, \labref{lab:1.2}?

\chapter{Two}\label{ch:two}
This is chapter \ref{ch:two}. That's all there is.

\exercise Here's the first exercise: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?  Refer to \labref{lab:1.2} for details.

Can we reference Exercise \ref{lab:2.1}?
Why not \labref{lab:2.2}?

\end{document}

Here, you don't need a second enumeration for the exercise labels and the environment you wanted to define with \newenvironment{\Exercises}.

Exercises - First idea

Second idea: Nested enumerations (Recommended and updated)

After some playing around with enumitem I could create the following solution of your problem. It only uses enumitem and you don't have to deal with a self created counter. It works fine for me and I would recommend this alternative, as it is more elegant, I think. I added some comments, so I think you should understand what I have done.

Remark: The configuration of the horizontal lengths/margins/indention in enumitem isn't that easy to understand (at least for me it wasn't :o)) and I refer to Multiline item indent and the enumitem documentation for more details.

\documentclass{report}
\usepackage{enumitem}
\usepackage{lipsum}


% New list for exercises
\newlist{exercises}{enumerate}{2}

\setlist[exercises]{%
  label=\textbf{Exercise \thechapter.\arabic*}~,  % Label: Exercise C.E
  ref=\thechapter.\arabic*, % References: C.E (important!)
  align=left,               % Left align labels
  labelindent=0pt,          % No space betw. margin of list and label
  leftmargin=0pt,           % No space betw. margin of list and following lines
  itemindent=!,             % Indention of item computet automatically
}

\setlist[enumerate, 1]{label=(\alph*)}            % Label for subexercises

\newcommand{\exercise}{%
  \item \label{lab:\arabic{chapter}.\arabic{exercisesi}}  % Append label to item
}


\newcommand{\labref}[1]{Exercise~\ref{#1}}

\begin{document}

\chapter{One}\label{ch:one}
This is chapter \ref{ch:one}.
\lipsum[2]

\begin{exercises}

    \exercise Here's the first exercise: Make it longer to see line wrap. Now the question: How many angels can dance on the head of a pin?
        \begin{enumerate}
            \item Why are they laughing?
            \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
        \end{enumerate}

    \exercise How many chucks can a woodchuck chuck?
        \begin{enumerate}
            \item Discuss: Can a woodchuck really chuck wood?
            \item If a woodchuck \emph{can} chuck 3 cubits per hour, how many hours would it take
            to chuck a mega-cubit forest?
        \end{enumerate}

    \exercise Why can't I make the parts of exercises labeled as (a), (b), ... without setting [label=] manually?

\end{exercises}

We can reference Exercise \ref{lab:1.1}.
What about Exercise \ref{lab:1.2}?
And also using the shorthand, \labref{lab:1.2}?

\chapter{Two}\label{ch:two}
This is chapter \ref{ch:two}. That's all there is.

\begin{exercises}

    \exercise Here's the first exercise: How many angels can dance on the head of a pin?
        \begin{enumerate}
            \item Why are they laughing?
            \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
        \end{enumerate}

    \exercise How many chucks can a woodchuck chuck?  Refer to \labref{lab:1.2} for details.

\end{exercises}

Can we reference Exercise \ref{lab:2.1}?
Why not \labref{lab:2.2}?

\end{document}

Exercises - Second idea

  • Thank you, dawu, for these helpful solutions. In my book, I prefer the formatting of the first (manual counter) solution, but I agree that the 2nd is more elegant. I would like to acknowledge your help with this, so how can I refer to you? – user101089 Aug 13 '14 at 20:06
  • You're welcome! Can you explain me, why you prefer the first solution in detail? What formatting do you mean in detail? The indetion of the second (and so on) lines? It would be no problem to realize this at the second solution and work with it. For all other things, you can contact me (take a look at my Github page for my e-mail address. Could you please accept my answer above? Thanks! – dawu Aug 13 '14 at 20:16
  • OK: I want to have the text following \exercise formatted so that second and all following lines have no indent. That's because in my book I often have several paragraphs or code blocks following the main question, or really the question preamble, that I want to have displayed directly under, with the same left margin. Then the exercise parts, using \begin{enumerate} ... \end{enumerate} would appear closer to the left margin, as shown in your first (manual counter) solution. Thanks again; I'll find you on Github. – user101089 Aug 13 '14 at 21:32
  • I changed the configuration of the exercises list and now there is no more indention of the following lines, so it looks like the first solution. Is this how you want it? I also mentioned, that I forgot one important option before (ref=\thechapter.\arabic*). Please also note my remark. – dawu Aug 13 '14 at 23:48
  • Yes, that does it nicely! Another improvement I made for my use: added itemsep to \setlist[enumerate, 1]{label=(\alph*),itemsep=0pt}. I had actually been using \begin{enumerate*} ... \end{enumerate*} with the mdwlist package before to reduce inter-item spacing for the question parts. – user101089 Aug 14 '14 at 13:31

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.