# A simple section exercise section

What I want seems pretty simple, but I was not able to find a satisfactory answer.

What I want is something like this

\section{Foo}
A bunch of definitions, theorems, remarks, etc.
\begin{exercise}
\ex\label{ex:foo:t1} Show that this is that.
\begin{ans}
This is that by casual observation.
\end{ans}
\ex Show that is this
\begin{ans}
Just reverse the answer to exercise \ref{ex:foo:t1}.
\end{ans}
\end{exercise}


The output should be something like:

1 Foo

A bunch of definitions, theorems, remarks, etc.

Exercises

1. Show that this is that.

Ans: This is that by casual observation.

2. Show that is this

Ans: Just reverse the answer to exercise 1.

I also need some flexibility in numbering. For example, with a few changes to the preamble, the output could be:

1 Foo

A bunch of definitions, theorems, remarks, etc.

Exercises For Section 1

1.1 Show that this is that.

Ans: This is that by casual observation.

1.2 Show that is this

Ans: Just reverse the answer to exercise 1.1.

• Packages exsheets, answers, probsoln etc. can do this, I think – user31729 Nov 17 '15 at 19:30
• Thank you for your comment. Do you mind putting it in form of an answer with a concrete example of how to achieve this using those packages? – mh patter Nov 17 '15 at 19:38
• Perhaps later on, but I think other users will be faster than I and most likely provide a better solution then because they do know more than I do about those packages – user31729 Nov 17 '15 at 19:42
• Most of the complexity in itemize and enumerate comes from embedding layers. A single layer enumerated environment is pretty trivia to set up. All you need is a counter and hanging indentation. see tex.stackexchange.com/questions/134408/… – John Kormylo Nov 17 '15 at 21:18
• Please always post a complete example. As it stands, it is almost impossible to determine how best to implement the desired formatting of \section{Foo} because it depends on your class and on any packages you are using to customise the format of sectional headings. – cfr Nov 17 '15 at 23:35

Here's an implementation using the enumitem to do some of the lifting with formating:

In the code below, we have:

\newlist{exercise}{enumerate}{5}
\setlist[exercise]{label*=\arabic*.,ref=\arabic*,before={\subsection*{Exercises}}}


This defines a list-like environment called exercise.

The next bit,

% copy \item into \ex
\let\ex\item


defines \ex as \item.

Finally, we define an ans environment:

% a simple ans environment
\newenvironment{ans}{\par Ans:}{}


Here's the complete code:

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}

% use the enumitem package to ease the list-like
% definition of the exercise environment
\usepackage{enumitem}
\newlist{exercise}{enumerate}{5}
\setlist[exercise]{label*=\arabic*.,ref=\arabic*,before={\subsection*{Exercises}}}

% copy \item into \ex
\let\ex\item

% a simple ans environment
\newenvironment{ans}{\par Ans:}{}

\begin{document}
\section{Foo}
A bunch of definitions, theorems, remarks, etc.
\begin{exercise}
\ex\label{ex:foo:t1} Show that this is that.
\begin{ans}
This is that by casual observation.
\end{ans}
\ex Show that is this
\begin{ans}
Just reverse the answer to exercise \ref{ex:foo:t1}.
\end{ans}
\end{exercise}
\end{document}


Regarding your requirements for numbering flexibility, if you write:

\setlist[exercise]{label*=\thesection.\arabic*.,ref=\thesection.\arabic*,before={\subsection*{Exercises}}}


then you obtain:

If you reach a point in the future when you need to separate the answers from the questions, then you might like to look at the answers package; here's a link to questions tagged with related questions: https://tex.stackexchange.com/questions/tagged/answers.