Remove line break before enumerate
\documentclass{article}
\usepackage{MinionPro}%optional
\usepackage{enumitem}
\makeatletter
\newcounter{exercises}
\let\ltx@endenumerate\endenumerate
\def\exercise{\futurelet\@next@item\@xercise}
\def\@xercise{%
\refstepcounter{exercises}%
\settowidth\@tempdima{\textbf{\theexercises.}}%
\@tempdimb5pt%
\description[leftmargin=\@tempdima,labelwidth=0pt,labelsep=0pt]
\item[\textbf{\theexercises.}]%
\ifx\@next@item\item
\let\endenumerate\ltx@endenumerate
\enumerate[labelsep=\@tempdimb,label=(\alph*)]
\else
\let\endenumerate\relax
\hskip\@tempdimb
\fi
}
\def\endexercise{\endenumerate\enddescription}
\makeatother
\begin{document}
\begin{exercise}%
\item If $A$ is a subset of $B$ and $B$ is a subset of $C$, prove that $A$ is a subset of $C$.
\item If $B\subset A$, prove that $A\cup B=A$, and conversely.
\item If $B\subset A$, prove that for any set $C$ both $B\cup C\subset A\cup C$ and $B\cap C\subset A\cap C$.
\end{exercise}
\begin{exercise}%
Prove that $A\cup (B\cap C)=(A\cup B)\cap (A\cup C)$.
\end{exercise}
\end{document}
Note that \item
in this definition of exercise
is an optional command (inside this environment). However, you might want to have an environment that disallows the use of \item
and a starred version that allows it:
\documentclass{article}
\usepackage{MinionPro}%optional
\usepackage{enumitem}
\makeatletter
\newcounter{exercises}
\def\exercise{%
\refstepcounter{exercises}%
\settowidth\@tempdima{\textbf{\theexercises.}}%
\description[leftmargin=\@tempdima,labelwidth=0pt,labelsep=0pt]
\item[\textbf{\theexercises.}]
\hspace\p@
}
\let\endexercise\enddescription
\newenvironment{exercise*}{%
\let\hspace\@gobble
\exercise\enumerate[labelsep=3.5pt,label=(\alph*)]}{\endenumerate}
\makeatother
\begin{document}
\begin{exercise*}
\item If $A$ is a subset of $B$ and $B$ is a subset of $C$, prove that $A$ is a subset of $C$.
\item If $B\subset A$, prove that $A\cup B=A$, and conversely.
\item If $B\subset A$, prove that for any set $C$ both $B\cup C\subset A\cup C$ and $B\cap C\subset A\cap C$.
\end{exercise*}
\begin{exercise}
Prove that $A\cup (B\cap C)=(A\cup B)\cap (A\cup C)$.
\end{exercise}
\end{document}
If you want to be fancy you can add a warning to the definition as follows -- however, apart from beeing fancy it might be helpful too (for debuging your manuscript).
\makeatletter
\newcounter{exercises}
\let\ltx@item\item
\def\exercise{%
\refstepcounter{exercises}%
\settowidth\@tempdima{\textbf{\theexercises.}}%
\description[leftmargin=\@tempdima,labelwidth=0pt,labelsep=0pt]
\item[\textbf{\theexercises.}]
\def\item{%
\@latex@warning{You can't use \noexpand\item inside exercise.
You might want to use the starred version of exercise.}}
\hspace\p@
}
\let\endexercise\enddescription
\newenvironment{exercise*}{%
\let\hspace\@gobble
\exercise
\let\item\ltx@item
\enumerate[labelsep=3.5pt,label=(\alph*)]}{\endenumerate}
\makeatother
If you wish to maintain your current input (nesting an enumerate
within an exercise
, as needed), then it's easiest to convert exercise
to a list, which by default sets nested \item
s on the same line:
\documentclass{article}
\usepackage{enumitem,etoolbox}
\newlist{exercise}{enumerate}{1}
\setlist[exercise]{label={\bfseries \arabic*.},resume}
\makeatletter
\patchcmd{\enit@enumerate@i}{\fi}{\fi\ifnum\pdfstrcmp{\@currenvir}{exercise}=0 \item\fi}{}{}
\makeatother
\begin{document}
\begin{exercise}
\begin{enumerate}[label=(\alph*)]
\item If~$A$ is a subset of~$B$ and~$B$ is a subset of~$C$, prove that~$A$ is a subset of~$C$.
\item If $B \subset A$, prove that $A \cup B = A$, and conversely.
\item If $B \subset A$, prove that for any set~$C$ both $B \cup C \subset A \cup C$ and $B \cap C \subset A \cap C$.
\end{enumerate}
\end{exercise}
\begin{exercise}
Prove that $A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$.
\end{exercise}
\end{document}
The patch adds an \item
at the start of the exercise
environment only.