[Tex/LaTex] An inline exercise environment

environmentsexercisesnumbering

In my lecture notes I want to leave some work as exercises and plan to give solutions later in the text. For example,

"The proof of this theorem is left as an exercise (Exercise 1.2)".

It must also have a label so that it could be referred later. I think I need an inline exercise environment.

This post answers my question. In the last answer in this post Werner has proposed that it could done in a better way may be with macros. I have too many of them so would be very difficult to do it with \lipsum \begin \end every single time. Can this be done with a single command?

Best Answer

Here is an implementation that provides a \exinline command, taking a label argument to produce the relevant text.

Sample output

\documentclass{article}

\usepackage{amsthm}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{exercise}[theorem]{Exercise}

\newcommand{\exinline}[1]{(\refstepcounter{theorem}Exercise~\thetheorem\label{#1})}

\begin{document}

\section{Euler's Theorem}

Some text.

\begin{theorem}[Euler]
  \label{thm:Euler}
  This is Euler's theorem.
\end{theorem}

\begin{proof}
  The proof of this theorem is left as an exercise \exinline{ex:Euler}.
\end{proof}

\begin{exercise}
  \label{ex:extra}
  Reformulate the theorem.
\end{exercise}

Here is the solution to Exercise~\ref{ex:Euler}.  By\dots

Note that Exercise~\ref{ex:extra} requires you to provide another formulation
of Theorem~\ref{thm:Euler}.

\end{document}

Note in this example Exercises and Theorems share the same counter. If that is not the case, you need to replace the theorem counter in definition with the appropriate counter, and change \thetheorem correspondingly. E.g. if exercises have their own number, declared via \newtheorem{exercise}{Exercise}[section], the command definition should read

\newcommand{\exinline}[1]{(\refstepcounter{exercise}Exercise~\theexercise\label{#1})}

If you are using cleveref for references then a small modification is needed to the refstepcounter command so \cref picks up the correct name.

\documentclass{article}

\usepackage{amsthm}
\usepackage[capitalize]{cleveref}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{exercise}[theorem]{Exercise}

\newcommand{\exinline}[1]{(\refstepcounter[exercise]{theorem}Exercise~\thetheorem\label{#1})}

\begin{document}

\section{Euler's Theorem}

Some text.

\begin{theorem}[Euler]
  \label{thm:Euler}
  This is Euler's theorem.
\end{theorem}

\begin{proof}
  The proof of this theorem is left as an exercise \exinline{ex:Euler}.
\end{proof}

\begin{exercise}
  \label{ex:extra}
  Reformulate the theorem.
\end{exercise}

Here is the solution to \cref{ex:Euler}.  By\dots

Note that \cref{ex:extra} requires you to provide another formulation
of \cref{thm:Euler}.

\end{document}

This produces the same output as the first example, with cleveref correctly providing the names.