試験クラスのソリューション環境にフックして、回答をドキュメントの最後に配置する方法

試験クラスのソリューション環境にフックして、回答をドキュメントの最後に配置する方法

私は、マーティン・シャラーが質問に対する回答で行ったように、文書の最後に答えを置きたい。tex core - 質問の解答を「個別に」表示する - TeX - LaTeX Stack Exchange、私は今、「真偽」タイプの質問の回答と複数選択質問の回答を入れることができますが、試験クラスのソリューション環境でラップされた回答を入れる方法がありません。

これが私のコードです

%\documentclass[answers,12pt]{exam}
\documentclass[12pt]{exam}
\usepackage{environ}
\usepackage[normalem]{ulem}
\usepackage{etoolbox}

\usepackage{amssymb}
\newcommand\False{$\times$}
\newcommand\True{$\checkmark$}


\newlength\answerspace
\setlength\answerspace{0.5in}
\newcommand\dottedanswerline[1][{}]{%
  % One optional argument, the default value of which is empty.
  \unskip\linebreak[0]\enspace
  \hbox{}\nobreak\dotfill
  \ifprintanswers %whether to show the answers or leave it as a blank
    \hbox to \answerspace{(\hfil#1\hfil)}%
   % \xappto\answertext{\expandonce{\thequestion\theanswer}}
   %\xappto\answertext{\thequestion,#1\space}
  \else
    \hbox{(\hskip 0.5in)}%
    \xappto\answertext{\thequestion,#1\space}
  \fi
}% dottedanswerline


\newcommand{\answertext}{}
\usepackage{etoolbox}
\NewEnviron{answer}{\xappto\answertext{\expandonce{\BODY}}}%


\makeatletter

\AtEndEnvironment{solution}{\xappto\answertext{\BODY\space}}
%\AtEndEnvironment{solution}{\xappto\answertext{\thequestion,\BODY\space}}

\newlength\choiceitemwidth
\newif\ifshowsolution \showsolutiontrue
\newcounter{choiceitem}%选项的序号计数单元

\def\thechoiceitem{\Alph{choiceitem}}%用ABCD来显示选项序号

\def\makechoicelabel#1{#1\uline{\thechoiceitem.}\else\thechoiceitem.\fi\space} %答案显示下划线+字母或者字母,用于最终显示

\def\choice@mesureitem#1{\cr\stepcounter{choiceitem}\makechoicelabel#1}%计数器加1,同时显示ABCD标签,是用于测量长度的

%这个是用来进行测量用的,第一遍\BODY的parse
\def\choicemesureitem{\@ifstar
  {\choice@mesureitem\ifprintanswers \xappto\theanswer{\thechoiceitem}}%
  {\choice@mesureitem\iffalse}}%

\def\choice@blockitem#1{%
  \ifnum\value{choiceitem}>0\hfill\fi
  \egroup\hskip0pt
  \hbox to \choiceitemwidth\bgroup\hss\refstepcounter{choiceitem}\makechoicelabel#1}

\def\choiceblockitem{\@ifstar
  {\choice@blockitem\ifprintanswers}%
  {\choice@blockitem\iffalse}}

\def\choice@paraitem#1{%
  \par\refstepcounter{choiceitem}\makechoicelabel#1}

\def\choiceparaitem{\@ifstar
  {\choice@paraitem\ifprintanswers}%
  {\choice@paraitem\iffalse}}


\NewEnviron{mychoices}{%
  \def\theanswer{}
  \begingroup
    \let\item\choicemesureitem
    \setcounter{choiceitem}{0}%
    \settowidth{\global\choiceitemwidth}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}%
  \endgroup 
  \dottedanswerline[\theanswer]
  \trivlist\item\relax
  \parindent0pt
  \setcounter{choiceitem}{0}%
  \ifdim\choiceitemwidth<0.24\linewidth
    \choiceitemwidth=0.25\linewidth
    \let\item\choiceblockitem
    \bgroup\BODY\hfill\egroup
  \else\ifdim\choiceitemwidth<0.48\linewidth
    \choiceitemwidth=0.5\linewidth
    \let\item\choiceblockitem
    \bgroup\BODY\hfill\egroup
  \else % \choiceitemwidth > 0.5\linewidth
    \let\item\choiceparaitem
    \BODY
  \fi\fi
  \endtrivlist

}
\makeatother



\begin{document}
\section{True OR False questions}
\begin{questions}
\question text text text text text text text text text text text text.\dottedanswerline[\True]

\question text text text text text text text text text text text text.\dottedanswerline[\True]

\question text text text text text text text text text text text text.\dottedanswerline[\False]
\end{questions}


\section{Multiply choices questions}
\begin{questions}
  \question Why is there air?
  \begin{mychoices}
    \item To blow up volleyballs.
    \item*  To fill an unneeded void.
  \end{mychoices}

  \question text text text text text text text text text text text text text text text text text text text text
  \begin{mychoices}
        \item foo
        \item bar
        \item*baz
  \end{mychoices}

\end{questions}

\section{Other questions}
\begin{questions}
\question[5] text text text text text text text text text text text text text text text text?
\begin{solution}[2cm]
1, text answer1
2, text answer2
\end{solution}

\question text text text text text text text text text text text text text text text text?
\begin{parts}
\part[2]
part one?
\part[2]
part two?
\end{parts}
\begin{solution}[2cm]
1, answer to part1
2, answer to part2
\end{solution}
\end{questions}
\section{Answers}
% \newpage
% Some time later
\answertext

\end{document} 

そして、これがスクリーンショットです。私は、ドキュメントの最後に回答(青色)を配置したいだけです。これらの回答は、試験クラスによって定義されたソリューション環境に配置されるため、コマンドを使用して\AtEndEnvironment{solution}{\xappto\answertext{\thequestion,\BODY\space}}ソリューション環境の本体をグローバル変数に追加しようとしましたが、確かに\BODY定義されていません。

もう 1 つの改善点は、現在画像に示されているように同じ行ではなく、異なる種類の回答を異なる段落に配置する必要があることです。

回答は文書の最後に記載する

関連情報