7

Consider the following code:

\documentclass[a4paper,12pt]{book}
\usepackage{amsthm}
\usepackage{hyperref}

\theoremstyle{plain}
\newtheorem{exercise}{Exercise}
\newtheorem{solution}{Solution}

\begin{document}
  \begin{exercise}
    This is an exercise.
  \end{exercise}

  \begin{solution}
    This is a solution.
  \end{solution}
\end{document}

I'd like to:

  1. Assign the same number to each "exercise" and "solution" pair (possibly via some sort of unique ID they share?);
  2. Automatically generate hyperlinks from an "exercise" title to its corresponding "solution" and vice versa, so one can easily navigate from a problem to its solution and vice versa.

I'm probably asking for something fairly difficult -- I'd be happy with just the first question. I'm not sure if they should be two separate questions but they seem quite related to me.

9
  • 1
    Will the location of the exercise and solution always be next to one another? Or do you plan on putting solutions (say) at the end of the document, while the exercises will be part of the main document body?
    – Werner
    Nov 4, 2014 at 20:58
  • Maybe you are looking for something similar to this question?
    – Ruben
    Nov 4, 2014 at 21:02
  • 1
    For your second objective, see Link from label to ref and back?
    – Mico
    Nov 4, 2014 at 21:10
  • @Werner Location should be irrelevant in the ideal solution, but I plan to keep them separate (solutions at the end of the document).
    – DvD
    Nov 4, 2014 at 21:15
  • 1
    The nice answers so far address your question, but they don't address one you didn't ask and might want to consider. I have found it very convenient to write the solutions right where the problems appear in the text, then make them appear at the end (or in another document). The answers package (and others) make this possible. You should be able to get the numbering and linking to work with its structure. Nov 7, 2014 at 14:51

4 Answers 4

3
\documentclass[a4paper,12pt]{book}
\usepackage{amsthm}
\usepackage{hyperref}

\theoremstyle{plain}
\newcounter{exercise}[chapter]
\renewcommand\theexercise{\thechapter.\arabic{exercise}}
\newtheorem{xexercise}[exercise]{Exercise}
\newtheorem*{xsolution}{Solution}

\newenvironment{exercise}[1]
{\begin{xexercise}\label{#1}\def\thissoln{\hyperref[sol-#1]{Solution}}}
{\par\thissoln\end{xexercise}}

\newenvironment{solution}[1]
{\begin{xsolution}[Solution \ref{#1}]\label{sol-#1}}
{\end{xsolution}}

\begin{document}

\chapter{aaa}
  \begin{exercise}{z}
    This is an exercise.
  \end{exercise}
\chapter{bbb}
  \begin{exercise}{a1}
    This is an exercise.
  \end{exercise}
  \begin{exercise}{thing}
    This is an exercise.
  \end{exercise}

\chapter{solutions}
  \begin{solution}{z}
    This is a solution.
  \end{solution}
  \begin{solution}{a1}
    This is a solution.
  \end{solution}
  \begin{solution}{thing}
    This is a solution.
  \end{solution}
\end{document}
3

You can define the solution environment to have the same number as the last exercise, but with the possibility of having a different number grabbed through the \label-\ref mechanism.

The solution number will be a link to the exercise. If the solution doesn't immediately follow the corresponding exercise, because you want to state two exercises next to each other, specify the label to the exercise and use it as an optional argument to \begin{solution}.

\documentclass[a4paper,12pt]{book}
\usepackage{amsthm}
\usepackage{hyperref}

\theoremstyle{definition} % body text is upright
\newtheorem{innerexercise}{Exercise}
\newtheorem*{innersolution}{Solution of \exerciseref}
\newenvironment{exercise}
  {\innerexercise\label{exercise@@\theinnerexercise}}
  {\endinnerexercise}
\newenvironment{solution}[1][]
  {\if!#1!%
     \def\exerciseref{\ref{exercise@@\theinnerexercise}}%
   \else
     \def\exerciseref{\ref{#1}}%
   \fi
   \innersolution}
  {\endinnersolution}

\begin{document}
\begin{exercise}
This is an exercise.
\end{exercise}

\begin{solution}
This is a solution.
\end{solution}

\begin{exercise}\label{another}
This is another exercise.
\end{exercise}

\begin{exercise}\label{ohwell}
This is yet another exercise.
\end{exercise}

\begin{solution}[another]
A solution.
\end{solution}

\begin{solution}[ohwell]
Too much.
\end{solution}

\end{document}

enter image description here

3
3

I found the package exercise.sty and It fulfill all requirements.

You define, exercises like that:

\begin{Exercise} [⟨key val list⟩] ... \end{Exercise}

\begin{Exercise*} [⟨key val list⟩] ... \end{Exercise*}

And Answers:

\begin{Answer} [⟨key val list⟩] ... \end{Answer}

All above with these possible key values:

label={⟨string ⟩} 
title={⟨string ⟩} 
difficulty={⟨number ⟩} 
origin={⟨string ⟩} 
name={⟨string ⟩} 
counter={⟨counter ⟩} 
number={⟨string ⟩}

you can use label key and then in an answer referencing with ref and number key values. Btw, you can use these label as a normal label latex with \ref{label}.

By example:

\documentclass[12pt]{article}
\usepackage{hyperref}
\usepackage{exercise}
\begin{document}

\begin{Exercise}[label=ex1]
This is an exercise.
\end{Exercise}

\begin{Answer}
This is an answer.
\end{Answer}

\end{document}

So, the result looks like this:

enter image description here

This package exercise has really others options, like environments of exercises and other stuffs.

2
  • 2
    While the content behind this link might answer the question, it is nevertheless a link only and links can become dead. You should elaborate on your answer
    – user31729
    Jan 2, 2015 at 15:34
  • Sorry guys, I edited my answer because I found a better way, so I did it following yours advices. Jan 2, 2015 at 16:50
1

Based on the collective input I developed a couple alternatives:

  1. Without answers package

\documentclass[a4paper,12pt]{book}
\usepackage{amsthm}
\usepackage{hyperref}

\theoremstyle{definition}
\newtheorem{innerexercise}{Exercise}
\newtheorem*{innersolution}{Solution \exerciseref}
\newenvironment{exercise}[1]
  {\innerexercise\label{#1} (p\pageref{sol:#1})}
  {\endinnerexercise}
\newenvironment{solution}[1]
  {\def\exerciseref{\ref{#1}}%
   \innersolution\label{sol:#1} (p\pageref{#1})}
  {\endinnersolution}

\begin{document}

\chapter{Exercise One}

\begin{exercise}{labelone}
 Exercise one.
\end{exercise}

\chapter{Exercise Two}

\begin{exercise}{labeltwo}
 Exercise two.
\end{exercise}

\chapter{Solutions}

\begin{solution}{labeltwo}
 Solution two.
\end{solution}

\begin{solution}{labelone}
 Solution one.
\end{solution}
  1. With answers package

\documentclass[a4paper,12pt]{book}
    \usepackage{amsthm}
    \usepackage{answers}
      \Newassociation{sol}{solution}{ans}
    \usepackage{hyperref}

\newtheorem{innerexercise}{Exercise}
\newenvironment{exercise}[1]
  {\begin{innerexercise}\label{#1} (p.~\pageref{sol:#1})\renewcommand{\Currentlabel}{#1}}
  {\end{innerexercise}}
\newtheorem*{innersolution}{Solution \exerciseref}
\renewenvironment{solution}[1]
  {\def\exerciseref{\ref{#1}}%
   \innersolution\label{sol:#1} (p.~\pageref{#1})}
  {\endinnersolution}

\begin{document}
\Opensolutionfile{ans}[solutionsfile]
\chapter{Problems}
\begin{exercise}{First}
 First exercise
 \begin{sol}
  First solution.
 \end{sol}
\end{exercise}
\begin{exercise}{Second}
 Second exercise
 \begin{sol}
  Second solution.
 \end{sol}
\end{exercise}
\Closesolutionfile{ans}

\chapter{Solutions}
\input{solutionsfile}
\end{document}

In any case I can manage numbered exercise-solution pairs with the added benefit of \pageref that serves both as a hyperlink for people navigating this content on a PDF and as a page marker for those who read it on paper.

The second solution is more manageable because I don't have to keep solutions ordered. It's also better style as solutions are kept inside their respective "parents" and you just need to define one label per pair.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .