4

In order to make it easier to write hand-in assigments, I want to create two environments Subexc and subexc, where I can write the entire exercise down before starting. Subexc is supposed to be run with an argument, specifying the enumeration format to be used (note that I am using the enumerate package). Subexc then saves the format in a variable \subExcFormat and the exercise number in a counter subExcCounter. When subexc is run afterwards, it then continues the exercise number and the format from \Subexc. An example:

\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}

However, I get the errors

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.

What went wrong? :-)

1
  • Please make your code compilable, starting with \documentclass{...} and ending with \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to help you. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jub0bs
    Aug 22 '13 at 14:26
5

You can't use #1 in the end part. But you can simply move the definition of \subExcFormat in the begin part. Using \theenumi is wrong: \value{enumi} is what you need (it's the cause of the warning, by the way).

\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}

I removed also useless \ignorespaces commands, \begin{itshape} and \end{itshape}. I remain dubious about

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

that seems superfluous; just avoid a blank line if you don't want a new paragraph. Note also \par\vspace{1mm} rather than the inverse.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.