Text FormattingReferences back and forth between exercise and solution

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
voorneveld
Posts: 15
Joined: Mon Sep 07, 2009 11:18 am

References back and forth between exercise and solution

Post by voorneveld »

In the MWE below, I use hyperref to link from a solution back to an exercise.

But is it possible to link back and forth, i.e., by clicking on the exercise, I go to the answer. Clicking on the number of the answer, I go back to the exercise?

Code: Select all

\documentclass[english]{article}

\usepackage{hyperref}
\usepackage{babel}

% For exercise numbers in the margin and a small font
\usepackage{ntheorem}
\theoremstyle{margin}% Added for marginal number
\theoremheaderfont{\small\bfseries}
\theorembodyfont{\small}
\theoremsymbol{}
\newtheorem{exercise}{\hspace*{-0.45em}}[section]%corrects for spacing between name and beginning text

% My solution environment
\newenvironment{solution}[1]
{%
\begingroup%
    \small%
    \trivlist%
    \item[\llap{\textbf{\ref{#1}}}]%
}%
{%
\endgroup%
\endtrivlist%
}%

\begin{document}
\section{First section}

\begin{exercise}\label{first exercise}
Question
\end{exercise}

\begin{solution}{first exercise}
Answer
\end{solution}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

References back and forth between exercise and solution

Post by cgnieder »

Here is an idea. It relies upon the solutions being typeset in the same order and the same section as the exercises it will produce warnings if they're not (and errors if hyperref isn't loaded but that could easily be taken care of).

Code: Select all

\documentclass[english]{article}

\usepackage{hyperref}
\usepackage{babel}

\newcounter{exercise}[section]
\renewcommand*\theexercise{\thesection.\arabic{exercise}}
\newenvironment{exercise}%
  {%
    \refstepcounter{exercise}%
    \hypertarget{ex:\theexercise}{}%
    \small
    \trivlist
    \item[\llap{\textbf{\hyperlink{sol:\theexercise}{\theexercise}}}]%
  }%
  {%
    \endtrivlist
  }

\newcounter{solution}[section]
\renewcommand*\thesolution{\thesection.\arabic{solution}}
\newenvironment{solution}%
  {%
    \refstepcounter{solution}%
    \hypertarget{sol:\thesolution}{}%
    \small
    \trivlist
    \item[\llap{\textbf{\hyperlink{ex:\thesolution}{\thesolution}}}]%
  }%
  {%
    \endtrivlist
  }

\usepackage{lipsum}% for dummy text
\begin{document}
\section{First section}

\begin{exercise}
 Question: \lipsum[4]
\end{exercise}

\begin{exercise}
 Question: \lipsum[4]
\end{exercise}

\newpage
\begin{solution}
 Answer: \lipsum[4]
\end{solution}

\begin{solution}
 Answer: \lipsum[4]
\end{solution}

\end{document}
Regards
site moderator & package author
voorneveld
Posts: 15
Joined: Mon Sep 07, 2009 11:18 am

Re: References back and forth between exercise and solution

Post by voorneveld »

Thank you for the answer!

I have the solutions in an appendix and (to make things slightly worse) sometimes omit solutions to exercises that I want to give my students as homework.

Is there a way to work around that?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

References back and forth between exercise and solution

Post by cgnieder »

Well, the first version was a very preliminary draft! I polished it a bit and now the only restriction is to use the solutions in the same order as the exercises. If a solution is to be omitted the macro \omitsolution is used. As the code grew a bit I decided to make this a small package for you called exnsols. You should save the following code under the name exnsols.sty in your project folder.

Code: Select all

% package EXNSOLS
\ProvidesPackage{exnsols}[2012/09/04 v0.1 exercises and solutions]

% if hyperref is not loaded there shouldn't be errors:
\newcommand*\ex@hypertarget[2]{#2}
\newcommand*\ex@hyperlink[2]{#2}

\AtBeginDocument{
  \@ifpackageloaded{hyperref}{}
  {
    \let\hypertarget\ex@hypertarget
    \let\hyperlink\ex@hyperlink
  }
}

% the exercise environment, counted sectionwise
\newcounter{exercise}[section]
\newcounter{exerciseID}
\renewcommand*\theexercise{\thesection.\arabic{exercise}}
\newenvironment{exercise}%
  {%
    \stepcounter{exercise}%
    \refstepcounter{exerciseID}%
    \hypertarget{ex:\theexerciseID}{}%
    \expandafter\xdef\csname exercise\roman{exerciseID}\endcsname{\theexercise}%
    \ifcsname solution\roman{exerciseID}\endcsname
    \else
      \let\hyperlink\ex@hyperlink
    \fi
    \small
    \trivlist
    \item[\llap{\textbf{\hyperlink{sol:\theexerciseID}{\theexercise}}}]%
  }%
  {\endtrivlist}

% the solution environment
\newcounter{solutionID}
\newenvironment{solution}%
  {%
    \refstepcounter{solutionID}%
    \immediate\write\@auxout{%
      \noexpand\global\noexpand\@namedef{solution\roman{solutionID}}{\thesolutionID}}%
    \hypertarget{sol:\thesolutionID}{}%
    \small
    \trivlist
    \item[\llap{\textbf{\hyperlink{ex:\thesolutionID}{\@nameuse{exercise\roman{solutionID}}}}}]%
  }%
  {\endtrivlist}

\newcommand*\omitsolution{\stepcounter{solutionID}}
\endinput
Now you can use it like follows. As it writes stuff to the aux file to check if there is a solution that can be referenced it needs two compiler runs.

Code: Select all

\documentclass[english]{article}
\usepackage{exnsols}

\usepackage{hyperref}
\usepackage{babel}

\usepackage{lipsum}% for dummy text
\begin{document}
\section{First section}

\begin{exercise}
 Question: \lipsum[4]
\end{exercise}

\begin{exercise}
 Question: \lipsum[4]
\end{exercise}

\begin{exercise}
 Question: \lipsum[4]
\end{exercise}

\newpage
\appendix
\section{Solutions}
\begin{solution}
 Answer: \lipsum[4]
\end{solution}

\omitsolution

\begin{solution}
 Answer: \lipsum[4]
\end{solution}

\end{document}
Regards
site moderator & package author
voorneveld
Posts: 15
Joined: Mon Sep 07, 2009 11:18 am

Re: References back and forth between exercise and solution

Post by voorneveld »

This is wonderful. Thank you so much!

Mark
kvnamboothiri
Posts: 1
Joined: Fri Dec 21, 2012 7:31 pm

Re: References back and forth between exercise and solution

Post by kvnamboothiri »

Brilliant! I got this solutions for peanuts, in the first Google search itself. Thanks a lot!
Post Reply