I'm attempting to create two new environments, one for writing exercises and another for its solutions, with the following characteristics:
- The
Exercise
environment should have:- By default, have no number.
- An optional argument in which I customize the exercise number to any number of interest.
- All equations listed in this environment should be numbered in the format:
([exercise number].[equation number])
. If the exercise is unnumbered, then the format should be the usual:([equation number])
. - The equation numbers should reset at each new environment.
- The
Solution
environment should have:- By default, have no number.
- An optional argument in which I customize the solution number to any number of interest.
- All equations listed in this environment should be numbered in the format:
([exercise number].[equation number])
. The numbering should also follow up with the numbering from the equations listed in theExercise
environment. - The equation numbers should reset at each new environment.
At the moment, I'm facing two issues:
- If I leave both environments unnumbered, then I get the following errors:
Missing number, treated as zero. \begin{exercise}{}
Missing number, treated as zero. \begin{solution}{}
- If I leave the exercise environment unnumbered, the equations are numbered:
(0.[equation number])
.
Here's a MWE:
\documentclass[12pt]{article}
\usepackage{amsmath}
\setlength{\labelsep}{0.3em}
\newenvironment{exercise}[2][Exercise]
{\stepcounter{myproblemcnt}\setcounter{myproblemcnt}
{#2}\noindent\hskip\labelsep{\large\bfseries
#1}\hspace\labelsep{\large\bfseries#2}\par\noindent}
\makeatletter
\renewcommand{\theequation}{\arabic{myproblemcnt}.\arabic{equation}}
\newcounter{myproblemcnt}
\@addtoreset{equation}{myproblemcnt}
\makeatother
\newcounter{myanswercnt}
\newenvironment{solution}[2][Solution]
{\stepcounter{myanswercnt}\setcounter{myanswercnt}
{#2}\noindent\hskip\labelsep{\bfseries
#1}\hskip\labelsep{\bfseries#2}\par\noindent}
\begin{document}
\begin{exercise}{1}{}
\textit{This is an exercise.}
\begin{align}
i = 2
\end{align}
\end{exercise}
\begin{exercise}{}{}
\textit{This is an exercise.}
\begin{align}
i = 2
\end{align}
\end{exercise}
\begin{solution}{1}{}
This is a first solution.
\begin{align}
i = 2 \\
t = 3
\end{align}
\end{solution}
\begin{exercise}{7}{}
\textit{This is an exercise.}
\end{exercise}
\begin{solution}{1}{}
This is a first solution.
\begin{align}
i = 2 \\
t = 3
\end{align}
\end{solution}
\begin{solution}{2}{}
This is a second solution.
\begin{align}
i = 2 \\
t = 3
\end{align}
\end{solution}
\end{document}
\begin{Exercise}[optional]{1}