Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm writing up some lecture notes for a math course in LaTeX (using the book class so far, but I may change that), and as I do so possible homework exercises occur to me which I want to include in the notes. I haven't decided whether to put exercises at the end of each section or the end of each chapter, and in either case exercises may need to move along with associated text if I change my mind about the organization of topics.

(I don't want to intersperse the problems throughout the text, which would of course be the technically simplest solution.)

Ideally, it would be nice to be able to handle this in more or less the following way: in the .tex file, immediately after the text about topic A, I write \exercise{Problem about A}, which produces no immediate output. At some later point, I write \exerciseshere, which produces an enumerated list, with a heading like "Exercises", of all the exercises since the previous \exerciseshere. Alternatively, a command in the preamble allows me to set whether a list of exercises is produced automatically at the end of each section or the end of each chapter (keeping in mind that if there are no exercises, nothing should be done).

Added: It would also be good to be able to include a \label in an exercise, which could be used to refer to its counter in the enumerated list.

How can something like this be implemented? Or is there even already a package that does it?

P.S. I couldn't come up with a very informative title or tag for this question. Please feel free to retitle or retag.

share|improve this question
    
It turns out that we already have an exercises tag. :) –  Caramdir Mar 16 '11 at 18:36
    
@Caramdir: Thanks! I had assumed that tag was used for something else, but I see now this fits the tag exactly. –  Mark Meckes Mar 16 '11 at 18:40
    
Not an answer, but just a comment: the LMS document class does something a bit like this with addresses. They get specified in the title, but typeset at the very end of the document. So I'm hopeful for you! –  Loop Space Mar 16 '11 at 19:00

1 Answer 1

up vote 15 down vote accepted

I don't know any package that can do this out of the box, though I suspect that there are several packages that could be “abused” to give that effect. However, it is also not too hard to implement this directly.

\documentclass{article}
\usepackage{amsthm}

\theoremstyle{remark}
\newtheorem{ExInternal}{Exercise}[section]

\makeatletter
\let\@exercises\@empty%
\newcommand\exercise[2][]{%
    \g@addto@macro\@exercises{%
        \begin{ExInternal}[#1]%
            #2%
        \end{ExInternal}%
    }%
}

\newcommand\exerciseshere{%
    \subsection*{Exercises}
    \@exercises%
    \global\let\@exercises\@empty%
}
\makeatother
\begin{document}

\section{Prime Numbers}

A \emph{prime number} is a positive integer other than $1$ that is only divisible by $1$ and itself.

\exercise[Euclid's Theorem]{\label{ex:euclid}Show that there are infinitely many prime numbers.}

As you will show in Exercise \ref{ex:euclid}, there are infinitely many primes.
The number of primes that are smaller than a given natural number $n$ is denoted $\pi(n)$.
\exercise{Find an asymptotic formula for $\pi(n)$. \emph{Hint:} You might find Exercise \ref{ex:zeta} helpful.}

\exerciseshere

\section{Zeta function}

The zeta function is given by $\zeta(s) = \sum_{n=1}^\infty n^{-s}$, where $s$ is a complex number with real part bigger than $1$.
\exercise{\label{ex:zeta}Extend $\zeta$ as far as possible and find all zeros of the function.}%
For example $\zeta(2) = \frac{\pi^2}{6}$.


\exerciseshere
\end{document}

Some explanations:

  • The actual exercises are typeset using a theorem environment called ExInternal, which is defined at the top. You can of course change it to suit your needs. The advantage of theorem environments is that they take care of the numbering.

  • The command \exercise wraps an ExInternal environment around its argument and adds the result to the end of \@exercises (via the internal LaTeX macro \g@addto@macro). It also takes an optional argument that is passed as the optional argument to the theorem environment.

  • Finally, \exerciseshere prints a heading (you probably want to change that to suit your needs), then whatever is in \@exercises and afterwards empties \@exercises.

example

share|improve this answer
    
Looks great, thanks! –  Mark Meckes Mar 17 '11 at 0:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.