1

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:
    1. By default, have no number.
    2. An optional argument in which I customize the exercise number to any number of interest.
    3. 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]).
    4. The equation numbers should reset at each new environment.
  • The Solution environment should have:
    1. By default, have no number.
    2. An optional argument in which I customize the solution number to any number of interest.
    3. 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 the Exercise environment.
    4. The equation numbers should reset at each new environment.

At the moment, I'm facing two issues:

  1. 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}{}

  1. 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}

Here's the output: MWE-Output

1
  • 2
    #1 (Exercise) is optional, #2 (number) is required. The protocol is \begin{Exercise}[optional]{1} Commented Sep 20, 2018 at 2:06

1 Answer 1

2

The main issue you had was pointed out in @JohnKormylo’s comment: You seemed to be confused about LaTeX’s optional argument syntax. According to your requirements, when you want an unnumbered exercise, you type something like

\begin{exercise} ... \end{exercise}

and when you want a numbered exercise, you type something like

\begin{exercise}[number] ... \end{exercise}

There are other issues. For instance, 1. changing \labelsep just for your use in the exercise/solution headings is a bad idea; 2. the <end environment stuff> to the \newenvironment command was missing; 3. the counter myanswercnt was never really used; etc.

I’m not entirely sure about the interpretations of your requirements 3 and 4 on solution. On the one hand, you want the numbering of the equations in solution to follow that in exercise. On the other hand, you want the numbering to be reset. I deduce from your requirement 3 that solution’s should go inside each exercise. So here is my attempt:

\documentclass[12pt]{article}
\usepackage{amsmath}

\newcounter{myproblemcnt}

\makeatletter
\@addtoreset{equation}{myproblemcnt}
\makeatother

\newenvironment{exercise}[1][]{%
  \setlength\parindent{0pt}%
  \stepcounter{myproblemcnt}%
  \if\relax\detokenize{#1}\relax
    {\large\textbf{Exercise}\par}%
  \else
    \setcounter{myproblemcnt}{#1}%
    \renewcommand{\theequation}{\arabic{myproblemcnt}.\arabic{equation}}%
    {\large\textbf{Exercise~#1}\par}%
  \fi
}{}

\newenvironment{solution}[1][]{%
  \setlength\parindent{0pt}%
  \if\relax\detokenize{#1}\relax
    \textbf{Solution}\par
  \else
    \textbf{Solution~#1}\par
  \fi
}{}

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

\begin{solution}[1]
    This is a first solution.
    \begin{align}
    i = 2 \\
    t = 3
    \end{align}
\end{solution}

\end{exercise}


\begin{exercise}[7]
    \textit{This is an exercise.}

\begin{solution}
    This is a first solution, but not numbered.
    \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{exercise}

\end{document}

exercises and solutions

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .