0

I wrote a small environment to write exercises:

\documentclass{book}
\usepackage{lipsum}
\usepackage{tocloft}
\usepackage{xparse}

\newcommand{\listofexercises}{List of exercises}
\newlistof{exercise}{exp}{\listofexercises}

% Restart counter at each new chapter
\makeatletter
\@addtoreset{exercise}{chapter}
\makeatother

% Exercise environment
\newenvironment{exercise}[1][]{%
    \refstepcounter{exercise}
    \par\noindent\textbf{Exercice \thechapter.\theexercise} {#1}
    \newline
    \addcontentsline{exp}{exercise}{%
        Exercice \thechapter.\theexercise
        \ifx&#1&\else\ -- #1\fi
    }\par
}{
    \newline%
}

\begin{document}

\mainmatter

\chapter{Foo}

\lipsum[1][1-3]

\begin{exercise}
What is the difference between a duck?
\end{exercise}

\lipsum[2][1-3]

\begin{exercise}
What is the answer to life the universe and everything?
\end{exercise}

\begin{exercise}[Don't answer yes]
Are you human?
\end{exercise}

\chapter{Bar}

\begin{exercise}[Impossible?]
If you enjoy wasting time, is that time really wasted?
\end{exercise}

\backmatter

\listofexercise

\end{document}

Sadly, the spacing doesn't help to clearly identify the exercise. The parindent are confusing and the spacing is not great.

enter image description here

Some workaround would set no parindent for the whole document and add a double \newline after the environment, but I don't think it is a good solution.

Moreover if I add a new line after an exercise I get an error. With this:

\begin{exercise}
What is the answer to life the universe and everything?

\end{exercise}

I get this:

! LaTeX Error: There's no line here to end.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.44 \end{exercise}
1
  • I think a better way is to make an example environment like that derives from a theorem environment using \newtheorem{example}{Example}[chapter] or similar. If you want you own list of these you can see some of the solutions here tex.stackexchange.com/q/74857/106804 – oliversm Aug 8 '20 at 0:16
1

I'm sure this can be done in a prettier way. My first idea was something like this.

Instead of just the par-command you could use \par\addvspace{\baselineskip}:

\documentclass{book}
\usepackage{lipsum}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{parskip}

\newcommand{\listofexercises}{List of exercises}
\newlistof{exercise}{exp}{\listofexercises}

% Restart counter at each new chapter
\makeatletter
\@addtoreset{exercise}{chapter}
\makeatother

% Exercise environment
\newenvironment{exercise}[1][]{%
    \refstepcounter{exercise}
    \par\addvspace{\baselineskip}\noindent\textbf{Exercice \thechapter.\theexercise} {#1}
    \par
    \addcontentsline{exp}{exercise}{%
        Exercice \thechapter.\theexercise
        \ifx&#1&\else\ -- #1\fi
    }\par
}{
    % \newline%
}

\begin{document}

\mainmatter

\chapter{Foo}

\lipsum[1][1-3]

\begin{exercise}
What is the difference between a duck?
\end{exercise}

\lipsum[2][1-3]

\begin{exercise}
What is the answer to life the universe and everything?
\end{exercise}

\begin{exercise}[Don't answer yes]
Are you human?
\end{exercise}

\chapter{Bar}

\begin{exercise}[Impossible?]
If you enjoy wasting time, is that time really wasted?
\end{exercise}

\backmatter

\listofexercise

\end{document}

baselineskip in exercise

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.