练习枚举

练习枚举

为了更容易编写作业,我想创建两个环境Subexcsubexc,在开始之前我可以在这两个环境中写下整个练习。Subexc应该使用参数运行,指定要使用的枚举格式(请注意,我正在使用包enumerate)。Subexc然后将格式保存在变量中\subExcFormat,将练习编号保存在计数器中subExcCounter。当subexc随后运行时,它会继续练习编号和格式\Subexc。一个例子:

\usepackage{enumerate}
\newenvironment{Subexc}[1]
{%
    \begin{enumerate}[#1]%
        \item\ignorespaces\begin{itshape}%
}{%
    \end{itshape}%
    \end{enumerate}\setcounter{subExcCounter}{\theenumi}%
    \global\let\subExcFormat#1%
    \vspace{1 mm}\par\noindent\ignorespacesafterend%
}

\newenvironment{subexc}
{%
    \begin{enumerate}[\subExcFormat]%
    \setcounter{enumi}{\thesubExcCounter}%
    \ignorespaces\item\begin{itshape}%
}
{%
    \end{itshape}%
    \end{enumerate}%
    \setcounter{subExcCounter}{\theenumi}%
    \vspace{1 mm}\par\noindent\ignorespacesafterend%
}

\begin{document}
\section*{Exercise 717}
\emph{In this exercise, we will prove the Riemann hypothesis.} %Text from exercise.

\begin{Subexc}{(a)} % Enumeration format of the form (a), (b), ...
  Prove that $8 + 7 = 15$. % exercise text.
\end{Subexc}
Blablabla %My answer

\begin{subexc} % No format; it will continue the format from the last section.
  Show that $e^{i\pi} + 1 = 0$.
\end{subexc}
Bluhbluhbluh %My answer

\begin{subexc}
   Use (a) and (b) to show the Riemann hypothesis.
\end{subexc}
Wowowow %my answer
\end{document}

但是,我得到了错误

Illegal parameter number in definition of \endSubexc }
Illegal parameter number in definition of \subExcFormat \end{\subexc}
Undefined control sequence \begin{subexc}
Undefined control sequence \begin{subexc}
(Warning) The counter will not be printed.

什么地方出了错? :-)

答案1

您不能#1在结束部分使用。但您可以简单地将 的定义移到\subExcFormat开始部分。使用\theenumiis error:\value{enumi}正是您所需要的(顺便说一下,这是警告的原因)。

\documentclass{article}
\usepackage{enumerate}
\newcounter{subExcCounter}
\newenvironment{Subexc}[1]
  {%
   \gdef\subExcFormat{#1}%
   \enumerate[#1]%
   \item\itshape
  }
  {%
   \endenumerate\setcounter{subExcCounter}{\value{enumi}}%
   \par\vspace{1 mm}\noindent\ignorespacesafterend
  }

\newenvironment{subexc}
  {%
   \expandafter\enumerate\expandafter[\subExcFormat]%
   \setcounter{enumi}{\value{subExcCounter}}%
   \item\itshape
  }
  {%
   \endenumerate
   \setcounter{subExcCounter}{\value{enumi}}%
   \par\vspace{1 mm}\noindent\ignorespacesafterend
  }

\begin{document}
\section*{Exercise 717}
\emph{In this exercise, we will prove the Riemann hypothesis.} %Text from exercise.

\begin{Subexc}{(a)} % Enumeration format of the form (a), (b), ...
  Prove that $8 + 7 = 15$. % exercise text.
\end{Subexc}
Blablabla %My answer

\begin{subexc} % No format; it will continue the format from the last section.
  Show that $e^{i\pi} + 1 = 0$.
\end{subexc}
Bluhbluhbluh %My answer

\begin{subexc}
   Use (a) and (b) to show the Riemann hypothesis.
\end{subexc}
Wowowow %my answer
\end{document}

我还删除了无用的\ignorespaces命令,\begin{itshape}并且\end{itshape}。我仍然怀疑

   \par\vspace{1 mm}\noindent\ignorespacesafterend

这似乎是多余的;如果您不想有新段落,请避免使用空行。请注意,\par\vspace{1mm}而不是相反。

在此处输入图片描述

相关内容