5

I would like to be able to do something like this

\begin{exo}[Name][\Star\Star] Content \end{exo}

which would print something like

**Exercise 7 (Name). Content

I want exo's counter to share the counter with other theorems and be able to reference it with cref. If possible, I would like to use amsthm to minimize the changes in my files.

An example of code that I would like to work

\documentclass{article}

\usepackage{amsthm}

\begin{document}
\newtheorem{eDef}{Def}
\newtheorem{eExo}[eDef]{Exo}

\begin{eDef}[Name]
Content
\end{eDef}

\begin{eExo}[Name][**]
Content
\end{eExo}

\begin{eExo}[Name][*]
Content
\end{eExo}

\begin{eExo}[Name][***]
Content
\end{eExo}
\end{document}

Resulting in

Def 1 (Name). Content
**Exercise 2 (Name). Content
*Exercise 3 (Name). Content
***Exercise 4 (Name). Content
x4rkz
  • 162
  • 1
    \newtheorem{eExo}[eDef]{**Exercise} is sufficent? –  Jun 26 '19 at 10:54
  • No because some exercise would have two stars, other one or three stars. I'm not sure creating an environment for each difficulty is the right solution. Unless it is? – x4rkz Jun 26 '19 at 11:07
  • 1
    @x4rkz I'm not sure why the exercises don't have their own counter. The number of asterisks indicates the difficulty; you can use the Exercise package. – user5402 Jun 26 '19 at 11:14
  • You're right, I don't think I need them to use the same counter. I didn't know this pacakge, gonna learn about it – x4rkz Jun 26 '19 at 11:25

1 Answers1

9

The main problem is the double optional argument. I suggest using a different syntax in order to distinguish between them.

\documentclass{article}
\usepackage{amsthm,xparse}

\theoremstyle{definition}
\newtheorem{eDef}{Def}
\newtheorem{eExoinner}[eDef]{\Exoname}
\newcommand{\Exoname}{Exo}

\NewDocumentEnvironment{eExo}{D(){}o}
 {\renewcommand\Exoname{\makebox[0pt][r]{#1}Exo}%
  \IfNoValueTF{#2}{\eExoinner}{\eExoinner[#2]}}
 {\endeExoinner}

\begin{document}

\begin{eDef}[Name]
Content
\end{eDef}

\begin{eExo}[Name]
Content
\end{eExo}

\begin{eExo}(*)[Name]
Content
\end{eExo}

\begin{eExo}(**)[Name]
Content
\end{eExo}

\begin{eExo}(***)[Name]
Content
\end{eExo}

\begin{eExo}
Content
\end{eExo}

\begin{eExo}(*)
Content
\end{eExo}

\begin{eExo}(**)
Content
\end{eExo}

\begin{eExo}(***)
Content
\end{eExo}

\end{document}

enter image description here

A different strategy is to define four different environments.

\documentclass{article}
\usepackage{amsthm}

\theoremstyle{definition}
\newtheorem{eDef}{Def}
\newtheorem{eExoinner}[eDef]{\makebox[0pt][r]{\Exostar}Exo}
\newcommand{\Exostar}{}

\newenvironment{eExo}{\eExoinner}{\endeExoinner}
\newenvironment{eExo*}{\renewcommand\Exostar{*}\eExoinner}{\endeExoinner}
\newenvironment{eExo**}{\renewcommand\Exostar{**}\eExoinner}{\endeExoinner}
\newenvironment{eExo***}{\renewcommand\Exostar{***}\eExoinner}{\endeExoinner}

\begin{document}

\begin{eDef}[Name]
Content
\end{eDef}

\begin{eExo}[Name]
Content
\end{eExo}

\begin{eExo*}[Name]
Content
\end{eExo*}

\begin{eExo**}[Name]
Content
\end{eExo**}

\begin{eExo***}[Name]
Content
\end{eExo***}

\begin{eExo}
Content
\end{eExo}

\begin{eExo*}
Content
\end{eExo*}

\begin{eExo**}
Content
\end{eExo**}

\begin{eExo***}
Content
\end{eExo***}

\end{document}
egreg
  • 1,121,712