# exercises in the margin, hints and solutions at the end

I use the answers package to put questions in the text and hints and solutions at the end of the doc. Now I like to use the Tufte book style, and put small exercises in the margin and still have hints and solutions at the end of the doc. I tried to put an exercise in a marginpar, but that does not work. The MWE below explains the point in detail.

\Newassociation{solution}{Solution}{ans}
\Newassociation{hint}{Hint}{hint}
\newtheorem{exercise}{Ex.}

\renewcommand{\Hintlabel}[1]{\textbf{h.#1.}}
\renewcommand{\Solutionlabel}[1]{\textbf{s.#1.}}
\begin{document}

\Opensolutionfile{hint}
\Opensolutionfile{ans}

\section{Test}
\label{sec:sec-1}

With the answer package we can put hints and answers at the end of the doc, see below.
\begin{exercise}
What is $2+2$?
\begin{hint}
Why is this the same as $1+1+1+1$?
\end{hint}
\begin{solution}
4
\end{solution}
\end{exercise}

Now I like to use the Tufte book style, and put the number and text of an exercise in the margin, and have the hints and answers at the end of the doc, just as the $2+2$ example above. For example:

Let us practice with division. \sidenote{\textbf{Ex. 2} What is $21/7$?}`. More text, blah blah blah blah blah \sidenote{\textbf{Ex. 3} what is $48/6$?}.

This, however, does not work with the answer package, because using it requires to put the hint and the solution within the exercise environment. And marginpar does not to support this. In other words, this does not work:

\begin{verbatim}
\marginpar{
\begin{exercise}
What is $2+2$?
\begin{solution}
4
\end{solution}
\end{exercise}
}
\end{verbatim}

So, is there another way to obtain the feature as demonstrated by my exercises 2 and 3? Any help is much appreciated.

\Closesolutionfile{hint}
\Closesolutionfile{ans}
\subsection*{Hints}
\input{hint}
\subsection*{Solutions}
\input{ans}

\end{document}

You could try this old trick. I assembled this code here in stack exchange.

I'm no expert, but I do my list of exercises with key answers successfully.

\documentclass{exam}
\usepackage{multicol}
\usepackage{amsmath}

\unframedsolutions

\begin{document}
\begin{multicols}{2}
\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}

\begin{questions}

\question this is question 1

\begin{parts}
\item item a
\begin{solution}
solution of item a
\end{solution}

\item item b
\begin{solution}
solution of item b
\end{solution}

\item item c
\begin{solution}
solution of item c
\end{solution}
\item item d
\begin{solution}
~solution of item d
\end{solution}
\end{parts}

\question this is question 2
\begin{solution}
$\dfrac{1}{2}$

\end{solution}

\question this is question 3

\begin{parts}
\item item a
\begin{solution}
solution of item a
\end{solution}

\item item b
\begin{solution}
solution of item b
\end{solution}

\item item c
\begin{solution}
solution of item c
\end{solution}
\item item d
\begin{solution}
solution of item d
\end{solution}
\end{parts}

\question this is question 4
\begin{solution}
this is a long long long long long long long long long long long long long solution for question 4
\end{solution}

\end{questions}
\end{multicols}

\vskip20pt
\begin{center}
\end{center}

\begin{multicols}{2}
\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}

\end{multicols}
\end{document}
• I still have some minor issues that make this code not entirely automatic. You still have to label manually the labels of the question and the parts in the solution environment. Anyways, it works. Aug 7, 2020 at 0:40
• Thanks for your answer. I resolved the problem differently, but putting the exercise at the end, and just include a reference to the exercise in the margin. For larger exercises that works better. Oct 14, 2020 at 18:34

I found this trick, but I am still curious to see a better answer.

\begin{exercise}\label{ex:1}
\marginpar{{\cref{ex:1} What is $2+2$?}
\begin{hint}
Why is this the same as $1+1$?
\end{hint}
\begin{solution}
4
\end{solution}
\end{exercise}

A possible solution for xsim users:

\usepackage{lipsum}
\usepackage{xsim}
\usepackage{needspace}

\DeclareExerciseProperty{hint}

% we'll use a description list for the hints:
\newcommand\printhints{%
\begin{description}
\ForEachUsedExerciseByType{%
\GetExercisePropertyT{hint}
{\item[##3]####1}%
}%
\end{description}
}

\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}

\newsavebox\exercisebox
\DeclareExerciseEnvironmentTemplate{margin}{%
\begin{lrbox}{\exercisebox}
\textbf{\GetExerciseProperty{counter}} %
\IfExercisePropertySetT{subtitle}{(\GetExerciseProperty{subtitle}) }%
}{%
\end{lrbox}%
\marginpar{\raggedright\unhbox\exercisebox}%
}

\DeclareExerciseEnvironmentTemplate{enumerate}
{\begin{description}\item[\GetExerciseProperty{counter}]}
{\end{description}}

\xsimsetup{
exercise/template = margin ,
solution/template = enumerate ,
exercise/the-counter = \arabic{exercise}.
}

\begin{document}

\section{Problems}
% set hint through option:
\begin{exercise}[subtitle=Pythagoras,hint=This is a hint to the first problem.]
This is the first problem.
\end{exercise}
\begin{solution}
This is the solution to the first problem.
\end{solution}
\lipsum[2]

\begin{exercise}[subtitle=Another Problem]
This is the second problem.
\end{exercise}
\begin{solution}
This is the solution to the second problem.
\end{solution}
\lipsum[3]

% set hint with custom command:
\begin{exercise}
This is the third problem.
\hint{This is a hint to the third problem.}
\end{exercise}
\begin{solution}
This is the solution to the third problem.
\end{solution}
\lipsum[4]

\section{Hints}
\printhints

\section{Solutions}