Unwanted line break after beginning of solution environment in exam class

In your comnments you point out that you wish horizontal indenting-width of 1.5em via \AtBeginEnvironment{solution}{...} when beginning the first line of a subsequent paragraph.

Therefore I assume you don't wish any vertical gap before the first line of a subsequent paragraph.


The commented source code of LaTeX 2ε can be found at http://mirrors.ctan.org/macros/latex/base/source2e.pdf.

The environments list and trivlist are described in section File A ltlists.dtx, 55 List, and related environments.

The macro underlying the list-environment is the \list-macro.

It takes two undelimited arguments. (An undelimited argument either is a single token or is a collection of tokens nested in curly braces.)

The first argument of that macro can contain directives for "formatting" item-labels.

The second argument can contain other commands/directives, e.g., directives for adjusting horizontal and vertical placement.


With the environments answer and otheranswer I use the \list-macro and within the \list-macro's second argument I use \myProofOrSolution for choosing between two different sets of placement-directives: Within the solution-environment \mymarginadjust is set to \@secondoftwo. Otherwise it is set to \@firstoftwo.


According to section 55.4 Horizontal Spacing (dimens), the width of the horizontal indenting of paragraphs with things based on the \list-macro is \listparindent and the height of the vertical indenting of paragraphs with things based on the \list-macro is \parsep.

The order in time in which things get carried out by the \list-macro is:
Carrying out the directives delivered via the arguments takes place before setting \parindent to \listparindent and \parskip to \parsep.

Thus, if you wish to have the indenting "hardwired", you can—within the \list-macro's second argument—set \listparindent to 1.5em and \parsep to 0em.

If you wish the indenting to be that of the surrounding scope/to be that of the surrounding environment, you can within the \list-macro's second argument add directives for setting \listparindent to \parindent and \parsep to \parskip.
If you do so, these directives get carried out before setting \parindent to \listparindent and \parskip to \parsep and as a result the values of \parindent and \parskip while carrying out the \list-macro/the answer/otheranswer-environment will be the same as before carrying out the \list-macro/before carrying out the answer/otheranswer-environment.


In this example the \list-macro is (ab)used for achieving some indenting while not really introducing some labelled items.

Thus the value of the counter \@listdepth needs to be taken care of.
This counter will be increased globally by the list-macro and it will be decreased globally by the \list-macro.

The "\romannumeral-ization" of the value of that counter is part of the name of the macros \@listi, \@listii, \@listiii and \@listvi which store default-directives for the horizontal and vertical spacing for each nesting-level of \list-based environments like enumerate and itemize.

These macros are to be defined by the documentclass.

As there is not really a start of an enumeration or an itemization, the \@listdepth needs to be decreased by 1 after starting the answer/otheranser-environment.

As the \endlist-macro of \list-based environments like itemize/enumerate does decrease that counter by 1 globally, at the end of the answer/otheranswer-environment the value the \@listdepth needs to be restored to the value it had at the beginning of that environment.


A few remarks about horizontal spacing of text processed by \list-based environments:

The parameter \leftmargin denotes the increasing of the left margin of the text within the environment in relation to the left margin of text within the scope that surrounds that environment.

E.g., if the left margin with the surrounding scope is 2.3 cm and the \list-macro starts with the directive \leftmargin=0.7cm, then the text processed until reaching \endlist will have a left margin of 3cm.

\list-based-environments internally keep track of the left margin by means of the macro \@totalleftmargin.

So, imagine, a \list-macro by means of the \leftmargin-parameter, which denotes the increasing, has calculated the \@totalleftmargin. Further imagine a vertical line drawn parallel to the left border of the paper (or of the box wherein everything takes place) in a distance of \@totalleftmargin. (Well, that line is not there. So it is not real. There is not even a sensory impression that there might be a drawn line. Thus that line is fictional. Thus I will refer to that line as the "fictional vertical total-leftmargin-line".)

The text of an item will horizontally be placed to the right of that fictional vertical total-leftmargin-line.

The first line of the text of an item will be indented to the right of that fictional vertical total-leftmargin-line.
The indentation-width of that first line will be \itemindent.

First lines of subsequent paragraphs of the text of an item will also be indented to the right of that fictional vertical total-leftmargin-line. Here the indentation-width will be \listparindent.

There is also \rightmargin. As indenting usually goes from the left, this usually is left unchanged.

A few remarks about vertical spacing of text processed by \list-based environments:

Paragraphs within an item are separated by each other by a vertical gap of width \parsep.

The vertical gap between the last line of an item and the first line of the subsequent item will be \parsep+\itemsep`.

Both for the vertical gap between the last line of text preceding the \list-based environment and the first line of the \list-based environment, and for the vertical gap between the last line of text produced by the \list-based environment text and the line following that last line, you have two possibilities:

In case the \list-based environment is also the start of a new paragraph/is a paragraph on its own, that vertical gap will be \parskip+\partopsep+\topsep. (The \parskip-value from outside that environment before the beginning of the environment.)

In case the \list-based environment is not the start of a new paragraph, that vertical gap will be \topsep.

A few remarks about placement of labels belonging to items:

Labels of items will be placed as follows:

Horizontally: To the left of the fictional vertical total-leftmargin-line you will have a gap of width \labelsep. To the left of that gap a horizontal box will be placed which has the width \labelwidth. That horizontal box will contain the text of the label.

Vertically: Labels will be aligned with the first line of text belonging to the item in question.

\documentclass{exam}
\printanswers
\usepackage{amsthm,amssymb,amsmath}
\usepackage{etoolbox}
\usepackage{lipsum}

\newcommand{\HRule}[1][\medskipamount]{\par
  \vspace*{\dimexpr-\parskip-\baselineskip+#1}
  \noindent\rule{\linewidth}{0.2mm}\par
  \vspace*{\dimexpr-\parskip-.5\baselineskip+#1}}

\makeatletter

\newcommand\myProofOrSolution[2]{#1}%
\newcommand\myresetlistparameters{}%
\newcommand\myrestore@listdepth{}%

\newenvironment{answer}{\par
  \pushQED{\qed}%
  \normalfont
  \list{}{%
    \myProofOrSolution{%
      \edef\myresetlistparameters{%
        \noexpand\itemindent=\the\itemindent\relax
        \noexpand\labelwidth=\the\labelwidth\relax
        \noexpand\labelsep=\the\labelsep\relax
        \noexpand\listparindent=\the\listparindent\relax
      }%
      \edef\myrestore@listdepth{%
        \noexpand\@listdepth=\the\@listdepth\relax
      }%
      \settowidth\leftmargin{\textit{Proof.}}%
      \addtolength\leftmargin\labelsep
      \itemindent=-\leftmargin
      \labelwidth=0pt
      \listparindent=\parindent
      \parsep=\parskip
      \topsep=0pt
      \partopsep=0pt
      % In case you wish to change the vertical default-spacing
      % of enumerate and itemize when they occur within an
      % answer that occurs inside a proof,
      %,this is the place where to locally redefine the macros
      % \@listi, \@listii, \@listiii and \@listvi
      % which hold the spacing-directives for each
      % nesting-level of these environmenmts.
      %\def\@listi{...}%
      %\def\@listii{...}%
      %\def\@listiii{...}%
      %\def\@listvi{...}%
      % I don't do that in this example.
    }{%
      \edef\myresetlistparameters{%
        \noexpand\itemindent=\the\itemindent\relax
        \noexpand\labelwidth=\the\labelwidth\relax
        \noexpand\labelsep=\the\labelsep\relax
        \noexpand\listparindent=\the\listparindent\relax
      }%
      \edef\myrestore@listdepth{%
        \noexpand\@listdepth=\the\@listdepth\relax
      }%
      \vspace*{-\baselineskip}%
      \vspace*{-\parskip}%
      \itemindent=0pt
      \settowidth\leftmargin{{\Solution@Emphasis\solutiontitle}}%
      \labelwidth=\leftmargin
      \listparindent=\parindent
      \parsep=\parskip
      \topsep=0pt
      \partopsep=0pt
      % In case you wish to change the vertical default-spacing
      % of enumerate and itemize when they occur within an
      % answer that occurs inside a solution,
      %,this is the place where to locally redefine the macros
      % \@listi, \@listii, \@listiii and \@listvi
      % which hold the spacing-directives for each
      % nesting-level of these environmenmts.
      %\def\@listi{...}%
      %\def\@listii{...}%
      %\def\@listiii{...}%
      %\def\@listvi{...}%
      % I don't do that in this example.
    }%
  }%
  \myProofOrSolution{%
   \item
  }{%
    \item[\phantom{{\Solution@Emphasis\solutiontitle}}]%
  }%
  \begingroup
  \advance\@listdepth by -1\relax
  \myresetlistparameters
  \ignorespaces
}{%
  \endgroup
  \myrestore@listdepth
  \popQED\endlist
}

\newenvironment{otheranswer}{\par
  \pushQED{\qed}%
  \normalfont
  \list{}{%
    \myProofOrSolution{%
      \edef\myresetlistparameters{%
        \noexpand\itemindent=\the\itemindent\relax
        \noexpand\labelwidth=\the\labelwidth\relax
        \noexpand\labelsep=\the\labelsep\relax
        \noexpand\listparindent=\the\listparindent\relax
      }%
      \edef\myrestore@listdepth{%
        \noexpand\@listdepth=\the\@listdepth\relax
      }%
      \itemindent=0pt
      \leftmargin=0pt
      \labelwidth=0pt
      \listparindent=\parindent
      \parsep=\parskip
      \topsep=0pt
      \partopsep=0pt
      \itemsep=0pt
      % In case you wish to change the vertical default-spacing
      % of enumerate and itemize when they occur within an
      % otheranswer that occurs inside a proof,
      %,this is the place where to locally redefine the macros
      % \@listi, \@listii, \@listiii and \@listvi
      % which hold the spacing-directives for each
      % nesting-level of these environmenmts.
      %\def\@listi{...}%
      %\def\@listii{...}%
      %\def\@listiii{...}%
      %\def\@listvi{...}%
      % I don't do that in this example.
    }{%
      \edef\myresetlistparameters{%
        \noexpand\itemindent=\the\itemindent\relax
        \noexpand\labelwidth=\the\labelwidth\relax
        \noexpand\labelsep=\the\labelsep\relax
        \noexpand\listparindent=\the\listparindent\relax
      }%
      \edef\myrestore@listdepth{%
        \noexpand\@listdepth=\the\@listdepth\relax
      }%
      \vspace*{-\baselineskip}%
      \vspace*{-\parskip}%
      \itemindent=0pt
      \leftmargin=0pt
      \labelsep=0pt
      \labelwidth=0cm
      \listparindent=\parindent
      \parsep=\parskip
      \topsep=0pt
      \partopsep=0pt
      \itemsep=0pt
      % In case you wish to change the vertical default-spacing
      % of enumerate and itemize when they occur within an
      % otheranswer that occurs inside a solution,
      %,this is the place where to locally redefine the macros
      % \@listi, \@listii, \@listiii and \@listvi
      % which hold the spacing-directives for each
      % nesting-level of these environmenmts.
      %\def\@listi{...}%
      %\def\@listii{...}%
      %\def\@listiii{...}%
      %\def\@listvi{...}%
      % I don't do that in this example.
    }%
  }%
  \myProofOrSolution{%
   \item
  }{%
    \item[\phantom{{\Solution@Emphasis\solutiontitle}}]%
  }%
  \begingroup
  \advance\@listdepth by -1\relax
  \myresetlistparameters
  \ignorespaces
}{%
  \endgroup
  \myrestore@listdepth
  \popQED\endlist
}


\AtBeginEnvironment{solution}{\let\myProofOrSolution=\@secondoftwo}
\AtBeginEnvironment{solution}{\setlength{\parindent}{1.5em}}
\AtBeginEnvironment{solution}{\setlength{\parskip}{0em}}
\makeatother

\AtBeginEnvironment{answer}{\renewcommand\qedsymbol{$\blacklozenge$}}
\AfterEndEnvironment{answer}{\HRule}

\AtBeginEnvironment{otheranswer}{\renewcommand\qedsymbol{$\blacklozenge$}}
\AfterEndEnvironment{otheranswer}{\HRule}

\makeatletter
\newcommand*\NoIndentAfterEnv[1]{%
\AfterEndEnvironment{#1}{\par\@afterindentfalse\@afterheading}}
\makeatother
\NoIndentAfterEnv{answer}
\NoIndentAfterEnv{otheranswer}

\begin{document}

environment \verb|proof| and environment \verb|answer|:

\begin{proof}
\begin{answer}%
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution. 
Itemize inside answer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate inside answer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{answer}
Here is some text that belongs to the much longer solution.
Itemize outside answer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate outside answer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is some text that belongs to the much longer solution.
\end{proof}

\newpage

environment \verb|solution| and environment \verb|answer|:

\begin{solution}
\begin{answer}%
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution. 
Itemize inside answer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate inside answer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{answer}
Here is some text that belongs to the much longer solution.
Itemize outside answer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate outside answer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is some text that belongs to the much longer solution.
\end{solution}    

\newpage

environment \verb|proof| and environment \verb|otheranswer|:

\begin{proof}
\begin{otheranswer}%
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution. 
Itemize inside otheranswer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate inside otheranswer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{otheranswer}
Here is some text that belongs to the much longer solution.
Itemize outside otheranswer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate outside otheranswer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is some text that belongs to the much longer solution.
\end{proof}

\newpage

environment \verb|solution| and environment \verb|otheranswer|:

\begin{solution}
\begin{otheranswer}%
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution. 
Itemize inside otheranswer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate inside otheranswer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{otheranswer}
Here is some text that belongs to the much longer solution.
Itemize outside otheranswer:
\begin{itemize}
\item First Item.
\item Second Item.
\end{itemize}
Enumerate outside otheranswer:
\begin{enumerate}
\item First Item.
\item Second Item.
\end{enumerate}
Here is some text that belongs to the much longer solution.
\end{solution}  

\newpage

environment \verb|questions| and environment \verb|solution| and environment \verb|answer|:    

\begin{questions}

\question Here is a question.

\begin{solution}
\begin{answer}
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{answer}
This will be a fully written out solution.
\end{solution}

\question Here is another question.

\begin{parts}
    \part Part one.
\begin{solution}
\begin{answer}
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{answer}
Rest of solution.
\end{solution}
    \part Two.
\begin{solution}
\begin{answer}
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{answer}
Rest of solution.
\end{solution}
\end{parts}

\newpage

environment \verb|questions| and environment \verb|solution| and environment \verb|otheranswer|:

\question Here is a question.

\begin{solution}
\begin{otheranswer}
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{otheranswer}
This will be a fully written out solution.
\end{solution}

\newpage    

environment \verb|questions| and environmemt \verb|parts| and
environment \verb|solution| and environment \verb|otheranswer|:

\question Here is another question.

\begin{parts}
    \part Part one.
\begin{solution}
\begin{otheranswer}
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{otheranswer}
Rest of solution.
\end{solution}
    \part Two.
\begin{solution}
\begin{otheranswer}
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.
Here is the first paragraph of text for the answer before the solution.

Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.
Here is the next paragraph of text for the answer before the solution.    
\end{otheranswer}
Rest of solution.
\end{solution}
\end{parts}

\end{questions}

\end{document}

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here