# Combine mdframed, xparse and cleveref

Inspired by this post: Typesetting a definition, I wanted to define some fancy theorem-like environments.

Contrary to the examples given in this post, I need a definition environment with a counter and an optional title, so I used xparse.

Here is a MWE.

\documentclass{article}

\usepackage{mathtools, isomath}
\usepackage{amsthm}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{xparse}
\usepackage{cleveref}

\newtheorem{theorem}{Theorem}

\newcommand{\thedefinition}{\arabic{theorem}}
\NewDocumentEnvironment{definition}{o}{%
\refstepcounter{theorem}
\begin{mdframed}[%
singleextra={
\node[
overlay,
anchor=west,
xshift=7pt,
fill=gray,
rounded corners=2pt,
draw] at (P-|O) {\bfseries
\IfValueTF{#1}{%
Definition~\thedefinition~(#1)
}{%
Definition~\thedefinition
}
};
},
firstextra={
\node[
overlay,
anchor=west,
xshift=7pt,
fill=gray,
rounded corners=2pt,
draw] at (P-|O) {\bfseries
\IfValueTF{#1}{%
Definition~\thedefinition~(#1)
}{%
Definition~\thedefinition
}
};
},
]
}{%
\end{mdframed}
}

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod

\begin{definition}[Optional]
\label{defi}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
\end{definition}

\cref{defi}
\end{document}


I erased some part of the style, so don't worry about that.

My problem is the last line with cref, it gives theorem 1 and not definition 1 which is normal because I didn't do anything to change that. I found some answers instructing to use crefalias or the aliascnt package, but i didn't succeed in using them. Any help would be appreciated.

PS: If you have some advices to simplify the code, I'll be happy to take them.

• Wouldn't it be better to use a new counter? Or a new theorem type? Right now, you are not really using amsthm at all that I can see. All you get from it is a \newcounter{theorem}. – cfr Jul 11 '15 at 1:21
• @cfr I have multiple environments that I want numbered with the same counter. The definition environment is supposed to act like a new theorem type, so i don't really understand what you mean by "Or a new theorem". – Zii8roZi Jul 11 '15 at 1:33
• You will cause readers much confusion if you say theorem 3 when there is not theorem 2 just because there was a definition 2. If they are of different kinds then they get different labels and different counters. If not, they they get a single name (definition or theorem or...) and a single counter. But I'm not sure I've understood what you are doing. – cfr Jul 11 '15 at 2:46
• It's a common practice in some journals to use the same counter. This way when you want to look for definition 22 you don't need to look for the closest definition. You just look to the current counter (of the page you're reading) and you know where to go from there ;) – Zii8roZi Jul 11 '15 at 3:02
• I must be blind but I can read that nowhere in the question… :( Anyway, then the OP just needs to use \label[definition]{defi} inside of the definition environment – cgnieder Jul 11 '15 at 12:28

If I understood this correctly you want both theorems and definitions sharing the same counter but want use different names with cleveref when referring to on or the other? In this case it should suffice to give the type as optional argument in the definition environment:

\documentclass{article}

\RequirePackage{mathtools, isomath}
\RequirePackage{amsthm}
\RequirePackage[framemethod=tikz]{mdframed}
\usepackage{xparse}
\usepackage{cleveref}

\newtheorem{theorem}{Theorem}

\newcommand{\thedefinition}{\arabic{theorem}}
\NewDocumentEnvironment{definition}{o}{%
\refstepcounter{theorem}
\begin{mdframed}[%
singleextra={
\node[
overlay,
anchor=west,
xshift=7pt,
fill=gray,
rounded corners=2pt,
draw] at (P-|O) {\bfseries
\IfValueTF{#1}{%
Definition~\thedefinition~(#1)
}{%
Definition~\thedefinition
}
};
},
firstextra={
\node[
overlay,
anchor=west,
xshift=7pt,
fill=gray,
rounded corners=2pt,
draw] at (P-|O) {\bfseries
\IfValueTF{#1}{%
Definition~\thedefinition~(#1)
}{%
Definition~\thedefinition
}
};
},
]
}{%
\end{mdframed}
}

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod

\begin{definition}[Optional]
\label[definition]{defi}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
\end{definition}

\Cref{defi} and \cref{foo}

\begin{theorem}\label{foo}
foo bar
\end{theorem}

\end{document}


A more automised solution uses the aliascnt package:

\usepackage{aliascnt}
\usepackage{cleveref}

\newtheorem{theorem}{Theorem}
\newaliascnt{definition}{theorem}

\NewDocumentEnvironment{definition}{o}{%
\refstepcounter{definition}


Then the optional argument of \label is not necessary.

I take it the definition of the definition environment is still a draft (the result can surely not be what you want)? Do you have a reason for using \RequirePackage instead of \usepackage?

• I erased some part of the definition to not clutter the question (but you're right that's not the final version). RequirePackage was just a copy/paste error, I'll fix that in my question. Thanks for the answer, I'll wait a little bit (I really don't wanna change all my labels), if nothing comes up, I'll accept your anwser. – Zii8roZi Jul 11 '15 at 12:46
• @Zii8roZi then see my edit: there is an automated solution – cgnieder Jul 11 '15 at 12:53