% \input{exercise.sty}
%
% environments for making examples, exercises, solutions to exercises,
% continuations of examples.
% solutions to examples? 
%
% the following options:
%  with rat / without  (global)                            see ratfull
%    or with pointer to where the solution is
%  indented / not indented                             choose exercis or Exercis
%  exercises numbered with pages when refered to       see \exerciseref
%
% where is the indentation of a description defined? 
%
% usage: 
% \exercisa{ Text of exercise }  
%  (obsolete, widely used in 97)
%  makes an indented exercise which doesn't get labelled.
%  recommended alternative:
%
% \exercis{label}{ Text of exercise }
%  makes an indented exercise with a label.
% \Exercis{label}{ Text of exercise }
%  makes an NON-indented exercise with a label.
% \exercinr{label}{ Text of exercise }
%  same, no rat
% \exerciss{label}{ Text of exercise }
%  same, with margin giving pointer to solns (the pointer can be an arrow or rat)
% \exercissx{A1}{label}{ Text of exercise }
%  same, with a rating (eg A1) appended to the number. B2. 
%                            suggestion: 1,2,3 for difficulty. 1 easy. A,B,C for importance. A=essential.
% \exercisx{A1}{label}{ Text of exercise }
%  same, with a rating (eg A1) appended to the number. but no solution pointer.
%
% how to refer to an exercise: 
% \exerciseref{#1}
%
% to make a global change use 
% perl -pi -e 's/\\exercis\{/\\exerciss\{/' tex/_l1.tex
%
% \solutiona{ Text }
%  OBSOLETE -- makes use of the solution_number counter
% (  Now useful  for making unnumbered solution ) 
%  recommended alternative
% \soln{ref}{ Text }
% \solution.
%  OBSOLETE -- starts a non-indented solution.
%  recommended alternative
% \Soln{ref}{ Text } 
%
% \exampl{label}{ Text }
%  an indented example
% \cexampl{ref}{ Text }
%  continuation, indented, of example
% \Exampl{label}{ Text }
%  a non-indented example
% \Cexampl{ref}{ Text }
%  continuation, non-indented, of example
% \exampla{text}
%  for use where you don't want to bother with a ref
%
% \exerciseref{ref} makes ``exercise 32 (p. 10)''
% \exerciserefrange{refa}{refb} makes ``exercises 32-36 (pp. 10-12)''
% \exerciserefshort{ref} makes ``32 (p. 10)''
% \exerciserefrangeshort{refa}{refb} makes ``32-36 (pp. 10-12)''
%
% The above commands all work by invoking the following environments
%
% \begin{examplee} blah blah \end{examplee}     equivalent to \exampl{}{}
% \begin{Examplee} blah blah \end{Examplee}     equivalent to \Exampl{}{}
% The other environments are {sexercise} Sexercise
% exercisee Exercisee exercisenr exercises Exercises
% cexamplee Cexamplee
%
% NB, the indented environments don't work perfectly because the 
% \label{} does not get the right number. To fix this use the 
% \exercisehack command immediately before the label, or just use
% the convenient forms.
% A correct solution to this would replace the {description}\item[...]
% by a new trivlist, I suppose.

% silly auxiliary things
% width was 0.4in jan 2000.
%\newcommand{\dorat}{\raisebox{-0.3in}[0in][0in]{\psfig{figure=figs/myrat1b.eps,width=0.4in}\hspace{0.5mm}}}% was ratbert4 % was raise  -0.45in . width 1.37cm
\newcommand{\dorat}{\raisebox{-0.2in}[0in][0in]{\psfig{figure=figs/myrat1b.eps,width=0.3in}\hspace{0.5mm}}}% was ratbert4 % was raise  -0.45in . width 1.37cm
%
% a ratA is a rat with a lightning bolt or similar indication of
% imperativeness of doing the question.
%
\newcommand{\doratA}{\raisebox{-0.3in}[0in][0in]{\psfig{figure=figs/myrat1bA.eps,width=0.3in,silent=}\hspace{0.5mm}}}% was ratbert4 % was raise  -0.45in . width 1.37cm
\newcommand{\ratfull}{\makebox[0in][r]{\dorat}}% change to {} to get no rats
\newcommand{\trianglefull}{\makebox[0in][r]{{\ensuremath{\triangleright}}\hspace{1.5mm}}}
%
% The counters and their invokers -- \exercise just spits out `exercise 1'
%   with no punctuation
\newcounter{exercisenumber}[chapter]
\def\theexercisenumber {\thechapter.\arabic{exercisenumber}}
\newcommand{\exercise}{\refstepcounter{exercisenumber}%
{\ratfull\sf{Exercise}~\theexercisenumber}}
\newcommand{\exercisetriangle}{\refstepcounter{exercisenumber}%
{\trianglefull\sf{Exercise}~\theexercisenumber}}
\newcommand{\exercisenorat}{\refstepcounter{exercisenumber}%
{\sf{Exercise}~\theexercisenumber}}
\newcommand{\exercisesol}[1]{\refstepcounter{exercisenumber}%
{\solref{#1}\sf{Exercise}~\theexercisenumber}}
\newcommand{\solref}[1]{%
\makebox[0in][r]{\raisebox{0in}[0in][0in]{\parbox{0in}%
%{\small\sf{\makebox[0in][r]{Soln.\ on}\\%
{\tiny\sf{\makebox[0in][r]{Solutions}\\%
\makebox[0in][r]{\mbox{on \pdotsmall}\pageref{#1}}%
}}\dorat}}}
%}}$\rightarrow$}}}
%
\newcounter{solution_number} % an obsolete object, useful for hacks but not for the book.
\setcounter{solution_number}{1}
% this is for solutions to examples where the solution is so close
% that a reference is ugly
\newcommand{\solution}{{\sf{Solution\colonspace}}}
% was:
%\newcommand{\solution}{{\sf{Solution to exercise}
% \arabic{solution_number}}\addtocounter{solution_number}{1}}
%
% separated numbering system:::::::::::::
\newcounter{examplenumber}[chapter]%%%%%%%%%%%%%%%% not used!
%\def\theexamplenumber {\thechapter.\arabic{examplenumber}}
%\newcommand{\example}{\refstepcounter{examplenumber}%
%{\sf{Example}~\theexamplenumber}}
%\newcommand{\examplehack}{%
%\addtocounter{examplenumber}{-1}\refstepcounter{examplenumber}}
% joint numbering system
\def\theexamplenumber {\thechapter.\arabic{exercisenumber}}
\newcommand{\example}{\refstepcounter{exercisenumber}%
{\sf{Example}~\theexercisenumber}}
\newcommand{\examplehack}{% make examples share the exercise numbers
\addtocounter{exercisenumber}{-1}\refstepcounter{exercisenumber}}
%
\newcommand{\exercisehack}{%
\addtocounter{exercisenumber}{-1}\refstepcounter{exercisenumber}}
%
% obsolete counters
\newcounter{exercise_number}[section]
\newcounter{example_number}[section]
%

%
%   			examples
%
%                 example environment , NOT indented
\newenvironment{Examplee}% 
{\begin{trivlist}\item[]\example\colonspace }% 
{\end{trivlist}}
%		 continuation of example , NOT indented
\newenvironment{Cexamplee}[1]%
{\begin{trivlist}\item[]%
{\sf Example~\ref{#1} (continued)\colonspace }}%
{\end{trivlist}}
%		 example environment , indented
\newenvironment{examplee}% 
{\begin{description}\item[\example\colonspace] %
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
%		 continuation of example , indented
\newenvironment{cexamplee}[1]%
{\begin{description}\item[{\sf{Example~\ref{#1} (continued)\colonspace}}] %
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
%
%			things like proofs
%
%		 proof  , NOT indented
\newenvironment{Prooflike}[1]%
{\begin{trivlist}\item[] {\sf{#1. }}}% was \colonspace
{\hfill$\Box$\end{trivlist}}
%		 proof  ,  indented
\newenvironment{prooflike}[1]%
{\begin{description}%
\item[{\sf #1. }] %% make depth one greater, since we are indenting % was \colonspace
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\hfill$\epfsymbol$\end{description}}% was \diamond
%
%			exercises and solutions
%
%		 exercise  , NOT indented
\newenvironment{Exercisee}%
{\begin{trivlist}\item[]\exercise\colonspace }%
{\end{trivlist}}
\newcommand{\difficulty}[1]{\raisebox{5pt}{\footnotesize{[{\em{#1}\/}]}}}% was $^{\mbox{[#1]}}$
% plain difficulty
\newcommand{\pdifficulty}[1]{\small{[{\em{#1}\/}]}}
\newenvironment{Exerciseex}[1]%
{\begin{trivlist}\item[]\exercise\nocolon\difficulty{#1}\smallcolonspace }% removed colons from all exercises with diff
{\end{trivlist}}

% A is identical to the above. B and C have different markers

\newenvironment{ExerciseexA}[1]%
{\begin{trivlist}\item[]\exercise\nocolon\difficulty{#1}\smallcolonspace }%
{\end{trivlist}}
\newenvironment{ExerciseexB}[1]%
{\begin{trivlist}\item[]\exercisetriangle\nocolon\difficulty{#1}\smallcolonspace }%
{\end{trivlist}}
\newenvironment{ExerciseexC}[1]%
{\begin{trivlist}\item[]\exercisenorat\nocolon\difficulty{#1}\smallcolonspace }%
{\end{trivlist}}

%		 exercise  ,  indented
\newenvironment{exercisee}%
{\begin{description}%
\item[\exercise\colonspace] %% make depth one greater, since we are indenting
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}

% same but with rating
\newenvironment{exerciseex}[1]%
{\begin{description}%
\item[\exercise\nocolon\difficulty{#1}\smallcolonspace] %% make depth one greater, since we are indenting
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
%		 exercise  ,  indented , no rat
\newenvironment{exercisenr}%
{\begin{description}%
\item[\exercisenorat\colonspace] 
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
%		 exercise  ,  indented , B-level encouragement indicated
\newenvironment{exerciseexB}[1]%
{\begin{description}%
\item[\exercisetriangle\nocolon\difficulty{#1}\smallcolonspace] 
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
%		 exercise  ,  indented , C-level, so no encouragement indicated
\newenvironment{exerciseexC}[1]%
{\begin{description}%
\item[\exercisenorat\nocolon\difficulty{#1}\smallcolonspace] 
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
%		exercise with soln page pointer in margin
%		 exercise  , NOT indented
\newenvironment{Exercises}[1]%
{\begin{trivlist}\item[]\exercisesol{#1}\colonspace }%
{\end{trivlist}}
\newenvironment{Exercisesx}[2]% exercise with difficulty rating
{\begin{trivlist}\item[]\exercisesol{#1}\difficulty{#2} }%
{\end{trivlist}}
\newenvironment{exercises}[1]%
{\begin{description}%
\item[\exercisesol{#1}\colonspace] 
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
% same but with a rating
\newenvironment{exercisesx}[2]%
{\begin{description}%
\item[\exercisesol{#1}\difficulty{#2}] 
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}
\newcommand{\exref}[1]{ex~\ref{#1}\typeout{exercise or example?}}
\newcommand{\exerciseref}[1]{exercise~\ref{#1} \mbox{(\pdot\pageref{#1})}}
\newcommand{\exercisebref}[1]{(exercise~\ref{#1}, \mbox{\pdot\pageref{#1})}}
\newcommand{\Exerciseref}[1]{Exercise~\ref{#1} \mbox{(\pdot\pageref{#1})}}
\newcommand{\exerciseonlyref}[1]{exercise~\ref{#1}}
\newcommand{\Exerciseonlyref}[1]{Exercise~\ref{#1}}
\newcommand{\exerciserefrange}[2]{exercises~\ref{#1}--\ref{#2} \mbox{(p\pdot\pageref{#1}--\pageref{#2})}}
\newcommand{\Exerciserefrange}[2]{Exercises~\ref{#1}--\ref{#2} \mbox{(p\pdot\pageref{#1}--\pageref{#2})}}
\newcommand{\exerciseonlyrange}[2]{exercises~\ref{#1}--\ref{#2}}
\newcommand{\Exerciseonlyrange}[2]{Exercises~\ref{#1}--\ref{#2}}
\newcommand{\pagerange}[2]{\mbox{(p\pdot\pageref{#1}--\pageref{#2})}}
\newcommand{\exerciserefshort}[1]{\ref{#1} \mbox{(\pdot\pageref{#1})}}
\newcommand{\exerciseonlyrangeshort}[2]{\ref{#1}--\ref{#2}}
\newcommand{\exerciserefrangeshort}[2]{\ref{#1}--\ref{#2} \mbox{(p\pdot\pageref{#1}--\pageref{#2})}}
\newcommand{\exampleref}[1]{example~\ref{#1} \mbox{(\pdot\pageref{#1})}}
\newcommand{\Exampleref}[1]{Example~\ref{#1} \mbox{(\pdot\pageref{#1})}}
\newcommand{\exampleonlyref}[1]{example~\ref{#1}}
\newcommand{\Exampleonlyref}[1]{Example~\ref{#1}}
%		 solution to exercise  , NOT indented
\newenvironment{Sexercise}[1]% used by \soln
{\begin{trivlist}\item[]%
\mbox{\sf Solution to \exerciseref{#1}\colonspace} 
\label{#1.sol}% this makes it possible to point to where the soln is
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{trivlist}}
%
\newenvironment{CSexercise}[1]% used by \continuedsoln
{\begin{trivlist}\item[]%
\mbox{\sf Solution to \exerciseref{#1}\colonspace} 
\label{#1.csol}% this makes it possible to point to where the soln is
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{trivlist}}
%
\newenvironment{Sexample}[1]%
{\begin{trivlist}\item[]%
\mbox{\sf Solution to \exampleref{#1}\colonspace} 
\label{#1.sol}% this makes it possible to point to where the soln is
% this caused problems? :
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{trivlist}}
%		 solution to example  , NOT indented
\newcommand{\sexample}[2]{\begin{Sexample}{#1} #2 \end{Sexample}}
%		 solution to exercise  , indented
% CUP recommend not using indented form.
%
\newenvironment{sexercise}[1]%
{\begin{description}%
\item[\mbox{\sf Solution to \exerciseref{#1}\colonspace}] 
\label{#1.sol}% this makes it possible to point to where the soln is
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{description}}

% 
% Defns of Convenient forms:
%
% \exercisa is obsolete
\newcommand{\exercisa}[1]{\begin{exercisee} #1 \end{exercisee}}
% \exampl{label}{ Text }
\newcommand{\exampl}[2]{\begin{examplee}\examplehack\label{#1} #2 \end{examplee}}
% \cexampl{label}{ Text }
\newcommand{\cexampl}[2]{\begin{cexamplee}{#1} #2 \end{cexamplee}}
% \Exampl{label}{ Text }
\newcommand{\Exampl}[2]{\begin{Examplee}\label{#1} #2 \end{Examplee}}
% \Cexampl{label}{ Text }
\newcommand{\Cexampl}[2]{\begin{Cexamplee}{#1} #2 \end{Cexamplee}}
% \exercis{label}{ Text }
\newcommand{\exercis}[2]{\begin{exercisee}\exercisehack\label{#1} #2 \end{exercisee}}
%
% This is the main one I used until Sun 5/1/03 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
% \exercisx{A1}{label}{ Text }
% from Sun 5/1/03 such commands are replaced by 
% \exercisxA{1}{label}{ Text }    if it was an A-rated exercise
% or
% \exercisxB{1}{label}{ Text } for Bs etc
% 
\newcommand{\exercisx}[3]{\begin{exerciseex}{#1}\exercisehack\label{#2} #3 \end{exerciseex}}
% NEW in Sun 5/1/03 - the first is identical to old \exercisx 
\newcommand{\exercisxA}[3]{\begin{exerciseex}{#1}\exercisehack\label{#2} #3 \end{exerciseex}}
\newcommand{\exercisxB}[3]{\begin{exerciseexB}{#1}\exercisehack\label{#2} #3 \end{exerciseexB}}
\newcommand{\exercisxC}[3]{\begin{exerciseexC}{#1}\exercisehack\label{#2} #3 \end{exerciseexC}}
\newcommand{\exercisxD}[3]{\begin{exerciseexC}{#1}\exercisehack\label{#2} #3 \end{exerciseexC}}
\newcommand{\exercisxE}[3]{\begin{exerciseexC}{#1}\exercisehack\label{#2} #3 \end{exerciseexC}}
%
% the following 5 with ``a'' (available) are identical to the above 5 but are provided in case
% I ever wanted to include links to AVAILABLE solutions (currently in the unpublishedsolutions.tex
% and therefore not referred to)
% 
\newcommand{\exercisaxA}[3]{\begin{exerciseex}{#1}\exercisehack\label{#2} #3 \end{exerciseex}}
\newcommand{\exercisaxB}[3]{\begin{exerciseexB}{#1}\exercisehack\label{#2} #3 \end{exerciseexB}}
\newcommand{\exercisaxC}[3]{\begin{exerciseexC}{#1}\exercisehack\label{#2} #3 \end{exerciseexC}}
\newcommand{\exercisaxD}[3]{\begin{exerciseexC}{#1}\exercisehack\label{#2} #3 \end{exerciseexC}}
\newcommand{\exercisaxE}[3]{\begin{exerciseexC}{#1}\exercisehack\label{#2} #3 \end{exerciseexC}}
% \exercinr{label}{ Text }   % NO RATING
\newcommand{\exercinr}[2]{\begin{exercisenr}\exercisehack\label{#1} #2 \end{exercisenr}}
% \exerciss{label}{ Text }
\newcommand{\exerciss}[2]{\begin{exercises}{#1.sol}\exercisehack\label{#1} #2 \end{exercises}}
% This is the other main one I used until Sun 5/1/03 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
% \exercissx{A1}{label}{ Text }   % includes talking rat
% now use thus:  \exercissx{1}{label}{ Text }   % includes talking rat
%\newcommand{\exercissx}[3]{\begin{exercisesx}{#2.sol}{#1}\exercisehack\label{#2} #3 \end{exercisesx}}
%\newcommand{\exercissxA}[3]{\begin{exercisesx}{#2.sol}{#1}\exercisehack\label{#2} #3 \end{exercisesx}}
%\newcommand{\exercissxB}[3]{\begin{exerciseexB}{#1}\exercisehack\label{#2} #3 (Solution on \pref{#2.sol}.) \end{exerciseexB}}
\newcommand{\exercissx}[3]{\begin{exerciseex}{#1,   {\rm \pref{#2.sol}}}\exercisehack\label{#2} #3 \end{exerciseex}}
\newcommand{\exercissxA}[3]{\begin{exerciseex}{#1,  {\rm \pref{#2.sol}}}\exercisehack\label{#2} #3 \end{exerciseex}}
\newcommand{\exercissxB}[3]{\begin{exerciseexB}{#1, {\rm \pref{#2.sol}}}\exercisehack\label{#2} #3 \end{exerciseexB}}
\newcommand{\exercissxC}[3]{\begin{exerciseexC}{#1, {\rm \pref{#2.sol}}}\exercisehack\label{#2} #3 \end{exerciseexC}}
\newcommand{\exercissxD}[3]{\begin{exerciseexC}{#1, {\rm \pref{#2.sol}}}\exercisehack\label{#2} #3 \end{exerciseexC}}
\newcommand{\exercissxE}[3]{\begin{exerciseexC}{#1, {\rm \pref{#2.sol}}}\exercisehack\label{#2} #3 \end{exerciseexC}}
% \Exercis{label}{ Text }
\newcommand{\Exercis}[2]{\begin{Exercisee}\label{#1} #2 \end{Exercisee}}
% \Exerciss{label}{ Text }
\newcommand{\Exerciss}[2]{\begin{Exercises}{#1.sol}\label{#1} #2 \end{Exercises}}
\newcommand{\Exercisx}[3]{\begin{Exerciseex}{#1}\label{#2} #3 \end{Exerciseex}}
\newcommand{\ExercisxA}[3]{\begin{ExerciseexA}{#1}\label{#2} #3 \end{ExerciseexA}}
\newcommand{\ExercisxB}[3]{\begin{ExerciseexB}{#1}\label{#2} #3 \end{ExerciseexB}}
\newcommand{\ExercisxC}[3]{\begin{ExerciseexC}{#1}\label{#2} #3 \end{ExerciseexC}}
\newcommand{\ExercissxA}[3]{\begin{ExerciseexA}{#1,  {\rm \pref{#2.sol}}}\label{#2} #3 \end{ExerciseexA}}
\newcommand{\ExercissxB}[3]{\begin{ExerciseexB}{#1,  {\rm \pref{#2.sol}}}\label{#2} #3 \end{ExerciseexB}}
\newcommand{\ExercissxC}[3]{\begin{ExerciseexC}{#1,  {\rm \pref{#2.sol}}}\label{#2} #3 \end{ExerciseexC}}
% \Exerciss{label}{ Text }
\newcommand{\Exercissx}[3]{\begin{Exercisesx}{#2.sol}{#1}\label{#2} #3 \end{Exercises}}
% my intention was that soln would be indented, and Soln not, 
% but now I want all solns not to be indented.
% \soln{label}{ Text } 
% \newcommand{\soln}[2]{\begin{sexercise}{#1} #2 \end{sexercise}}
% \Soln{label}{ Text }
\newcommand{\Soln}[2]{\begin{Sexercise}{#1} #2 \end{Sexercise}}
\newcommand{\soln}[2]{\begin{Sexercise}{#1} #2 \end{Sexercise}}
\newcommand{\continuedsoln}[2]{\begin{CSexercise}{#1} #2 \end{CSexercise}}
%
% OBSOLETE
% problema is just the same as an old exercisa -- no rat, no clever depth change
\newcommand{\problema}[1]{\begin{description}\item[\exercise\colonspace] #1 \end{description}}
\newcommand{\exercisnr}[1]{\begin{description}\item[\exercise\colonspace]
\ifnum \@enumdepth >3 \@toodeep\else
% this advances the enum depth. I don't know what causes it to get unadvanced.
\advance\@enumdepth \@ne 
 #1 \end{description}}
\newcommand{\exampla}[1]{\begin{examplee} #1 \end{examplee}}
\newcommand{\solutiona}[1]{\begin{description}\item[\solution\colonspace]
 \ifnum \@enumdepth >3 \@toodeep\else
% this from chapternotes.sty
\advance\@enumdepth \@ne 
\fi
{#1} \end{description}}

% old stuff... see  exercise.sty.old
%%%%%%%%%%%%%%%%%%%%%%5
%%%%%%%%
%            Used in exercise sheets
%%%%
%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\handle}{}
\newcounter{qno}

\newenvironment{Ex}[1]%
{\begin{trivlist}\item[]\addtocounter{qno}{1}{\large\bf{\handle\arabic{qno}}}$\;\;${\label{#1}}\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi}%
{\end{trivlist}}

\newcommand{\newq}{\vspace*{-\baselineskip}\subsubsection*{}
\noindent{\addtocounter{qno}{1}}{\large\bf{\handle\arabic{qno}}}$\;\;$}
