1

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

\renewcommand{\AnswerName}{Solution}
\renewcommand{\AnswerHeader}{\medskip\centerline{\textbf{ Answer (\ExerciseName\ \ExerciseHeaderNB)}\smallskip}}
%%%%%%%%%%%%
\begin{document}

ABCDE

\begin{exo}
ABCDE   
\end{exo}

\begin{Answer}
A
\end{Answer}

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

Output :

enter image description here

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

2

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
\renewcommand{\AnswerName}{Solution}
\renewcommand{\AnswerHeader}{\medskip\centerline{\textbf{ Answer (\myExerciseName\ \ExerciseHeaderNB)}\smallskip}}
%%%%%%%%%%%%
\begin{document}

ABCDE

\begin{exo}
ABCDE   
\end{exo}

\begin{Answer}
A
\end{Answer}

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

\begin{exo}
XYZ   
\end{exo}

\begin{Answer}
QWE
\end{Answer}

\end{document}

Result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.