考试类解决方案环境开始后出现不需要的换行符

考试类解决方案环境开始后出现不需要的换行符

问题更新:Ulrich 的回答(及其迭代)大部分实现所需的行为:

  • 答案环境(ans是下面提供的新 MWE 中的答案环境),其中结束通过使用\blacklozenge(该ans环境旨在模仿环境proof)来表示。
  • 应通过水平线将 中的内容ans与完整解决方案分开(水平线上方和下方的空间应相同)。这样做的目的是,在课堂solution环境中exam,学生可以看到非常简短的答案进行验证,而无需查看或浏览整个解决方案。这种设置需要将ans环境嵌套在 中solution的环境中exam
  • solution无论是否使用,环境都应该正常运行(ans有些问题无法快速回答)。

根据目前的答案,似乎存在一些环境在 中的使用方式问题ans,即itemizeenumerate(项目的标签会进入项目的文本中)。这是一个容易解决的问题吗(在 MWE 中,我展示了我如何尝试处理它)?使用其他环境时是否会出现其他间距问题?有没有关于如何ans以不同的方式定义环境以绕过这些问题但仍能模仿 的所需行为的想法proof

在此处输入图片描述

梅威瑟:

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

答案1

在您的评论中,您指出您希望\AtBeginEnvironment{solution}{...}在开始后续段落的第一行时将水平缩进宽度设置为 1.5em。

因此,我假设您不希望在后续段落的第一行之前有任何垂直间隙。


LaTeX 2ε 的注释源代码可以在这里找到http://mirrors.ctan.org/macros/latex/base/source2e.pdf

环境listtrivlist描述见下文文件 A ltlists.dtx、55 列表和相关环境

环境之下的宏list\list宏。

它需要两个未分隔的参数。(未分隔的参数要么是单个标记,要么是嵌套在花括号中的标记集合。)

该宏的第一个参数可以包含用于“格式化”项目标签的指令。

第二个参数可以包含其他命令/指令,例如调整水平和垂直位置的指令。


在环境中answerotheranswer我使用\list-macro,在\list-macro 的第二个参数中,我使用它\myProofOrSolution在两组不同的放置指令之间进行选择:在解决方案环境中\mymarginadjust设置为\@secondoftwo。否则设置为\@firstoftwo


根据部分55.4 水平间距(尺寸)\list,基于-macro的内容的段落水平缩进的宽度为,基于-macro\listparindent的内容的段落垂直缩进的高度为。\list\parsep

-macro 执行操作的时间顺序\list是:在设置到和到 之前
,执行通过参数传递的指令。\parindent\listparindent\parskip\parsep

因此,如果您希望缩进“硬连线”,您可以在\list-macro 的第二个参数中将其设置\listparindent为 1.5em 和\parsep0em。

如果您希望缩进与周围范围/周围环境相同,则可以在\list-macro 的第二个参数中添加用于设置\listparindent\parindent\parsep的指令\parskip
如果这样做,这些指令将在设置为 和 之前执行,\parindent因此\listparindent\parskip执行\parsep-macro // -环境时和的\parindent值将与执行 -macro// -环境之前相同。\parskip\listanswerotheranswer\listanswerotheranswer


在这个例子中,\list-macro 被(滥用)用于实现一些缩进,但并没有真正引入一些标记的项目。

\@listdepth因此需要注意计数器的值。
该计数器将通过 list-macro 全局增加,并通过 -macro 全局减少\list

该计数器值的“ -ization”是宏、和\romannumeral名称的一部分,它们存储了基于 的环境(如和 )的每个嵌套级别的水平和垂直间距的默认指令。\@listi\@listii\@listiii\@listvi\listenumerateitemize

这些宏由文档类定义。

由于实际上并没有开始枚举或逐项列举,因此在启动/环境\@listdepth后需要将其减少 1 。answerotheranser

由于基于环境\endlist的宏(如/)会将该计数器减少 1\listitemizeenumerate全球answer,在/环境结束时,otheranswer\@listdepth需要恢复到该环境开始时的值。


\list关于基于的环境处理的文本水平间距的几点说明:

此参数\leftmargin表示环境内文本左边距相对于该环境周围范围内文本左边距的增加。

例如,如果左边距和周围范围为 2.3 厘米,并且 -macro\list从指令 开始\leftmargin=0.7cm,则处理直到到达的文本\endlist将具有 3 厘米的左边距。

\list-based-environments 在内部通过宏跟踪左边距\@totalleftmargin

因此,想象一下,一个\list-macro 通过 -parameter \leftmargin(表示增加)计算出\@totalleftmargin。进一步想象一条垂直线,平行于纸张左边框(或所有事情发生的框),距离\@totalleftmargin。(嗯,那条线不存在。所以它不是真实的。甚至没有感觉上可能存在一条画线。因此那条线是虚构的。因此我将把那条线称为“虚构的垂直总左边距线”。)

项目的文本将水平放置在虚构的垂直总左边距线的右侧。

项目文本的第一行将缩进到虚构的垂直总左边距线的右侧。
第一行的缩进宽度为\itemindent

项目文本后续段落的第一行也将缩进到虚构的垂直总左边距线的右侧。此处缩进宽度为\listparindent

还有\rightmargin。由于缩进通常从左侧开始,因此这通常保持不变。

\list关于基于的环境处理的文本垂直间距的几点说明:

项目内的段落之间以宽度为 的垂直间隙分隔\parsep

项目的最后一行和后续项目的第一行之间的垂直间隙将为\parsep+\itemsep`。

对于基于 - 的环境之前的最后一行文本\list与基于 - 的环境的第一行之间的垂直间隙\list,以及基于 - 的环境文本生成的最后一行文本\list与最后一行之后的行之间的垂直间隙,有两种可能性:

如果\list基于 - 的环境也是新段落的开头/本身就是一个段落,则垂直间隙将为\parskip+\partopsep +\topsep。 (\parskip环境开始之前该环境外部的 - 值。)

如果\list基于 的环境不是新段落的开始,则垂直间隙将是\topsep

关于物品标签放置的几点说明:

物品标签放置如下:

水平方向:在虚构的垂直总左边距线的左侧,将有一个宽度为 的间隙\labelsep。在该间隙的左侧将放置一个宽度为 的水平框\labelwidth。该水平框将包含标签的文本。

垂直:标签将与相关项目的第一行文本对齐。

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

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

答案2

由于您似乎不需要在answer-environment 中使用任何缩进,因此我提供了另一种无需\list/的解决方案\trivlist,只需开始新行并在/生效期间插入适当的胶水即可\vskip\offinterlineskip\nointerlineskip

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

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容