6

Question update: Ulrich's answer (and its iterations) largely achieves the desired behavior:

  • An answer environment (ans is the answer environment in the new MWE provided below) where the end is indicated by the use of \blacklozenge (the ans environment is meant to mimic the proof environment).
  • The content from ans should be separated from the full-blown solution by a horizontal rule (the space above and below the rule should be the same). The idea is that, within the solution environment in the exam class, a student can see a very brief answer for verification without having to look at or scour the entire solution. This set up entails the ans environment being nested within the solution environment in exam.
  • The solution environment should behave normally with or without ans being used (some questions do not lend themselves to quick answers).

With the current answer, there seems to be an issue with how some environments are used within ans, namely itemize and enumerate (the labels for the items run into the text for the items). Is this an easy fix (in the MWE I show how I have tried to handle it)? Will there be other spacing issues when other environments are used? Any ideas for how to define the ans environment in a different way so as to bypass these issues but still mimic the desired behavior of proof?

enter image description here

MWE:

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

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

\newcommand\mymarginadjust[2]{#1}%

\makeatletter
\newenvironment{ans}{\par
  \pushQED{\qed}%
  \normalfont
  \list{}{%
    \mymarginadjust{%
      \settowidth\itemindent{\textit{Proof.}}%
      \addtolength\itemindent\labelsep
      \itemindent=0pt
      \leftmargin=0pt
      \labelwidth=0pt
      \listparindent=\parindent
      \parsep=\parskip
    }{%
      \vspace*{-\baselineskip}%
      \vspace*{-\parskip}%
      \itemindent=0pt
      \leftmargin=0pt
      \labelsep=0pt
      \labelwidth=0pt
      \listparindent=\parindent
      \parsep=\parskip
    }%
    \topsep=0pt
    \partopsep=0pt
  }%
  \mymarginadjust{%
   \item
  }{%
    \item[\phantom{{\Solution@Emphasis\solutiontitle}}]%
  }\ignorespaces
}{%
  \popQED\endlist\@endpefalse
}


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

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

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

\AtBeginEnvironment{solution}{\setlist[itemize,enumerate]{topsep=0pt,itemsep=0.5em,parsep=0pt,before=\vspace{0.5em},after=\vspace{0.5em}}}

\begin{document}

\begin{questions}

\question Question.

\begin{parts}
    \part One.
\begin{solution}
\begin{ans}
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.  
\begin{enumerate}
    \item One.
    \item Two.
    \item Three.
\end{enumerate}
The above is clearly problematic (the labels run into the text). Here is a sort of workaround that makes the list look better but still off and seemingly far from ideal:
\begin{enumerate}[leftmargin=*,labelindent=0.9em,labelsep=0.5em]
    \item One.
    \item Two.
    \item Three.
\end{enumerate}
Here is a sentence after a list within ans.
\end{ans}
Here is the first paragraph of text for the solution after the answer.
Here is the first paragraph of text for the solution after the answer.
Here is the first paragraph of text for the solution after the answer.
\begin{enumerate}
    \item One.
    \item Two.
    \item Three.
\end{enumerate}
Here is a paragraph of text after the list in the solution environment. 
Here is a paragraph of text after the list in the solution environment.
Here is a paragraph of text after the list in the solution environment.

Here is a paragraph of text after the list in the solution environment.
Here is a paragraph of text after the list in the solution environment.
Here is a paragraph of text after the list in the solution environment.
\end{solution}
\end{parts}

\question Question again.

\begin{solution}
Here is a solution, and it is clear that not using the ans environment is not an issue. 
\end{solution}
\end{questions}

\end{document}
  • Do you need your answer environment to be a trivlist? This is the source of your trouble I think: proof handles the new list gracefully, because it recognises a list as the first item in the trivlist environment, which is how proof is defined. But solution doesn't, because it isn't. – Paul Stanley Dec 18 '18 at 17:41
  • I don't really need it to be a trivlist I don't think. I just wanted something "basic" where I could write a snappy answer instead of a student having to read a full-blown solution when their question could be addressed quickly--I wanted to accomplish that while ensuring the black lozenge would always be aligned properly to indicate the end of a brief answer. – Daniel W. Farlow Dec 18 '18 at 23:33
  • What I was using before seemed far from ideal: \newcommand{\answer}[2][10pt]{#2\hfill$\bm{\blacklozenge}$\HRule[#1]} – Daniel W. Farlow Dec 18 '18 at 23:34
2
+50

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

  • Just a side remark (nothing that needs fixing as far as I am concerned, but I noticed this today): when using itemize or enumerate within otheranswer, the label is directly adjacent to the text for that item. Not sure why that is, but I simply get around it by using [labelsep=0.5em] as an option for itemize or enumerate using the enumitem package. This doesn't really matter to me, but I thought you might find the behavior...interesting. – Daniel W. Farlow Dec 20 '18 at 15:34
  • I realize I could just change \labelsep=0pt to \labelsep=0.5em when the definition of the otheranswer environment is made, but that seems unwise. I just thought the behavior was kind of odd (even using labelsep=0.5em at the beginning of itemize or enumerate does not seem best since the label is pushed out horizontally to the left), but it's not that important since I envision answers typically being very brief. Interesting nonetheless! – Daniel W. Farlow Dec 20 '18 at 15:42
  • Here is a picture of the output (the change you made has the net effect of what I referred to in my comments above). Here is the code with the screenshot beside it--you can see how enumerate in otheranswer results in things being slightly shifted to the left for whatever reason. – Daniel W. Farlow Dec 20 '18 at 22:29
  • @DanielW.Farlow I think the problem is solved now: I overlooked that answer/otheranswer will step the \@listdepth-counter. \@listdepth was larger by 1 within answer/otheranswer than outside. One of the effects was that enumerate/itemize chose the indenting for the wrong nesting-level of enumeration/itemization. Besides the fix I added another answer which seems to do what you need without \list/\trivlist at all. I think unlike the approach in this answer, the approach in my other answer will not only work with solution and proof but with other theorem-like envs as well. – Ulrich Diez Dec 22 '18 at 1:24
1

As you seem not to need whatsoever indentation with your answer-environment, I offer another solution which does without \list/\trivlist, just by starting new lines and inserting appropriate glue in terms of \vskip while \offinterlineskip/\nointerlineskip is in effect:

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

\makeatletter
\newskip\mytempskip
\newcommand{\answerrule}[1][\medskipamount]{%
  {\nopagebreak\mytempskip=#1\relax 
  \nopagebreak\parskip=0ex\relax
  \nopagebreak\par
  \nopagebreak\offinterlineskip
  % If the line "\nopagebreak\vskip-\prevdepth" is "uncommented", the vertical
  % gap between the previous line of text and the horizontal rule will be
  % measured from the baseline of the previous line.
  % If the line "\nopagebreak\vskip-\prevdepth" is "commented out", the vertical
  % gap between the previous line of text and the horizontal rule will be
  % measured from the "depth-line" of the previous line.
  %\nopagebreak\vskip-\prevdepth
  %%%%%
  \nopagebreak\vskip\mytempskip\relax
  \nopagebreak\hrule
  \nopagebreak\vskip\mytempskip\relax
  \nopagebreak\nointerlineskip
  \nopagebreak\noindent}%
}

\newcommand\UD@gobblesp{}\@firstofone{\def\UD@gobblesp} {}%
\newcommand\UD@ignoreparsandspace{%
  \kernel@ifnextchar\par
  {\expandafter\UD@ignoreparsandspace\@gobble}%
  {\expandafter\kernel@ifnextchar\expandafter{\@firstofone{ } }{\expandafter\UD@ignoreparsandspace\UD@gobblesp}{}}%
}%
\newcommand\UD@ignoreparsandspaceafterend{}%
\long\def\UD@ignoreparsandspaceafterend#1\ignorespaces\fi{#1\fi\UD@ignoreparsandspace}%

\newenvironment{answer}%
               {\normalfont\UD@ignoreparsandspace}%
               {\hfill\qed\answerrule\UD@ignoreparsandspaceafterend}%
\makeatother

\AtBeginEnvironment{solution}{\setlength{\parindent}{1.5em}}
\AtBeginEnvironment{solution}{\setlength{\parskip}{0em}}
\AtBeginEnvironment{answer}{\renewcommand\qedsymbol{$\blacklozenge$}}

\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}\par
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.
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|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 environmemt \verb|parts| and
environment \verb|solution| and environment \verb|answer|:

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

\end{questions}

\end{document}

enter image description here enter image description here enter image description here 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.