6

I'm having some trouble trying to change something about the enumerate environment. I want that the exercises number starts on the same line as the problem. Consider the follwing code.

\documentclass{article}
\usepackage{enumitem}

\newcounter{exercise}
\newenvironment{exercise}[1][]{\refstepcounter{exercise}
\noindent\textbf{\theexercise.#1 }}{}


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

Which produces:

rendered_mwe

I want something like this:

desired output

I don't want to remove the line break from all the enumerates in the document just for the ones that start as an exercise.

  • Welcome to TeX.SX! This question resp. the answer could be used as a staring point to your problem. – Ruben Jan 16 '17 at 14:12
  • ...forget about the link I posted above. – Ruben Jan 16 '17 at 14:25
10
\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}

example_rendered

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
2

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 \items on the same line:

enter image description here

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

  • HI, thanks for your answer, i have a question about the list you made, how can you make the counter reset after each section? – Zigisfredo Jan 20 '17 at 16:33
  • 1
    @Zigisfredo: Instead of just resume, use resume=persection-\thesection. – Werner Jan 20 '17 at 17:43

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.