10

Problem

Essentially I've got the same problem described in Cleveref with counters with same name: I've got theorems, lemmata and corollaries all sharing the same counter, and when referencing them using \cref the text always reads “theorem”.

The main difference is that I'm using \mdtheorem from the mdframed package to set up my theorem environments. I like the way the boxes look, so switching to amsthm or ntheorem as suggested in that answer is not an option for me. I even have amsthm included, but that alone isn't enough.

I've looked a bit at the sources. It seems cleveref provides a version of \refstepcounter which can take the name of the environment as an optional argument. So that should probably be used.

Workaround

So one solution which works for me is the following: copy mdframed.sty and add that optional argument in the right place, which looks approximately like this.

 {%#3 given -- number relationship
    \global\@namedef{the#2}{\@nameuse{the#3}}%
    \newenvironment{#2}[1][]{%
      \refstepcounter[#2]{#3}
      \ifstrempty{##1}%

But this is an ugly hack. In particular, it means I'll have to update mdframed myself to keep this in the code.

Question

I also don't see any such use of an optional argument in amsthm or ntheorem. So my question is this: do you know of any more elegant way to achieve what I'm trying to do? Can you tell how these two packages address the problem, and can I somehow port that to mdframed?

Patching

Or can I use etoolbox and \patchcmd to get this working? I tried the following, but it didn't work:

\expandafter\patchcmd\csname mdtheorem\space code\endcsname
{\refstepcounter{#3}}{\refstepcounter[#2]{#3}}{}{}

With \tracingpatches the log claims that patching worked, but in fact nothing changed.

Example

Small reproducing example:

\documentclass{article}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{hyperref}
\usepackage[textwidth=5cm]{geometry}
\usepackage[nameinlink,noabbrev]{cleveref}
\mdtheorem{Theorem}{Theorem}
\mdtheorem{Lemma}[Theorem]{Lemma}
\crefname{Theorem}{theorem}{theorems}
\crefname{Lemma}{lemma}{lemmas}
\begin{document}
 \begin{Lemma}\label{x}
  Mary had a little lamb.
 \end{Lemma}
 Now look at \cref{x}.
\end{document}

Undesired rendering

1 Answer 1

5

The following seems to work:

enter image description here

\documentclass{article}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{hyperref}
\usepackage[textwidth=5cm]{geometry}
\usepackage[nameinlink,noabbrev]{cleveref}
\mdtheorem{Theorem}{Theorem}
\mdtheorem{Lemma}[Theorem]{Lemma}
\crefname{Theorem}{theorem}{theorems}
\crefname{Lemma}{lemma}{lemmas}
\let\oldLemma\Lemma
\renewcommand{\Lemma}{%
  \crefalias{Theorem}{Lemma}% Theorem counter now looks like Lemma
  \oldLemma}
\begin{document}
\begin{Lemma}\label{x}
  Mary had a little lamb.
\end{Lemma}
Now look at \cref{x}.

\begin{Theorem}\label{y}
  Mary had a little lamb.
\end{Theorem}
Now look at \cref{y}.
\end{document}

We change the way the universal Theorem counter is represented using an alias. We have to sneak it into the Lemma before the counter is stepped (implying that the settings key is too late), so we update the environment opening command \Lemma. Since the alias is set within a group, the local change is reversed when outside the environment.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .