add tag
Anonymous 1123
Based on the answer [here](https://topanswers.xyz/tex?q=1603), now I want to make a link from exercise to solution and vice versa so that when I click on an `Exercise` in pdf file, I jump to `solution` of this `Exercise` and vice versa. 

![ScreenHunter 1078.png](/image?hash=2ab6661e4b86ad6241430adcabed1bf4335c8ab64ff1d39857dfea87e54f9371)
Top Answer
user 3.14159
As mentioned in [samcarters's comment](https://topanswers.xyz/transcript?room=1648&id=86169#c86169), `hyperref` can be used here. Maybe this code is overly complicated, but it seems to work, at least in simple test situations. Automatic labels as well as links are inserted. To accomplish that, the commands that produce the headers are modified. For the solution, this can be done by redefining `\Solutionlabel`, as illustrated in the manual of the `answers` style. For the exercise, a new theorem style is defined to this effect.

```
\documentclass[12pt,a4paper]{report}
\usepackage{amsthm,amsfonts,amssymb,amsmath}
\usepackage{answers}
\usepackage{hyperref}

\newif\ifsolutions
\solutionstrue % comment this line for no solutions

\newtheoremstyle{autolabeled}%
  {}%
  {}%
  {}%
  {}%
  {\bfseries}%
  {.}%  % Note that final punctuation is omitted.
  {1ex}%
  {\hyperref[sol:autolabel-#2]{\thmname{#1}~\thmnumber{#2}\label{ex:autolabel-#2}}\thmnote{#3}}%

%\theoremstyle{definition}
\theoremstyle{autolabeled}
\Newassociation{sol}{Solution}{ans}
\newtheorem{ex}{Exercise}[chapter]
\renewcommand{\Solutionlabel}[1]{\label{sol:autolabel-#1}%
\hyperref[ex:autolabel-#1]{#1}}

\begin{document}
\chapter{Try}
\Opensolutionfile{ans}[ans1]

\section{Problems}

\begin{ex}\label{ex:first}
	 Why are marmots called marmots? 
 \begin{sol}\label{sol:first}
	Because they look like marmots.
 \end{sol}
\end{ex}

\clearpage
\begin{ex}
 Why are ducks called ducks? 
 \begin{sol}
  Because they look like ducks.
 \end{sol}
\end{ex}

\clearpage

\Closesolutionfile{ans}
\ifsolutions
\section{Solutions}
\input{ans1}
\fi
\end{document}
```
![ani.gif](/image?hash=beeec193d1b708a33b33a8df22076f09f0afb7e3c72bd91fb5c85be948b952f8)

This is a variation with colored links, in which the hyperlinks only appear if the target exists. That is, if you do not include the solutions (by setting `\solutionsfalse` or not setting `\solutionstrue`), there won't be hyperlinks.
```
\documentclass[12pt,a4paper]{report}
\usepackage{amsthm,amsfonts,amssymb,amsmath}
\usepackage{answers}
\usepackage{hyperref}
\hypersetup{
	colorlinks=true,
    linkcolor=blue,
}
\newif\ifsolutions
\solutionstrue % comment this line for no solutions
\makeatletter
\newcommand\HyperRefOnlyIfDefined[2]{%
\ifcsname r@#1\endcsname
\hyperref[#1]{#2}%
\else
#2%
\fi
}
\makeatother
\newtheoremstyle{autolabeled}%
  {}%
  {}%
  {}%
  {}%
  {\bfseries}%
  {.}%  % Note that final punctuation is omitted.
  {1ex}%
  {\HyperRefOnlyIfDefined{sol:autolabel-#2}{\thmname{#1}~\thmnumber{#2}\label{ex:autolabel-#2}}\thmnote{#3}}%

%\theoremstyle{definition}
\theoremstyle{autolabeled}
\Newassociation{sol}{Solution}{ans}
\newtheorem{ex}{Exercise}[chapter]
\renewcommand{\Solutionlabel}[1]{\label{sol:autolabel-#1}%
\HyperRefOnlyIfDefined{ex:autolabel-#1}{#1}}

\begin{document}
\chapter{Try}
\Opensolutionfile{ans}[ans1]

\section{Problems}

\begin{ex}\label{ex:first}
	 Why are marmots called marmots? 
 \begin{sol}\label{sol:first}
	Because they look like marmots.
 \end{sol}
\end{ex}

\clearpage
\begin{ex}
 Why are ducks called ducks? 
 \begin{sol}
  Because they look like ducks.
 \end{sol}
\end{ex}

\clearpage

\Closesolutionfile{ans}
\ifsolutions
\section{Solutions}
\input{ans1}
\fi
\end{document}
```
![Screen Shot 2021-01-17 at 2.27.17 AM.png](/image?hash=91df340a9cd5cf679489b28aa802980f54d798555ec1f8599b2c85001325d267)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.