Why there is no space left after the answer header?

The answer is in the definitions of \@@@ExeEnv and \@@@AnswerEnv

% exercise.sty, line 365:
\newcommand{\@@@ExeEnv}{%
    \pagebreak[1]\vskip\ExerciseSkipBefore
    \@QuestionLevel1
    \refstepExecounter
    \begingroup\@getExerciseInfo\ExerciseHeader
    \addcontentsline{\ext@exercise}{\toc@exercise}{\ExerciseName\
        \theExercise\ \expandafter{\itshape \ExerciseTitle}\hspace{.66em}}
    \endgroup\AtBeginExercise}

% exercise.sty, line 656:
\newcommand{\@@@AnswerEnv}{%
    \pagebreak[1]\vskip\AnswerSkipBefore\@QuestionLevel1
    \begingroup\@getAnswerInfo\AnswerHeader\endgroup\AtBeginAnswer}

As you can see, there is an unprotected end-of-line on line 371, which is what produces the space after “Exercise 1.1”.

\documentclass{book}
\usepackage{amsmath}
\usepackage[lastexercise]{exercise}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\@@@ExeEnv}{ \endgroup}{\endgroup}{}{} % remove the wrong space
\makeatother

\setlength{\ExerciseSkipBefore}{\baselineskip}
\setlength{\ExerciseSkipAfter}{1\baselineskip}
\setlength{\AnswerSkipBefore}{0\baselineskip}
\setlength{\AnswerSkipAfter}{1.2\baselineskip}
\renewcounter{Exercise}[chapter]

\renewcommand{\ExerciseHeader}{%
  \noindent\bfseries\ExerciseName\ \thechapter.\ExerciseHeaderNB. %
}
\renewcommand{\AnswerName}{Solution}
\renewcommand{\AnswerHeader}{%
  \noindent\bfseries\AnswerName\ \thechapter.\ExerciseHeaderNB. %
}

\begin{document}

    \chapter{My first chapter}
    \section{Problems}
    \begin{Exercise}
        Text of the 1st problem 
    \end{Exercise}
    \begin{Answer}
        Text of the solution of the 1st problem
    \end{Answer}
    \begin{Exercise}
        Text of the 2nd problem 
    \end{Exercise}
    \begin{Answer}
        Text of the solution of the 2nd problem
    \end{Answer}
    \begin{Exercise}
        Text of the 3rd problem 
    \end{Exercise}
    \begin{Answer}
        Text of the solution of the 3rd problem
    \end{Answer}
\end{document}

enter image description here


Seems to be a bug for me. Adding an space at the end of definition

\renewcommand{\AnswerHeader}{\noindent\bfseries\AnswerName\ \thechapter.\ExerciseHeaderNB. }
%                                                                                        ^^^

solves the issue as workaround. For me it seems there is a \xspace missing in the definition for the solution header text in the package ...

In the documentation you can find

\newcommand{\AnswerHeader}{\medskip\centerline{\textbf{Answer of \ExerciseName\ \ExerciseHeaderNB}\smallskip}}

astonishing for me with an \smallskip at its end.

MWE:

\documentclass{book}

\usepackage{amsmath}
\usepackage[lastexercise]{exercise}
\setlength{\ExerciseSkipBefore}{\baselineskip}
\setlength{\ExerciseSkipAfter}{1\baselineskip}
\setlength{\AnswerSkipBefore}{0\baselineskip}
\setlength{\AnswerSkipAfter}{1.2\baselineskip}
\renewcounter{Exercise}[chapter]
\renewcommand{\ExerciseHeader}{\noindent\bfseries\ExerciseName\ \thechapter.\ExerciseHeaderNB.}
\renewcommand{\AnswerName}{Solution}
\renewcommand{\AnswerHeader}{\noindent\bfseries\AnswerName\ \thechapter.\ExerciseHeaderNB. }

\begin{document}
    \chapter{My first chapter}
    \section{Problems}
    \begin{Exercise}
        Text of the 1st problem 
    \end{Exercise}
    \begin{Answer}
        Text of the solution of the 1st problem
    \end{Answer}
    \begin{Exercise}
        Text of the 2nd problem 
    \end{Exercise}
    \begin{Answer}
        Text of the solution of the 2nd problem
    \end{Answer}
    \begin{Exercise}
        Text of the 3rd problem 
    \end{Exercise}
    \begin{Answer}
        Text of the solution of the 3rd problem
    \end{Answer}
\end{document}

result:

resulting pdf