# Exercise package : how to reference a problem environment in answers?

Let us consider the following MWE.

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[frenchb]{babel}

\usepackage[lastexercise]{exercise}

%Environnement exercice
\newenvironment{exo}[1][\unskip]{%
\begin{Exercise}[title={#1}]}
{\end{Exercise}}

%Problème
\newcounter{prob}
\newenvironment{prob}[1][\unskip]{%
\begin{Exercise}[name={Problème}, counter={prob}, title={#1}]} {\end{Exercise}}

%%%%%%%%%%%%
\begin{document}

ABCDE

\begin{exo}
ABCDE
\end{exo}

A

\begin{prob}
ABC
\end{prob}
A
\end{document}


Output :

Is it possible to change the line Answer (Exercise 1) in Answer (Problem 1) automatically in terms of the exercise type ?

The exercise package stores custom names, if provided, in the internal macro \@ExerciseName. This is listed in page 17 of the manual:

\define@key{PPExercise}{name}%
{\global\@ExeReNametrue\gdef\@ExerciseName{#1}}


You can use this macro in the definition of the answer header. Note that because it is an internal macro (it contains @) you need to surround the use of the macro with \makeatletter and \makeatother.

When no custom name is provided (such as in the exo environment in the example) then this macro is either undefined (if no custom names have been used so far) or empty. In that case you should use the original \ExerciseName. In the code below the macro \ifdefempty from the etoolbox package is used to test which of the two macros should be used.

MWE:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[frenchb]{babel}
\usepackage{etoolbox}

\usepackage[lastexercise]{exercise}

%Environnement exercice
\newenvironment{exo}[1][\unskip]{%
\begin{Exercise}[title={#1}]}
{\end{Exercise}}

%Problème
\newcounter{prob}
\newenvironment{prob}[1][\unskip]{%
\begin{Exercise}[name={Problème}, counter={prob}, title={#1}]} {\end{Exercise}}

\makeatletter
\def\myExerciseName{\ifdefempty{\@ExerciseName}{\ExerciseName}{\@ExerciseName}}
\makeatother
%%%%%%%%%%%%
\begin{document}

ABCDE

\begin{exo}
ABCDE
\end{exo}

A

\begin{prob}
ABC
\end{prob}
A