Add optional arguments before and after “Exercise”

environmentsexercisesoptional argumentstheorems

I would like to create an exercise environment for which I can specify two optional arguments, one appearing before the "Exercise" mention and the other one appearing in brackets after the theorem number.

I'm using the amsthm package.

Using a basic theorem style such as

\newtheoremstyle{mystyle}{}{}{}{}{\sffamily\bfseries}{.}{ }{}
\theoremstyle{mystyle}{
    \newtheorem{exo}{\bfseries Exercice}}

the following code

\begin{exo}[Nom de l'exo]
Montrer par récurrence sur $n$ que
\end{exo}

gives me this output:

enter image description here

In another course, I used a different environment that helped me add something before the "Exercise":

\newtheoremstyle{exerci}{}{}{}{}{\bfseries}{.}{.5em}{\thmnote{#3}~\thmname{#1}~\thmnumber{#2}}
\theoremstyle{exerci}
\newtheorem{exo}[]{Exercice}

so

\begin{exo}[\faBatteryThreeQuarters]
Montrer par récurrence sur $n$ que
\end{exo}

gives me the following output:

enter image description here

Now I can't figure how to allow for both optional arguments to appear. Does anyone know how to achieve that?

Thanks in advance!

Best Answer

Here you can let a battery name (bata to batd) or nothing, and let an exercise name or not.

exercise command

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{ifthen}
\usepackage{fontawesome}



\newcounter{exer}
    \newenvironment{exercice}[2]
    {
    \addtocounter{exer}{1}
    
    \par\medskip\noindent{\textbf{\ifthenelse{\equal{#1}{}}{\hphantom{\bata}}{#1} Exercice {\theexer}\ifthenelse{\equal{#2}{}}{.}{ #2.}}}}
    {\par \medskip}
    
\begin{document}
    \newcommand{\bata}{\faBatteryQuarter}
    \newcommand{\batb}{\faBatteryHalf}
    \newcommand{\batc}{\faBatteryThreeQuarters}
    \newcommand{\batd}{\faBatteryFull}

    \exercice{\bata}{(Dérivation)}
    
        Montrer que la dérivée de la fonction $x \mapsto x^2$ est la fonction $x\mapsto 2x$.
        
    \exercice{\batb}{(Énoncé)}
    
        Faire ce que vous voulez pendant une demi-heure.
        
    \exercice{\batc}{}
        
        Ici, pas de nom d'exercice.

    \exercice{\batd}{(Comme avant)}
    
    \exercice{}{}
    
        Ici, carrément ni batterie, ni nom.
\end{document}
Related Question