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}


\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:32exerciseslist 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\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