\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{dnExercises}[2026/09/01 Exercices, questions numerotees]

%%% Exercices et numerotation des questions.
%%% Requiert dnTheorems (machinerie \newtheorem) et donc un fichier
%%% de langue, pour \exercisename.

\RequirePackage{dnTheorems}
\RequirePackage{dnFirstPage}  % provides \firstpage

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Compteur unique, independant du sectionnement : les exercices sont
%%% numerotes 1, 2, 3... sur tout le document, en feuille de TD comme
%%% en polycopie.  Si un livre demandait un jour des exercices
%%% numerotes par chapitre, on ajouterait un environnement distinct
%%% plutot que de compliquer celui-ci.
\theoremstyle{definition}
\newcounter{cntNo}
\newcounter{cntSubno}
\newtheorem{exercise}{\exercisename}
\newtheorem*{exerciseAst}{\exercisename}
\theoremstyle{plain}

\newenvironment{exe}{\setcounter{cntNo}{0}%
  \begin{exercise}}{\end{exercise}}
\newenvironment{exe*}{\setcounter{cntNo}{0}%
  \begin{exerciseAst}}{\end{exerciseAst}}

\newcommand{\solution}{\setcounter{cntNo}{0}%
  \par\removelastskip\vskip.5\baselineskip%
  \noindent{\textbf{\textit{Solution.}}}\\
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% numerotation des questions
%%% \no doit exister avant d'etre redefini : babel-french le fournit,
%%% babel-british non.  \providecommand comble le trou sans ecraser.
%%% L'argument optionnel de \no sert quand il faut de l'espace vertical
%%% supplementaire par rapport au paragraphe precedent.
\providecommand{\no}{}
\renewcommand{\no}[1][]{\stepcounter{cntNo}\setcounter{cntSubno}{0}%
  \ifmmode \tikz{%
    \path[R, use as bounding box] (0, 0) rectangle (2\parindent, 1ex);
    \node[scale=.7, anchor=east, inner sep=0.]
    at (2.7em, 0.55ex) {(\roman{cntNo})};
  }
  \else%
  \leavevmode\par%
  \ifthenelse{\equal{#1}{}}{\vspace{.13\baselineskip}}{\vspace{#1}}%
  \noindent%
  \tikz{%
    \path[use as bounding box] (0,0) rectangle (1.3\parindent, 2ex);
    \node[scale=.85, anchor=east, inner sep=0.]
    at (1.1\parindent, 0.5ex) {\textup{\thecntNo)}};
  }%
  \ifthenelse{\equal{#1}{}}{}{\hspace{-.9ex}}%
  \fi%
}

\newcommand{\noAst}{\stepcounter{cntNo}\setcounter{cntSubno}{0}%
  \leavevmode\par%
  \vspace{.13\baselineskip}\noindent
  \tikz{%
    \path[use as bounding box] (0,0) rectangle (1.3\parindent, 2ex);
    \node[scale=.85, anchor=east, inner sep=0.]
    at (1.1\parindent, 0.5ex) {\thecntNo$^\ast$)};
  }%
}

\newcommand{\tno}{\stepcounter{cntNo}\setcounter{cntSubno}{0}%
  \tikz{%
    \path[use as bounding box] (0, 0) rectangle (1.8em, 1ex);
    \path (1em, 0.55ex) node[scale=.8, left, inner sep=0]
    {(\roman{cntNo})};
  }
}

\newcommand{\subno}[1][]{%
  \stepcounter{cntSubno}%
  \leavevmode\par%
  \vspace{.13\baselineskip}
  \ifthenelse{\equal{#1}{}}{}{\vspace{#1}}\noindent
  \tikz{%
    \path[R, use as bounding box] (0,0) rectangle (2\parindent, 2ex);
    \node[scale=.85, anchor=east, inner sep=0.]
    at (1.8\parindent, 0.5ex) {\alph{cntSubno})};
  }%
}
