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.

amsthmat all that I can see. All you get from it is a\newcounter{theorem}. – cfr Jul 11 '15 at 01:21theorem 3when there is nottheorem 2just because there was adefinition 2. If they are of different kinds then they get different labels and different counters. If not, they they get a single name (definitionortheoremor...) and a single counter. But I'm not sure I've understood what you are doing. – cfr Jul 11 '15 at 02:46definition 22you don't need to look for the closestdefinition. 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 03:02fancyrefas an alternative tocleverref. I don't usecleverrefmyself, butfancyrefcan definitely do what you want easily.cleverrefI suspect only with difficulty, if at all. It is designed to figure out the right label automatically. If it is getting it wrong, that is not so good.fancyrefdoesn't try to figure it out for itself. Hence, you won't have the same problem. – cfr Jul 11 '15 at 03:07\crefname{theorem}{definition}{definitions}– cgnieder Jul 11 '15 at 09:03theoremcounter? – cfr Jul 11 '15 at 12:17\newtheorem, I can have the desired result. It's the usage of mdframed that makes that difficult – Zii8roZi Jul 11 '15 at 12:22\label[definition]{defi}inside of thedefinitionenvironment – cgnieder Jul 11 '15 at 12:28fancyrefif you really want to. However, it may just be easier to saydefinition \ref{defi}unless there's a clever solution out there. This is kind of why I don't usecleverrefalthough I'm not using a common counter as far as readers are concerned but only so far as TeX is concerned. (I use, say,enumeratebut might use\label{qn:qn1)to getquestion 1.) – cfr Jul 11 '15 at 12:29