3

In a book, I'd like to mark hard or difficult sections and exercises with superscript \star s. I can do this for sections as shown below using macros for \hard, \very hard inside \section{}.

I'm using a custom enumitem list environment for exercises, and for some reason I don't understand, \exercise\hard leaves a space between Exercise 1.2 and the output from $\^\star. Can someone suggest how to correct this?

Working example:

    \documentclass[12pt]{book}

    \usepackage{xspace}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Define new list type for exercises
    % from: http://tex.stackexchange.com/questions/196199/exercise-list-using-enumitem-how-control-indentation-and-labeling-of-sublists
    % by: Daniel Wunderlich
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    \usepackage{enumitem}      % this should be loaded in book.Rnw
    %
    \newlist{Exercises}{enumerate}{2}
    % set list style parameters
    \setlist[Exercises]{%
      label=\textbf{Exercise \thechapter.\arabic*}~,  % Label: Exercise Chapter.exercise
      ref=\thechapter.\arabic*, % References: Chapter.exercise (important!)
      align=left,               % Left align labels
      labelindent=0pt,          % No space betw. margin of list and label
      leftmargin=0pt,           % No space betw. margin of list and following lines
      itemindent=!,             % Indention of item computed automatically
      itemsep=3pt,
    }

    \newcommand{\exercise}{%
      \item\label{lab:\arabic{chapter}.\arabic{Exercisesi}}%      % Append label to item
      \setlist[enumerate, 1]{label=(\alph*),itemsep=0pt}          % Label for subexercises, but only within an exercise
    }
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Symbols for hard or difficult sections and problems
    \newcommand{\hard}{$^\star$\xspace}
    \newcommand{\veryhard}{$^{\star\star}$\xspace}

    \begin{document}

    \chapter{First chapter}
    \section{First section}
    asdfasfasfs
    \begin{Exercises}
        \exercise What is 2+2?
        \exercise\hard How many angels can fit on the head of a pin?
        \exercise\veryhard Prove the four-color theorem
    \end{Exercises}

    \section{A  hard section\hard}
    asfdAFASFAF

    \section{Very hard section\veryhard}
    asfdAFASFAF


    \end{document}
    % ----------------------------------------------------------------
azetina
  • 28,884
user101089
  • 1,227

1 Answers1

4

You are missing labelsep=0pt when setting options for the Exercises list, but, at this point, you will have to add a space when neither \hard nor \veryhard are issued.

MWE:

\documentclass[12pt]{book}

\usepackage{xspace}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define new list type for exercises
% from: http://tex.stackexchange.com/questions/196199/exercise-list-using-enumitem-how-control-indentation-and-labeling-of-sublists
% by: Daniel Wunderlich
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage{enumitem}      % this should be loaded in book.Rnw
%
\newlist{Exercises}{enumerate}{2}
% set list style parameters
\setlist[Exercises]{%
  label=\textbf{Exercise \thechapter.\arabic*},  % Label: Exercise Chapter.exercise
  ref=\thechapter.\arabic*, % References: Chapter.exercise (important!)
  align=left,               % Left align labels
  labelindent=0pt,          % No space betw. margin of list and label
  leftmargin=0pt,           % No space betw. margin of list and following lines
  itemindent=!,             % Indention of item computed automatically
  itemsep=3pt,
  labelsep=0pt,
}

\newcommand{\exercise}{%
  \item\label{lab:\arabic{chapter}.\arabic{Exercisesi}}%      % Append label to item
  \setlist[enumerate, 1]{label=(\alph*),itemsep=0pt}          % Label for subexercises, but only within an exercise
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Symbols for hard or difficult sections and problems
\newcommand{\hard}{$^\star$\xspace}
\newcommand{\veryhard}{$^{\star\star}$\xspace}

\begin{document}

\chapter{First chapter}
\section{First section}
asdfasfasfs
\begin{Exercises}
    \exercise\ What is 2+2?
    \exercise\hard How many angels can fit on the head of a pin?
    \exercise\veryhard Prove the four-color theorem
\end{Exercises}

\section{A  hard section\hard}
asfdAFASFAF

\section{Very hard section\veryhard}
asfdAFASFAF


\end{document}
% ---------------------------------------------------------------- 

Output

enter image description here

Another solution is to define two new commands

\newcommand{\exhard}{\hspace*{-\labelsep}\hard}
\newcommand{\exveryhard}{\hspace*{-\labelsep}\veryhard}

and use them instead of \hard and \veryhard after \exercise

MWE:

\documentclass[12pt]{book}

\usepackage{xspace}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define new list type for exercises
% from: http://tex.stackexchange.com/questions/196199/exercise-list-using-enumitem-how-control-indentation-and-labeling-of-sublists
% by: Daniel Wunderlich
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage{enumitem}      % this should be loaded in book.Rnw
%
\newlist{Exercises}{enumerate}{2}
% set list style parameters
\setlist[Exercises]{%
  label=\textbf{Exercise \thechapter.\arabic*},  % Label: Exercise Chapter.exercise
  ref=\thechapter.\arabic*, % References: Chapter.exercise (important!)
  align=left,               % Left align labels
  labelindent=0pt,          % No space betw. margin of list and label
  leftmargin=0pt,           % No space betw. margin of list and following lines
  itemindent=!,             % Indention of item computed automatically
  itemsep=3pt,
}

\newcommand{\exercise}{%
  \item\label{lab:\arabic{chapter}.\arabic{Exercisesi}}%      % Append label to item
  \setlist[enumerate, 1]{label=(\alph*),itemsep=0pt}          % Label for subexercises, but only within an exercise
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Symbols for hard or difficult sections and problems
\newcommand{\hard}{$^\star$\xspace}
\newcommand{\veryhard}{$^{\star\star}$\xspace}

\newcommand{\exhard}{\hspace*{-\labelsep}\hard}
\newcommand{\exveryhard}{\hspace*{-\labelsep}\veryhard}


\begin{document}

\chapter{First chapter}
\section{First section}
asdfasfasfs
\begin{Exercises}
    \exercise What is 2+2?
    \exercise\exhard How many angels can fit on the head of a pin?
    \exercise\exveryhard Prove the four-color theorem
\end{Exercises}

\section{A  hard section\hard}
asfdAFASFAF

\section{Very hard section\veryhard}
asfdAFASFAF


\end{document}
% ---------------------------------------------------------------- 

Output:

enter image description here


EDIT

If you want to improve the looking of exercises you can define the following commands

\newcommand{\simpleexercise}{\exercise\ \hphantom{\veryhard}}
\newcommand{\hardexercise}{\exercise\hard\hphantom{\hard}}
\newcommand{\veryhardexercise}{\exercise\veryhard\hphantom{\ }}

and use them instead of \exercise as in the following example

\documentclass[12pt]{book}

\usepackage{xspace}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define new list type for exercises
% from: http://tex.stackexchange.com/questions/196199/exercise-list-using-enumitem-how-control-indentation-and-labeling-of-sublists
% by: Daniel Wunderlich
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage{enumitem}      % this should be loaded in book.Rnw
%
\newlist{Exercises}{enumerate}{2}
% set list style parameters
\setlist[Exercises]{%
  label=\textbf{Exercise \thechapter.\arabic*},  % Label: Exercise Chapter.exercise
  ref=\thechapter.\arabic*, % References: Chapter.exercise (important!)
  align=left,               % Left align labels
  labelindent=0pt,          % No space betw. margin of list and label
  leftmargin=0pt,           % No space betw. margin of list and following lines
  itemindent=!,             % Indention of item computed automatically
  itemsep=3pt,
  labelsep=0pt,
}

\newcommand{\exercise}{%
  \item\label{lab:\arabic{chapter}.\arabic{Exercisesi}}%      % Append label to item
  \setlist[enumerate, 1]{label=(\alph*),itemsep=0pt}          % Label for subexercises, but only within an exercise
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Symbols for hard or difficult sections and problems
\newcommand{\hard}{$^\star$\xspace}
\newcommand{\veryhard}{$^{\star\star}$\xspace}

\newcommand{\simpleexercise}{\exercise\ \hphantom{\veryhard}}
\newcommand{\hardexercise}{\exercise\hard\hphantom{\hard}}
\newcommand{\veryhardexercise}{\exercise\veryhard\hphantom{\ }}


\begin{document}

\chapter{First chapter}
\section{First section}
asdfasfasfs
\begin{Exercises}
    \simpleexercise What is 2+2?
    \hardexercise How many angels can fit on the head of a pin?
    \veryhardexercise Prove the four-color theorem
\end{Exercises}

\section{A  hard section\hard}
asfdAFASFAF

\section{Very hard section\veryhard}
asfdAFASFAF


\end{document}
% ---------------------------------------------------------------- 

Output:

enter image description here

karlkoeller
  • 124,410