I use an environment "exo" defined by
\usepackage{amsmath}
\newtheorem{exo}{Exercice}
I would like to optionally include some exercises later in the document, using the following:
\makeatletter
\newcounter{nbexosfac}
\setcounter{nbexosfac}{0}
\newcommand{\exofacultatif}[2][]{
\stepcounter{nbexosfac}
\edef\tmp{\string\thenbexosfac}
\expandafter\def\csname exosfacopt\tmp\endcsname{#1}
\expandafter\def\csname exosfaccorps\tmp\endcsname{#2}
}
\newcounter{myaux}
\newcommand{\restitueexos}{
\ifnum\thenbexosfac>0%
\begin{center} Exercices supplémentaires \end{center}
\setcounter{myaux}{0}
\@whilenum\value{myaux}<\value{nbexosfac}\do{
\stepcounter{myaux}
\edef\tmp{\string\themyaux}
\begin{exo}[\csname exosfacopt\tmp\endcsname]
\csname exosfaccorps\tmp\endcsname
\end{exo}
}
\setcounter{nbexosfac}{0}
\fi
}
\makeatother
However it does not work as intended in the document:
\begin{document}
\begin{exo}
Texte 1
\end{exo}
\exofacultatif{Texte 2}
\begin{exo}[essai 3]
Texte 3
\end{exo}
\exofacultatif[essai 4]{Texte 4}
\restitueexos
\end{document}
The stored data is not retrieved, but it adds an superfluous "()": \exofacultatif{} is not equivalent to \begin{exo}[] \end{exo}.
Any idea ?
Edit: I removed the superfluous \def\withexosfac{} and \ifdefined\withexosfac.
Edit 2: The solution by egreg is better. However I found a solution that seems to work, even if I am not sure why:
\makeatletter
\newcounter{nbexosfac}
\setcounter{nbexosfac}{0}
\newcommand{\exofacultatif}[2][]{
\stepcounter{nbexosfac}
\expandafter\def\csname exosfacopt\string{\thenbexosfac}\endcsname{#1}
\expandafter\def\csname exosfaccorps\string{\thenbexosfac}\endcsname{#2}
}
\newcounter{myaux}
\newcommand{\restitueexosfac}{
\ifnum\thenbexosfac>0%
(...)
\setcounter{myaux}{0}
\@whilenum\value{myaux}<\value{nbexosfac}\do{
\stepcounter{myaux}
\edef\tmp{\csname exosfacopt\string{\themyaux}\endcsname}
\ifx\tmp\empty
\begin{exo}
\else
\begin{exo}[\tmp]
\fi
\csname exosfaccorps\string{\themyaux}\endcsname
\end{exo}
}
\setcounter{nbexosfac}{0}
\fi
}
\makeatother
Edit 3: So, on the basis of the code by egreg, I wrote
\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\newtheorem{exo}{Exercise}
\ExplSyntaxOn
\seq_new:N \exof_seq
\NewDocumentEnvironment{exo*}{+b}
{
\seq_gput_right:Nn \exof_seq { \begin{exo} #1 \end{exo} }
}{}
\newcommand{\restitueexosfac}{
\seq_if_empty:NF \exof_seq {%
\section*{Exercices~complémentaires}%
}
\seq_use:Nn \exof_seq {}
\seq_clear:N \exof_seq
}
\ExplSyntaxOff
\begin{document}
\section{Titre 1}
\begin{exo}
Texte 1
\end{exo}
\begin{exo}
Texte 2
\end{exo}
\section*{Autre titre}
\begin{exo}[essai 3]
Texte 3
\end{exo}
\begin{exo}[essai 4]
Texte 4
\end{exo}
\restitueexosfac
\section{Titre 2}
\begin{exo}
Texte 5
\end{exo}
\restitueexosfac
\end{document}
Edit 4: The following is not relevant any more.
However, you can see that the space is lost in the
\section*command inside\restitueexosfac. Is there a reason it is lost here and not in the\section*{Autre titre}command elsewhere? I can use~or\but I would prefer to change\restitueexosfacso that it is not necessary. Any idea?



\documentclass{article}. Sorry. I also see that a complete document is compiled in the page by tex.stackexchange.com. – dominique Sep 04 '22 at 13:17\ExplSyntaxOn) spaces are not tokenized as space tokens but ignored in any case. If you want a space token in expl3-syntax, write~. Outside expl3-syntax that denotes a non-breaking space. Inside expl3-syntax that denotes a normal breaking space/is tokenized as space-token/explicit character token of category 10(space) and character code 32.So, while in expl3-syntax do\section*{Exercices~complémentaires}%. – Ulrich Diez Sep 04 '22 at 17:32