%%
%% This is the original source file mathpar.sty
%%
%% Package `mathpar' to use with LaTeX 2e
%% Copyright Didier Remy, all rights reserved.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{exercise}
         [2001/23/02 v0.92 Exercise]

\newif \ifin@exercise \in@exercisefalse
\newif \ifAnswers \Answerstrue
\newif \ifAnswerAnchors \AnswerAnchorstrue
\newif \ifAnswerLater \AnswerLatertrue

\DeclareOption*{\edef \ExerciseAnswerFile{\CurrentOption}}
\DeclareOption{noanswers}{\Answersfalse}
\DeclareOption{immediate}{\AnswerLaterfalse}
\DeclareOption{noanchors}{\AnswerAnchorsfalse}
\ProcessOptions
\RequirePackage {keyval}

\def \@beginexercisehook {}
\def \AtBeginExercise {\g@addto@macro \@beginexercisehook}
\AtBeginDocument {\@beginexercisehook}

\AtBeginExercise
{\@ifundefined {ExerciseAnswerFile}{\edef \ExerciseAnswerFile{\jobname}}{}}

\def \exo@empty{}
\@ifundefined {thechapter}
   {\let \exo@chapter \exo@empty}
   {\def \exo@chapter {\thechapter}}
\newcommand {\pdf@hyperlink}[2]
  {\@ifundefined {hyperlink}{#2}{\hyperlink {#1}{#2}}}
\newcommand {\pdf@hypertarget}[2]
  {\@ifundefined {hypertarget}{#2}{\hypertarget {#1}{#2}}}

\def \ExerciseName {Exercise}
\def \AnswerName {Answer}
\def \exo@francais{frenchb}
\ifx \exo@francais \languagename
\def \ExerciseName {Exercice}
\def \AnswerName {Réponse}
\fi

\newcommand {\exo@SquareBox}[2][0.033em]
  {\hbox {\vbox {\hrule height #1\hbox to #2
         {\vrule width #1\vbox to #2{\vss}\hss \vrule width #1}\hrule
         height #1}}}

\AtBeginExercise{\newwrite \ans@channel \def \open@ans
 {\immediate \openout \ans@channel \ExerciseAnswerFile.ans\relax
 \global \let \open@ans \relax}}
%\openans
% \let \@exercise \exercise
% \def \exercise {\openans \global \let \exercise \@exercise\exercise}

%\def \ans@write #1{\xdef \ans@put {\write\ans@channel {#1}}\ans@put}
\def \ans@write #1{\open@ans \immediate \write\ans@channel {#1}}

\newtheorem{Exercise}{\pdf@hypertarget{Exo-\theExercise}{\ExerciseName}}
\newcounter{Question}

\let \exo@endparenv \@endparenv
\def \exo@no@endparenv {\let \@endparenv \exo@endparenv}
\def \exo@nobreakafter {\let \@endparenv \exo@no@endparenv}
\newenvironment {exercise}[1][]
    {\def \@test {#1}\ifx \@test \@empty
     \begin{Exercise}\else \begin{Exercise}[#1]\fi
     \in@exercisetrue
     \setcounter {Question}{0}%
     \let \nobreakafter \exo@nobreakafter
     \ans@write {\string
         \ExerciseNew {\@currentlabel}{\exo@chapter}{\thepage}{#1}}}
    {\ifvmode \vskip -\lastskip \nopagebreak \else
     \unskip \unpenalty \unskip \penalty 100\-\hbox{}\penalty 100\fi
     \hfill \hbox to 1em{\hfil \exo@SquareBox {0.5em}}\end{Exercise}}

\def \ans@lastlabel {}
\def \fragileanswer #1{\ans@write {#1}}

%% This environment can be redefined to customize immediate answers.
\newenvironment {AnswerNow}
   {\par \em \noindent {\underline {\AnswerName}:}}
   {\par \smallskip}

\def\@answer
  {\ifAnswers \ifin@exercise 
     \ifAnswerLater
        \em
        \stepcounter{Question}%
        \ifAnswerAnchors
           \@ifundefined {hyperlink}{}
              {\unskip \nobreak\hfil\penalty50\hskip 1em \hbox{}\nobreak
               \hskip 0em plus 1filll 
               \pdf@hyperlink
                  {Ans-\theExercise.\theQuestion}{\hbox {\AnswerName}}\nobreak
               \hskip 1em\penalty -10000}%
        \fi 
        \ifx \ans@lastlabel \@currentlabel
           \ans@write {\string \AnswerContinued
                         {\@currentlabel}{\theQuestion}{\thepage}}%
        \else
           \ans@write {\string \AnswerNew
                         {\@currentlabel}{\theQuestion}{\thepage}}%
           \global \edef \ans@lastlabel {\@currentlabel}%
        \fi
      \else
         \let \endanswer \endAnswerNow \let \ans@copytoend \AnswerNow
      \fi
   \fi \fi
   \ans@copytoend}
\let \answer \@answer  

\def \un@inpenc@loop#1#2{%
  \@tempcnta`#1\relax
  \loop
    \catcode\@tempcnta 12
  \ifnum\@tempcnta<`#2\relax
     \advance\@tempcnta\@ne
  \repeat}

  
%% To undo accents encodings, so as to be truely verbatim
\def\uninputencoding {\@ifundefined {inputencoding}{\catcode `\^^A 12}{%
    \un@inpenc@loop\^^A\^^H%
    \un@inpenc@loop\^^K\^^K%
    \un@inpenc@loop\^^N\^^_%
    \un@inpenc@loop\^^?\^^ff%
    }}

\def \env@answer{answer}
\def \next@end {\csname end\env@answer\endcsname
                \expandafter \endgroup \ignorespaces}
\bgroup
\catcode `\|=0 \catcode `\{=12 \catcode `\}=12
\catcode `\[=1 \catcode `\]=2  \catcode`\\=12

|gdef |ans@dotest#1\end{#2}#3|END[%
      |def |@test [#1]|ifx |@test|empty
          |def |@env [#2]|ifx |@env |env@answer 
             |let |next |next@end 
          |fi
      |fi
      |ifx |next |next@end |else
        |ifAnswers |ans@write [|exo@arg]|fi
      |fi]
|gdef |ans@testarg#1|END[|ans@dotest#1 |relax|relax\end{|relax}|END]
|egroup


\def\ans@copytoend
  {\uninputencoding
   \let \do \@makeother \dospecials \obeylines\ans@copy@first}
{\obeylines%
\gdef \ans@copy#1
  {\def\exo@arg{#1}\ans@testarg#1\END\next}%
\gdef \ans@copy@first#1
  {\def \@test{#1}\let \next \ans@copy%
   \ifx \@test \empty \else \def\exo@arg{#1}\ans@testarg#1\END\fi%
   \next}%
}

\def \AnswerSection {\section}
\newcommand{\inputanswers}[2][\ExerciseAnswerFile]
 {\ifAnswers \ifAnswerLater
    \AnswerSection {#2}%
    \immediate \write \ans@channel {\string \endinput}%
    \immediate \closeout \ans@channel
%    \@input {\ExerciseAnswerFile .ans}%
    \bgroup
    \newread \ans@read
    \openin \ans@read \ExerciseAnswerFile.ans
       \ifeof \ans@read
          \message {*** Warning! No answers to exercices}\closein  \ans@read
       \else \closein \ans@read
       \message {Including answers to exercices}\input \ExerciseAnswerFile.ans
       \fi 
     \egroup
  \fi \fi}

\def \AnswerContinued #1#2#3{\egroup
  \bgroup
  \pdf@hypertarget {Ans-#1.#2}{}
  \subsubsection* {\pdf@hyperlink {Exo-#1}{Exercise #1 (continued)}}
}
\def \AnswerNew #1#2#3{\egroup
  \bgroup
  \par \goodbreak\pdf@hypertarget {Ans-#1.#2}{}\nobreak
  \subsection* {\pdf@hyperlink {Exo-#1}{Exercise #1, page #3}}}
\newcommand {\ExerciseNew}[4]{\setcounter {Exercise}{#1}}


%%%% extension


% \newanswer{name}
%     {before-begin}{after-begin}
%     {fragile-insertion}
%     [number-of-args][default-value]{before-end}{after-end}

\newcommand {\NewQuestion}[2]
   {\setcounter {Exercise}{#1}\setcounter {Question}{#2}%
    \pdf@hypertarget {Ans-#1.#2}{}}
\def \AnswerLink #1{\pdf@hyperlink {Ans-\theExercise.\theQuestion}{#1}}
\def \ExerciseLink #1{\pdf@hyperlink {Exo-\theExercise}{#1}}

\newcommand \newanswer [6]
 {\newenvironment {exo-#1}{#2}{#3}\relax
  \newenvironment {now-#1}{#4}{#5}\relax
  \newenvironment {#1}[1][]
      {\def \hide {\Answersfalse}
       \def \show {\Answerstrue}
       \def \later {\AnswerLatertrue}
       \def \now {\AnswerLaterfalse}
       ##1\relax
       \def \env@answer {#1}%
       \ifAnswers 
         \ifAnswerLater
           \ifin@exercise
             \stepcounter{Question}%
             \fragileanswer
                  {\string \NewQuestion {\theExercise}{\theQuestion}}%
           \fi
           \fragileanswer{\string \begin{ans-#1}#6}%
           \csname exo-#1\endcsname
         \else
           \expandafter \let \expandafter \@next \csname endnow-#1\endcsname
           \expandafter \let \csname end#1\endcsname \@next
           \expandafter \let \expandafter \@next \csname now-#1\endcsname
           \let \ans@copytoend \@next
         \fi
       \fi 
       \ans@copytoend}
      {\csname endexo-#1\endcsname
       \ifAnswers \fragileanswer{\string \end{ans-#1}}\fi
      }\relax
  \newenvironment {ans-#1}}

\newcommand {\AnswerNiceLink}[1][\AnswerName]
    {\ifAnswerAnchors \@ifundefined {hyperlink}{}
         {\unskip \nobreak\hfil\penalty50\hskip 1em \hbox{}\nobreak
          \hskip 0em plus 1filll 
          \AnswerLink{\hbox {#1}}%
          \nobreak
          \hskip 1em\penalty -10000}\fi}

\newanswer {myanswer}
   {\em \AnswerNiceLink}
   {}
   {\AnswerNow}
   {\endAnswerNow}
   {{\thepage}}
   [1]
   {\ifnum \theQuestion=1\relax 
      \subsection* {\ExerciseLink {Exercise \theExercise, page #1}}%
    \else
      \subsubsection* 
         {\ExerciseLink {Exercice \theExercise\space (continued)}}%
    \fi}
   {}


