2

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.

Zii8roZi
  • 555
  • 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 01: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 01: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 02: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 03:02
  • In that case, I suggest you look at fancyref as an alternative to cleverref. I don't use cleverref myself, but fancyref can definitely do what you want easily. cleverref I 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. fancyref doesn't try to figure it out for itself. Hence, you won't have the same problem. – cfr Jul 11 '15 at 03:07
  • Apparently my language isn't supported, I have to use cleveref. – Zii8roZi Jul 11 '15 at 04:48
  • You want \crefname{theorem}{definition}{definitions} – cgnieder Jul 11 '15 at 09:03
  • @clemens But won't that change it for all things counted using the theorem counter? – cfr Jul 11 '15 at 12:17
  • @cfr oh, so the OP wants theorems and definitions sharing a common counter? – cgnieder Jul 11 '15 at 12:20
  • @clemens yes. If I define the definition environment with a simple \newtheorem, I can have the desired result. It's the usage of mdframed that makes that difficult – Zii8roZi Jul 11 '15 at 12:22
  • @clemens That's how I understood 'I have multiple environments that I want numbered with the same counter. ' in this comment. – cfr Jul 11 '15 at 12:23
  • 2
    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
  • You can provide the necessary language support for fancyref if you really want to. However, it may just be easier to say definition \ref{defi} unless there's a clever solution out there. This is kind of why I don't use cleverref although I'm not using a common counter as far as readers are concerned but only so far as TeX is concerned. (I use, say, enumerate but might use \label{qn:qn1) to get question 1.) – cfr Jul 11 '15 at 12:29
  • @clemens Thats why I included a link to the relevant comment which appears as this comment in my comment ;). – cfr Jul 11 '15 at 12:31
  • @cfr I must be blind! I overread this... Time for coffee :) – cgnieder Jul 11 '15 at 12:32

1 Answers1

1

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}

enter image description here

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?

cgnieder
  • 66,645
  • 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