隐藏环境并稍后在文档中显示

隐藏环境并稍后在文档中显示

我正在尝试为学生建立一个制作练习并附有相应答案的环境。

主意

我希望能够在一个环境中编写练习文本,然后直接编写答案(最好在另一个环境中)。练习应显示在编写的位置,但答案应保持隐藏,直到\listofanswers发出命令(例如)。然后,此命令将在发出命令的位置输出每个问题的答案(就像插入例如\listoffigures或参考书目一样)。答案应以与它们提供答案的练习相同的编号出现。这样,我可以收集每个答案并仅在文档末尾显示它们,而不是在相应的练习之后立即显示它们。

当前状态

到目前为止,我已经能够做到以下几点:

\documentclass{scrartcl}

% Exercise environment:
\newcounter{exercise}
\newenvironment{ex}[1]{\refstepcounter{exercise}\par\medskip\noindent%
   \textbf{Exercise~\theexercise\ #1\quad} \rmfamily}{\medskip}

% Basic answer environment that should take care of formatting:
\newenvironment{ansenv}{\par\medskip\noindent%
   \textbf{Answer to exercise~\theexercise\quad} \rmfamily}{\medskip}

% Create a new answer environment that will store the answer for outputting later:
\usepackage{etoolbox}
\usepackage{environ}
\newcommand{\listofanswers}{}
\NewEnviron{answer}{
    \xappto{\listofanswers}{
        \begin{ansenv} \BODY \end{ansenv}
    }
}

\begin{document}

\section{Exercises}
\begin{ex}{Exercise title 1}
    This is the first exercise.
\end{ex}
\begin{answer}
    This is the answer to the first exercise, and should only show up when and where \verb!\listofanswers! is used.
\end{answer}

\begin{ex}{Exercise title 2}
    This is the second exercise.
\end{ex}
\begin{answer}
    This is the answer to the second exercise, and should only show up when and where \verb!\listofanswers! is used.
\end{answer}

This is something that is neither an exercise nor an answer.

\section{Answers}
% Output all answers right here at the very end of the document:
\listofanswers

\end{document}

预期结果

我希望看到类似下面的内容: 使用我的自定义练习和答案环境时的预期输出。

实际结果

编译出现以下错误:

! Argument of \@firstoftwo has an extra }.
<inserted text> 
                \par 
l.32 \end{answer}

有人能帮助我找出这个错误或者知道更好的方法来完成我的练习和回答环境吗?

答案1

这是一个使用的解决方案collect包来定义答案的集合;\listofanswers简单定义如下\includecollection{answers}

\documentclass{scrartcl}
\usepackage{collect}

\definecollection{answers}

\newcommand\mycollect[1]{%
  \collect{answers}
    {\par\medskip\noindent\textbf{Answer to Exercise~#1}}
    {\par}
    {}{}%
}

\newenvironment{answer}
  {\begingroup\edef\x{\endgroup\noexpand\mycollect{\theexercise}}\x}
  {\endcollect}

% Exercise environment:
\newcounter{exercise}
\newenvironment{ex}[1]  
  {\refstepcounter{exercise}%
    \gdef\@tempa{\theexercise}\par\medskip\noindent%
   \textbf{Exercise~\theexercise\ #1\quad}%
  }
  {\medskip}

\newcommand\listofanswers{\includecollection{answers}}

\begin{document}

\section{Exercises}

\begin{ex}{Exercise title 1}
This is the first exercise.
\end{ex}
\begin{answer}
This is the answer to the first exercise.
\end{answer}

\begin{ex}{Exercise title 2}
This is the second exercise.
\end{ex}
\begin{answer}
This is the answer to the second exercise.
\end{answer}

\begin{ex}{Exercise title 3}
This is the third exercise.
\end{ex}
\begin{answer}
This is the answer to the third exercise.
\end{answer}

\section{Answers}
\listofanswers

\end{document}

在此处输入图片描述

谢谢埃格尔谁帮我解决了柜台的问题his answer计数器的问题

答案2

tcolorbox可以作为 的替代collect。它提供了几个命令来存储答案以供日后使用。

  • \savelowerto=file定义哪个文件将用于存储特定盒子的下部。
  • \tcbstartrecording开始录制所有下半部分
  • \tcbstoprecording完成低音部分的录制
  • \tcbinputrecords加载录音并打印
  • record选项允许定义要写的内容

完整代码示例:

\documentclass{article}

\usepackage[most]{tcolorbox}

\tcbset{
    enhanced,
    blanker,
    fonttitle=\bfseries,
    coltitle=black,
}

\NewTColorBox[auto counter, number within=section]{exercise}{+O{}}{ %
title={Exercise~\thetcbcounter:},
label={exercise@\thetcbcounter},
attach title to upper=\ ,
lowerbox=ignored,
savelowerto=exercices/exercise-\thetcbcounter.tex,
record={\string\answer{\thetcbcounter}{exercices/exercise-\thetcbcounter.tex}},
#1
}

\NewTotalTColorBox{\answer}{mm}{ %
title={Answer to Exercise~\ref{exercise@#1} on page~\pageref{exercise@#1}:},
phantomlabel={answer@#1},
attach title to upper=\ ,
}{\input{#2}}

\begin{document}
\section{Some exercices}

\tcbstartrecording

\begin{exercise}
First exercise
\tcblower
Answer to first exercise.
\end{exercise}

\begin{exercise}
This is the second exercise.
\tcblower
Answer to second exercise.
\end{exercise}

\begin{exercise}
Third exercise. 
\tcblower
Answer to third exercise.
\end{exercise}

\tcbstoprecording

\section{Answers to previous exercices}

\tcbinputrecords

\end{document}

结果如下:

在此处输入图片描述

相关内容