如何定义命令\选择和 Ex 环境以编译所需的结果

如何定义命令\选择和 Ex 环境以编译所需的结果

我有一个包含以下内容的 LaTeX 文件

\documentclass[12pt,a4paper]{article}
\usepackage{environ}
\def\choice ....??? %Help me define the \ choice command and ex environment
\NewEnviron{ex}{...???} %Help me define the \ choice command and ex environment
\begin{document}

\choice{2}
\begin{ex}
an example
\end{ex}
\begin{ex}
A question developed from the example above
\end{ex}
\choice{3}
\begin{ex}
another example
\end{ex}
\begin{ex}
A question develops from this second example
\end{ex}
\begin{ex}
A question develops from this second example 
\end{ex}
\choice{5}
\begin{ex}
another example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}

\end{document}

定义命令 \ choice 和 ex 环境以便能够编译生成的 .PDF 文件,如下所示

exam 1: an example
Question 1.1: A question developed from the example above
exam 2: another example
Question 2.1: A question develops from this second example
Question 2.1: A question develops from this second example
exam 3: another example
Question 3.1: A question develops from this third example
Question 3.2: A question develops from this third example
Question 3.3: A question develops from this third example
Question 3.4: A question develops from this third example

答案1

如果我理解正确,那么\choice表示要将以下多少个ex环境组合成一个示例。
以下这些环境中的第一个ex将用于示例标题。
以下这些环境中的第二个和更多个ex将用作问题,这些问题的编号在示例标题的编号之内。

\documentclass[12pt,a4paper]{article}

\newtheorem{exthm}{exam}
\newtheorem{choicethm}{Question}[exthm]
\newcommand\WhichThm{}
\newcommand\choicerange{0 }%

\makeatletter
\newcommand\DoNotIgnoreChoice[1]{\@bsphack\xdef\choicerange{\number\numexpr#1-1\relax\space}\@esphack}
\newcommand\DoIgnoreChoice[1]{\@bsphack\@esphack}
\newcommand\choice{}%
\global\let\choice=\DoNotIgnoreChoice
\global\let\WhichThm=\@firstoftwo
\newcommand\nestingerror{}%
\newcommand\nestingerrormessage{%
    \GenericError{\space \space \space \@spaces \@spaces \@spaces }%
                 {Error:\space nested \@currenvir-environments}%
                 {Don't nest \@currenvir-environments.}%
                 {Nesting \@currenvir-environments is not a good idea.}%
}%
\newenvironment{ex}{%
  \nestingerror
  \let\nestingerror=\nestingerrormessage
  \WhichThm
  {%
    \global\let\choice=\DoIgnoreChoice
    \begin{exthm}%
  }%
  {\begin{choicethm}}%
}{%
  \WhichThm
  {%
     \end{exthm}%
     \global\let\WhichThm=\@secondoftwo
  }%
  {\end{choicethm}}%
  \ifnum\value{choicethm}<\choicerange
  \else
     \global\let\WhichThm=\@firstoftwo
     \global\let\choice=\DoNotIgnoreChoice
     \choice{1}%
  \fi
}%
\makeatother

\begin{document}

\choice{2}
\begin{ex}
an example
\end{ex}
\begin{ex}
A question developed from the example above
\end{ex}
\choice{3}
\begin{ex}
another example
\end{ex}
\begin{ex}
A question develops from this second example
\end{ex}
\begin{ex}
A question develops from this second example 
\end{ex}
\choice{5}
\begin{ex}
another example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}
\begin{ex}
A question develops from this third example
\end{ex}

\end{document}

在此处输入图片描述

\choice- 被视为属于先前发起的示例的问题的事物之间的命令将被忽略。

例如,

\choice{2}
\begin{ex}
an example
\end{ex}
\choice{6}
\begin{ex}
A question developed from the example above
\end{ex}

该命令\choice{6}将被忽略,因为在遇到该命令时,尚未处理ex由其启动的示例的所有环境。\choice{2}

相关内容