# Cross-reference two numbered environments

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.

• 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 '14 at 20:58
• Maybe you are looking for something similar to this question? – Ruben Nov 4 '14 at 21:02
• For your second objective, see Link from label to ref and back? – Mico Nov 4 '14 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 '14 at 21:15
• 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. – Ethan Bolker Nov 7 '14 at 14:51

\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}


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}


• Is there a way to make grouped a bunch of answers? So by example, you did an exercise and then his answer, but I don't want to show the answer immediately but after, till a put a command like \showanswer[#grupo] – jonaprieto Jan 2 '15 at 17:45
• @d555 This should be a new question – egreg Jan 2 '15 at 18:00

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*}


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}

\end{document}


So, the result looks like this:

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

• Sorry guys, I edited my answer because I found a better way, so I did it following yours advices. – jonaprieto Jan 2 '15 at 16:50

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}
\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.