每个练习环境结束时答案的出现和消失

每个练习环境结束时答案的出现和消失

我设计了物理练习,并在每个练习的最后给出简短的数字答案,我希望这些答案放在右边,以保持问题的编号并且加粗。

重要的是,我希望能够在需要的时候将其隐藏(以备考试)。为此,我使用了上一个问题中非常有用的提示

不使用 % 和注释环境来注释行

我得到了代码。

\newif\ifprolog
\long\def\startprolog#1\stopprolog{%
\ifprolog
\par
\begingroup
\let\\\par
\color{red}\small #1
\par\medskip
\endgroup
\fi}
\prologtrue

我尝试通过选择出现和消失的命令来创建每个练习的答案\answerstrue

\documentclass[b5paper,10pt]{book} 
\usepackage[inline]{enumitem}
\newtheorem{exercise}{}[chapter]%[chapter] %{Άσκηση}%[numberby]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%
\newif\ifanswers
\long\def\startanswers #1 \stopanswers{%
\ifanswers
\par
\begingroup
\let \\ \par
\begin{flushright}
[\textbf{\small Answers: \, #1}]
\end{flushright}
\par\medskip
\endgroup
\fi}
\answerstrue    % is to show or not the answers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\chapter{physics}

\begin{exercise} 
here is the exercise ...
Αnswer the questions:
\begin{enumerate}
  \item quation 1
  \item quation 2
  \item quation 3
  \item quation 4
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%%
\startanswers
 \begin{enumerate*}
  \item % here there is no numerical answer because the question was theoretical
  \item $\sqrt{2}$
  \item $5/3$
  \item $\frac{1}{2}$
 \end{enumerate*}
\stopanswers
 %%%%%%%%%%%%%%%%%%%%%
\end{exercise}
\end{document}

在此处输入图片描述

问题是当我之后没有导入某些东西时 \item(因为这个问题是理论上的)

\startanswers
 \begin{enumerate*}
  \item % here there is no numerical answer because the question was theoretical

运行 .tex 文件时出现错误。

在此处输入图片描述


我的问题: 我该如何消除错误,或者你是否有更实际的想法从可打印文件中获取答案


在此处输入链接描述

答案1

当你使用包时枚举项start= ,您可以使用和resume/键resume*

\documentclass[b5paper,10pt]{book} 
\usepackage[inline]{enumitem}
\newtheorem{exercise}{}[chapter]%[chapter] %{Άσκηση}%[numberby]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%
\newif\ifanswers
\long\def\startanswers #1 \stopanswers{%
\ifanswers
\par
\begingroup
\let \\ \par
\begin{flushright}
[\textbf{\small Answers: \, #1}]
\end{flushright}
\par\medskip
\endgroup
\fi}
\answerstrue    % is to show or not the answers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\chapter{physics}

\begin{exercise} 
here is the exercise ...
Answer the questions:
\begin{enumerate}
  \item theoretical question 1
  \item question 2
  \item question 3
  \item question 4
  \item question 5
  \item theoretical question 6
  \item question 7
  \item question 8
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%%
\startanswers
 \begin{enumerate*}[start=2]%
  \item $\sqrt{2}$
  \item $\frac{5}{3}$
  \item $\frac{1}{2}$
  \item $(3,4,5)$
 \end{enumerate*}
 \begin{enumerate*}[start=7, resume*]%
  \item $\left(\frac{a+b}{2}\right)^2-\left(\frac{a-b}{2}\right)^2$
  \item $\frac{n^2+n}{2}$
 \end{enumerate*}
\stopanswers
 %%%%%%%%%%%%%%%%%%%%%
\end{exercise}
\end{document}

顺便一提:

您可以使用评论包将环境转换为评论:

\documentclass[b5paper,10pt]{book} 
\usepackage[inline]{enumitem}
\newtheorem{exercise}{}[chapter]%[chapter] %{Άσκηση}%[numberby]
\usepackage{comment}

\specialcomment{answers}{%
  \begingroup
  \let\\=\par
  \flushright
  [\bfseries\small\selectfont Answers: \, \ignorespaces
}{\mdseries\selectfont]\par\medskip\endgroup\ignorespacesafterend}%
%
% Toggle answers on/off via commenting/uncommenting
% the following line:
%\excludecomment{answers}

\begin{document}
\chapter{physics}

\begin{exercise} 
here is the exercise ...
Answer the questions:
\begin{enumerate}
  \item theoretical question 1
  \item question 2
  \item question 3
  \item question 4
  \item question 5
  \item theoretical question 6
  \item question 7
  \item question 8
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%%
\begin{answers}
  \begin{enumerate*}[start=2]%
    \item $\sqrt{2}$
    \item $\frac{5}{3}$
    \item $\frac{1}{2}$
    \item $(3,4,5)$
  \end{enumerate*}
  \begin{enumerate*}[start=7, resume*]%
    \item $\left(\frac{a+b}{2}\right)^2-\left(\frac{a-b}{2}\right)^2$
    \item $\frac{n^2+n}{2}$
  \end{enumerate*}
\end{answers}
%%%%%%%%%%%%%%%%%%%%%
\end{exercise}
\end{document}

相关内容