2

I would like to use the answers package to create exercises and answers (printed in different parts of the document). The exercises should refer to the solutions with a margin note (via marginnote) and the other way around. The MWE below already works quite well but I couldn't figure out how to define my own solutions environment (see 'FAILS'). It should be like the exercise environment, just with label 'Solution' instead of 'Exercise'.

\documentclass{scrreprt}
\usepackage[american]{babel}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{etoolbox}
\usepackage{marginnote}
\usepackage{answers}
\usepackage{hyperref}

\declaretheoremstyle[%
  spaceabove=0.5em,
  spacebelow=0.5em,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont%
]{mythmstyle}
\declaretheorem[style=mythmstyle, numberwithin=chapter]{exercise}
% \declaretheorem[style=mythmstyle, sibling=exercise]{solution}% => FAILS

\Newassociation{solution}{sol}{solutions}

\begin{document}
\Opensolutionfile{solutions}

\section{Section 1}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 1}
\end{Filesave}

\begin{exercise}[Header 1]\label{exlabel1}\marginnote{Sol.\ p.~\pageref{sollabel1}}\par\noindent
  First exercise
  \begin{solution}[Header 1]\label{sollabel1}\marginnote{Ex.\ p.~\pageref{exlabel1}}\par\noindent
    First solution.
  \end{solution}
\end{exercise}

\clearpage
\section{Section 2}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 2}
\end{Filesave}

\begin{exercise}[Header 2]\label{exlabel2}\marginnote{Sol.\ p.~\pageref{sollabel2}}\par\noindent
  \begin{enumerate}
  \item Part 1
  \item Part 2
  \end{enumerate}
  \begin{solution}[Header 2]\label{sollabel2}\marginnote{Ex.\ p.~\pageref{exlabel2}}
    \begin{enumerate}
    \item Solution Part 1
    \item Solution Part 2
    \end{enumerate}
  \end{solution}
\end{exercise}

\Closesolutionfile{solutions}

\clearpage

\input{solutions}

\end{document}

The final goal would be to not having to set the labels and margin notes manually. One idea is to manually define the exercise and solution environments and to pass the same 'base label' to each of them as an argument. These two environments then create a label for the exercise and one for its solution and refer to each other. I tried that in a different post (see Page number hyperlinks (exercises to solutions: fail; solutions to exercises: ok)) but wasn't too successful yet.

Update

Here is how far I got with the ideas on Adding another answer hyperlinked to the question itself

\documentclass{scrreprt}
\usepackage[american]{babel}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{etoolbox}
\usepackage{marginnote}
\usepackage{answers}
\usepackage{hyperref}

\newcounter{counter}
\numberwithin{counter}{section}
\makeatletter
\newenvironment{exercise}[2][]{\refstepcounter{counter}\par% #1 = header; #2 = label
  \normalfont\topsep6\p@\@plus6\p@\relax
  \trivlist
  \labelsep 0pt
  \def\mysollabel{#2}
  \preto\mysollabel{sol:}
  \def\myexlabel{#2}
  \preto\myexlabel{ex:}
\item[\hskip\labelsep\sffamily\bfseries Exercise~\thecounter\ #1]\label{\myexlabel}% this '\label' correctly refers to the exercise
  \marginnote{Solution p.~\pageref{\mysollabel}}%
  \ignorespaces%
}{%
  \endtrivlist\@endpefalse
}
\makeatother

\Opensolutionfile{solutions}
\Newassociation{solution}{Soln}{solutions}

\begin{document}
\section{Section 1}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 1}
\end{Filesave}

\begin{exercise}[Header 1]{ex:1:label}
  First exercise
  \begin{solution}[Header 1]{ex:1:label}
    First solution.
  \end{solution}
\end{exercise}

\clearpage
\section{Section 2}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 2}
\end{Filesave}

\begin{exercise}[Header 2]{ex:2:label}
  \begin{enumerate}
  \item Part 1
  \item Part 2
  \end{enumerate}
  \begin{solution}[Header 2]{ex:2:label}
    \begin{enumerate}
    \item Solution Part 1
    \item Solution Part 2
    \end{enumerate}
  \end{solution}
\end{exercise}

\Closesolutionfile{solutions}

% Renew the solution environment so that it hyperlinks back to the exercise
\makeatletter
\renewenvironment{Soln}[2][]{\par% #1 = header; #2 = label
  \normalfont\topsep6\p@\@plus6\p@\relax
  \trivlist
  \labelsep 0pt
  \def\myexlabel{#2}
  \preto\myexlabel{ex:}
  \def\mysollabel{#2}
  \preto\mysollabel{sol:}
\item[\hskip\labelsep\sffamily\bfseries Solution~\ref{\myexlabel}\ #1]\hypertarget{\mysollabel}{}%
  \marginnote{\hyperlink{\myexlabel}{Exercise p.~\pageref{\myexlabel}}}%
  \ignorespaces%
}%
{%
  \popQED\endtrivlist\@endpefalse
}%
\makeatother

\clearpage
\IfFileExists{solutions.tex}{\input{solutions.tex}}{}

\end{document}
  • Take a look at tex.stackexchange.com/questions/45670/… it contains the mechanics of how to link back and forth between questions and answers – cmhughes Jun 29 '17 at 16:04
  • Hi, thanks. I looked at it and tried to renew the solution environment (and work with hyperlinks)... see the Update. I'm far from being an expert in LaTeX, so not sure how to continue from the here. I see the trick with renewing the solution environment, but I need to somehow pass a second argument to it (the 'header'). Also, there is some still some major flaw as labels don't show up in the margin notes. – Marius Hofert Jun 29 '17 at 17:13
  • Not using the answers package, but: the crossref example here github.com/cgnieder/xsim/tree/master/examples does exactly that: crossreferencing solutions (and exercises) in the margin. – clemens Jun 30 '17 at 4:53
  • Thanks. I copied all files from 'code' to the current working directory, but still obtain ./xsim.interface.code.tex:51: Undefined control sequence. l.51 \NewExpandableDocumentCommand \numberofusedexercises {}. Out of curiosity, will xsim be in texlive at some point? – Marius Hofert Jul 2 '17 at 10:23
  • Hey there Marius, I'm having the same problem. I imagine it's because I'm running this on Debian Stretch and I don't have a recent enough LaTeX engine. Can you tell me what platform you're seeing this on? $ lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 9.0 (stretch) Release: 9.0 Codename: stretch Cheers, – cjac Jul 31 '17 at 20:28
1

From my Update I tried to improve the code. I realized that Soln needs to have three arguments and I made them all formal/required arguments so that answers can work with them (this part originally failed). I didn't need to use hyperlink specifically (the links turned out correctly). I'll convert this MWE into the original document and see whether everything still works (I'll report back if not). Feel free to comment on possible improvements of the code.

\documentclass{scrreprt}
\usepackage[american]{babel}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{etoolbox}
\usepackage{marginnote}
\usepackage{answers}
\usepackage{hyperref}

\newcounter{counter}
\numberwithin{counter}{section}

\newenvironment{exercise}[2]{\refstepcounter{counter}\par% #1 = header; #2 = label
  \trivlist
  \labelsep 0pt
  \def\mysollabel{#2}
  \preto\mysollabel{sol:}
  \def\myexlabel{#2}
  \preto\myexlabel{ex:}
\item[\hskip\labelsep\sffamily\bfseries Exercise~\thecounter~(#1)]\label{\myexlabel}% this '\label' refers to the exercise
  \marginnote{Solution~p.~\pageref{\mysollabel}}%
  \ignorespaces%
}{%
  \endtrivlist
}

\Opensolutionfile{solutions}
\Newassociation{solution}{Soln}{solutions}

\begin{document}
\section{Exercises Section 1}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 1}
\end{Filesave}

\newcommand*{\headerOne}{Header 1}
\begin{exercise}{\headerOne}{ex:1:label}\\
  First exercise
  \begin{solution}{\headerOne}{ex:1:label}\\
    First solution.
  \end{solution}
\end{exercise}

\clearpage
\section{Exercises Section 2}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 2}
\end{Filesave}

\newcommand*{\headerTwo}{Header 2}
\begin{exercise}{\headerTwo}{ex:2:label}
  \begin{enumerate}
  \item Part 1
  \item Part 2
  \end{enumerate}
  \begin{solution}{\headerTwo}{ex:2:label}
    \begin{enumerate}
    \item Solution Part 1
    \item Solution Part 2
    \end{enumerate}
  \end{solution}
\end{exercise}

\Closesolutionfile{solutions}

\renewenvironment{Soln}[3]{% #1 = label (from 'answers'; #2 = header; #3 = label
  \pushQED{\qed}
  \trivlist
  \labelsep 0pt
  \def\myexlabel{#3}
  \preto\myexlabel{ex:}
  \def\mysollabel{#3}
  \preto\mysollabel{sol:}
\item[\hskip\labelsep\sffamily\bfseries Solution~#1~(#2)]\label{\mysollabel}%
    \marginnote{Exercise~p.~\pageref{\myexlabel}}%
    \ignorespaces%
}{%
  \popQED\endtrivlist
}

\clearpage
\IfFileExists{solutions.tex}{\input{solutions.tex}}{}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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