# Matching enumerated exercises with their answer when using the answers package

I am using the answers package to write a number of exercise sheets. Some of the questions I write contain an enumerated list (say from part (a) to (h) and beyond). The problem I am having is in the placement of answers that accompanies each enumerated item. When the enumerated list is only short I can easily just hand code in the answers as follows:

\begin{sol}
(a) Here (b) Next (c) The one after next
\end{sol}


However, for long enumerated lists I often go back and add new questions, delete others, or just change their order, and as the answers have only been "hand coded", I must now go back and try to track all changes to the order of the answers and is a real pain for very long lists.

Is there any way the answer to each question in an enumerated list using the answers package can be "tied" together at source but when printed will still appear under the same exercise (Say Exercise 2)?

As an example, in my MWE is it possible to not have to hand code the answers as I have done from (a) to (h) but instead place each answer with the question at source and still get all answers within the one exercise printed under the same exercise heading (in my case, Exercise 2)?

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{ntheorem}
\theorembodyfont{\upshape}
\theoremseparator{.}

\Newassociation{sol}{Solution}{ans}
\renewcommand{\Solutionlabel}[1]{\small{#1.}}
\newtheorem{ex}{}[section]
\renewcommand{\theex}{\arabic{ex}}
\newenvironment{Ex}[1]{\begin{trivlist}\item \textsc{#1} %
\renewcommand{\Currentlabel}{#1}}{\end{trivlist}}

\usepackage{multicol}
\newenvironment{mtmulticols}[1]{%
\Writetofile{ans}{\string\begin{multicols}{#1}}
\begin{multicols}{#1}}{\end{multicols}%
\Writetofile{ans}{\string\end{multicols}}}

\begin{document}
\chapter{Polynomials}
\section{Partial fractions}
\label{sec:partial-fractions}

\Opensolutionfile{ans}[partial-fractions]
\section*{Exercises for Section \ref{sec:partial-fractions}}
\vspace{-0.6cm}
\hrulefill
\begin{Ex}{Warm-ups}
\end{Ex}
\begin{ex}
Who is the most famous mathematician of all time?
\begin{sol}
Euler
\end{sol}
\end{ex}
\begin{ex}
Resolve each of the following proper rational expressions (some filler
here) into partial fractions.
\begin{mtmulticols}{3}
\begin{enumerate}[label=(\alph*)]
\item $\displaystyle{\frac{1}{(x - 1)(x - 2)}}$
\item $\displaystyle{\frac{5x + 4}{x^2 + x - 2}}$
\item $\displaystyle{\frac{7x - 1}{2x^2 - x - 1}}$
\item $\displaystyle{\frac{25}{18x^3 - 9x^2 - 11x + 2}}$
\item $\displaystyle{\frac{x^2 + 15x - 4}{(x + 1)(x + 2)(x - 8)}}$
\item $\displaystyle{\frac{48}{(x^2 - 1)(x^2 - 9)}}$
\item $\displaystyle{\frac{1}{x^3 + 10x^2 + 25x}}$
\item $\displaystyle{\frac{x}{(x + 4)^3}}$
\item $\displaystyle{\frac{11x + 18}{(x^2 - 4)(x + 3)}}$
\end{enumerate}
\end{mtmulticols}
\begin{sol}
(a) $\displaystyle{\frac{1}{x - 2} - \frac{1}{x - 1}}$ \quad
(b) $\displaystyle{\frac{2}{x + 2} + \frac{3}{x - 1}}$ \quad
(c) $\displaystyle{\frac{3}{2x + 1} + \frac{2}{x - 1}}$ \quad
(d) $\displaystyle{\frac{3}{3x + 2} - \frac{12}{6x - 1} + \frac{1}{x - 1}}$ \quad
(e) $\displaystyle{\frac{2}{x - 8} + \frac{2}{x + 1} - \frac{3}{x + 2}}$ \quad
(f) $\displaystyle{\frac{3}{x - 1} + \frac{3}{x + 1} - \frac{1}{x + 3} + \frac{1}{x - 3}}$ \quad
(g) $\displaystyle{\frac{1}{25(x + 5)} - \frac{1}{5(x + 5)^2} + \frac{1}{25 x}}$ \quad
(h) $\displaystyle{\frac{2}{x - 2} + \frac{1}{x + 2} - \frac{3}{x + 3}}$
\end{sol}
\end{ex}

\Closesolutionfile{ans}
\input{partial-fractions}

\end{document}


You should try to devise a scheme to keep each item answer with the corresponding item. Here is one suggestion, introducing a Solutionsublist' environment to contain the question and the list ofsolitem with the individual solutions to each item.

\documentclass{memoir}

\usepackage{amsmath}
\usepackage[inline]{enumitem}
\usepackage{xcolor}
\usepackage{ntheorem}
\theorembodyfont{\upshape}
\theoremseparator{.}

\Newassociation{sol}{Solution}{ans}
\renewcommand{\Solutionlabel}[1]{\small{#1.}}
\newtheorem{ex}{}[section]
\renewcommand{\theex}{\arabic{ex}}
\newenvironment{Ex}[1]{\begin{trivlist}\item \textsc{#1} %
\renewcommand{\Currentlabel}{#1}}{\end{trivlist}}

\usepackage{multicol}
\newenvironment{mtmulticols}[1]{%
\Writetofile{ans}{\string\begin{multicols}{#1}}
\begin{multicols}{#1}}{\end{multicols}%
\Writetofile{ans}{\string\end{multicols}}}

\Newassociation{solitem}{Solutionsubitem}{ans}
\renewcommand{\Solutionsubitemlabel}[1]{#1}
\newenvironment{Solutionsublist}{\Writetofile{ans}{\string\begin{Solution}{\theex}}}{\Writetofile{ans}{\string\end{Solution}}}

\begin{document}
\chapter{Polynomials}
\section{Partial fractions}
\label{sec:partial-fractions}
\tracingmacros=1
\Opensolutionfile{ans}[partial-fractions]
\section*{Exercises for Section \ref{sec:partial-fractions}}
\vspace{-0.6cm}
\hrulefill
\begin{Ex}{Warm-ups}
\end{Ex}
\begin{ex}
Who is the most famous mathematician of all time?
\begin{sol}
Euler
\end{sol}
\end{ex}
\begin{ex}
Resolve each of the following proper rational expressions (some filler
here) into partial fractions.
\begin{multicols}{3}
\begin{Solutionsublist}
\begin{enumerate}[label=(\alph*)]
\item $\displaystyle{\frac{1}{(x - 1)(x - 2)}}$
\begin{solitem}
$\displaystyle{\frac{1}{x - 2} - \frac{1}{x - 1}}$
\end{solitem}
\item $\displaystyle{\frac{5x + 4}{x^2 + x - 2}}$
\begin{solitem}
$\displaystyle{\frac{2}{x + 2} + \frac{3}{x - 1}}$
\end{solitem}
\item $\displaystyle{\frac{7x - 1}{2x^2 - x - 1}}$
\begin{solitem}
$\displaystyle{\frac{3}{2x + 1} + \frac{2}{x - 1}}$
\end{solitem}
\item $\displaystyle{\frac{25}{18x^3 - 9x^2 - 11x + 2}}$
\begin{solitem}
$\displaystyle{\frac{3}{3x + 2} - \frac{12}{6x - 1} + \frac{1}{x - 1}}$
\end{solitem}
\item $\displaystyle{\frac{x^2 + 15x - 4}{(x + 1)(x + 2)(x - 8)}}$
\begin{solitem}
$\displaystyle{\frac{2}{x - 8} + \frac{2}{x + 1} - \frac{3}{x + 2}}$
\end{solitem}
\item $\displaystyle{\frac{48}{(x^2 - 1)(x^2 - 9)}}$
\begin{solitem}
$\displaystyle{\frac{3}{x - 1} + \frac{3}{x + 1} - \frac{1}{x + 3} + \frac{1}{x - 3}}$
\end{solitem}
\end{enumerate}
\end{Solutionsublist}
\end{multicols}
\end{ex}

\Closesolutionfile{ans}
`