I have read the answers to this question about how to manually include a link to the answers in each exercise when using the exercise package.
My question is whether it is possible to automate this in some way, by adding some code in the preamble, so that each exercise automatically includes a link to the answers.
\documentclass[11pt,twoside]{article}
\usepackage{amsmath}
\usepackage[colorlinks=true]{hyperref}
\hypersetup{allcolors=magenta}
\usepackage[usenames,svgnames,dvipsnames,table]{xcolor}
\colorlet{lightcyan}{cyan!40!white}
\usepackage{ifthen}
\newboolean{firstanswerofthechapter}
\usepackage{chngcntr}
\usepackage{stackengine}
\usepackage[lastexercise,answerdelayed]{exercise}
\counterwithin{Exercise}{section}
\counterwithin{Answer}{section}
\renewcounter{Exercise}[section]
\newcommand{\QuestionNB}{\bfseries\arabic{Question}.\ }
\renewcommand{\ExerciseName}{\textbf{Exercise}}
\renewcommand{\ExerciseHeader}{\noindent\def\stackalignment{l}% code from https://tex.stackexchange.com/a/195118/101651
\stackunder[0pt]{\colorbox{cyan}{\textcolor{white}{\LARGE\ExerciseName\;\textbf{\LARGE\ExerciseHeaderNB}}}}{\textcolor{lightcyan}{\rule{\linewidth}{2pt}}}\medskip}
\renewcommand{\AnswerName}{Exercise}
\renewcommand{\AnswerHeader}{\ifthenelse{\boolean{firstanswerofthechapter}}%
{\bigskip\noindent\textcolor{cyan}{\textbf{CHAPTER \thesection}}\newline\newline%
\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page %
\pageref{\AnswerRef}}}\smallskip}
{\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page \pageref{\AnswerRef}}}\smallskip}}
\setlength{\QuestionIndent}{16pt}
\begin{document}
\section{Section 1}
\begin{Exercise}\label{EX1}
Answers here: \refAnswer{EX1}
\Question question
\Question question
\Question question
\end{Exercise}
\setboolean{firstanswerofthechapter}{true}
\begin{Answer}[ref={EX1}]
\Question answer
\Question answer
\Question answer
\end{Answer}
\setboolean{firstanswerofthechapter}{false}
\section{Answers}
\shipoutAnswer
\end{document}
Best Answer
It seems that exercise provides both Exercise and Answer counters, which will provide an automatic naming protocol.
\AtBeginExercise
will handle the\label
. Adding an argument to\begin{Answer}
was a bit harder.