0

I am having trouble trying to set up mdframed so that

  1. it is compatible with cleverref
  2. all mdframed boxes share the same counter
  3. does not break when mdframed environments are nested

Demo file. Ideally we should see

  • Lemma 1.1: main lemma

  • Note 1.2: calculations

  • Lemma 1.3: calculation 1

  • Lemma 1.4: calculation 2

  • Remark 1.5: rmk 1

Here is my original preamble file, which accomplishes 1) and 3). But we have Lemma 1.1, Note 1.1, Lemma 1.2, Lemma 1.3. Remark 1.1 Because the counters are separate for each \mdtheorem

\documentclass{book}
\usepackage{amsthm,xcolor,mdframed,hyperref,cleveref}

%Headers and Sections

% Colors

\definecolor{tab-blue}{rgb}{0.341,0.471,0.643}
\definecolor{tab-orange}{rgb}{0.894,0.580,0.267}

%%%
%Counters
\newcounter{thechapter}
%%%
% Define fChapter
%for putting chapter numbers after the title


% Chapters
\newcommand{\fchapter}[1]%
{%
    \phantomsection\chapter*{Chapter #1}%
    \stepcounter{thechapter}
    \newpage% Chapter Number
} 

% Copy the same style but modify the colour only
\mdfdefinestyle{theoremstyle}{%
    backgroundcolor=tab-blue!10
}

\mdfdefinestyle{remarkstyle}{%
    backgroundcolor=tab-blue!5,
}

\mdfdefinestyle{notestyle}{%
    backgroundcolor=tab-orange!5,
}

\mdtheorem[style=theoremstyle]{lemma}{Lemma}[thechapter]
\mdtheorem[style=remarkstyle]{remark}{Remark}[thechapter]
\mdtheorem[style=notestyle]{note}{Note}[thechapter]

% Here we manage Clever Headers
\crefname{lemma}{lem.}{lems.}
\Crefname{lemma}{Lemma}{Lemmas}

\crefname{note}{note}{notes}
\Crefname{note}{Note}{Notes}

\begin{document}
\fchapter{1: main result}
\begin{lemma}[main lemma]\label{lem:main}
1234556
\end{lemma}
\begin{proof}
    123
\begin{note}[calculations]
\begin{lemma}[calculation 1]\label{lem:calc 1}
calc 1 here
\end{lemma}
\begin{proof}
calc 1 proof
\end{proof}
\begin{lemma}[calculation 2]\label{lem:calc 2}
calc 2 here
\end{lemma}
\begin{proof}
calc 2 proof
\end{proof}
\begin{remark}[rmk 1]\label{rmk: rmk 1}
rmk 
\end{remark}
\end{note}

\end{proof}

Cleveref tests: 
\begin{itemize}
    \item main lemma \cref{lem:main}
    \item note calculations: \cref{note:calculations}
    \item lemma calc 1: \cref{lem:calc 1}
    \item lemma calc 2: \cref{lem:calc 2}
    \item rmk 1 1: \cref{rmk: rmk 1}
\end{itemize}
\end{document}


Taking inspiration from here, and using a new counter, boxcounter, I have replaced the three \mdtheorem lines within the previous document. We now have a shared counter, but we broke cleveref, (which shows ??? when trying to reference the environments). This is because \cref is trying to reference the boxcounter instead of the environment itself.

Nested environments are also broken, with repeated numbers (Note 1.5 should be Note 1.2), as compiling the document gives

Lemma 1.1, Note 1.5, Lemma 1.3, Lemma 1.4, Remark 1.5.

\documentclass{book}
\usepackage{amsthm,xcolor,mdframed,hyperref,cleveref}
% Colors
\definecolor{tab-blue}{rgb}{0.341,0.471,0.643}
\definecolor{tab-orange}{rgb}{0.894,0.580,0.267}

%%%
%Counters
\newcounter{thechapter}

% Chapters
\newcommand{\fchapter}[1]%
{%
    \phantomsection\chapter*{Chapter #1}%
    \stepcounter{thechapter}
    \newpage% Chapter Number
} 

% Copy the same style but modify the colour only
\mdfdefinestyle{theoremstyle}{%
    backgroundcolor=tab-blue!10
}

\mdfdefinestyle{remarkstyle}{%
    backgroundcolor=tab-blue!5,
}

\mdfdefinestyle{notestyle}{%
    backgroundcolor=tab-orange!5,
}

\newcounter{boxcounter}[thechapter]
\renewcommand{\theboxcounter}{\arabic{thechapter}.\arabic{boxcounter}}

\newenvironment{lemma}[1][]{%
    \refstepcounter{boxcounter}
    \begin{mdframed}[%
        frametitle={Lemma \theboxcounter\ #1},
        style=theoremstyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{note}[1][]{%
    \refstepcounter{boxcounter}
    \begin{mdframed}[%
        frametitle={Note \theboxcounter\ #1},
        style=notestyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{remark}[1][]{%
    \refstepcounter{boxcounter}
    \begin{mdframed}[%
        frametitle={Remark \theboxcounter\ #1},
        style=remarkstyle
    ]%
}{%
    \end{mdframed}
}

% Here we manage Clever Headers
\crefname{lemma}{lem.}{lems.}
\Crefname{lemma}{Lemma}{Lemmas}

\crefname{note}{note}{notes}
\Crefname{note}{Note}{Notes}

\begin{document}
\fchapter{1: main result}
\begin{lemma}[main lemma]\label{lem:main}
1234556
\end{lemma}
\begin{proof}
    123
\begin{note}[calculations]\label{note:calculations}
\begin{lemma}[calculation 1]\label{lem:calc 1}
calc 1 here
\end{lemma}
\begin{proof}
calc 1 proof
\end{proof}
\begin{lemma}[calculation 2]\label{lem:calc 2}
calc 2 here
\end{lemma}
\begin{proof}
calc 2 proof
\end{proof}
\begin{remark}[rmk 1]\label{rmk: rmk 1}
rmk 
\end{remark}
\end{note}

\end{proof}

Cleveref tests: 
\begin{itemize}
    \item main lemma \cref{lem:main}
    \item note calculations: \cref{note:calculations}
    \item lemma calc 1: \cref{lem:calc 1}
    \item lemma calc 2: \cref{lem:calc 2}
    \item rmk 1 1: \cref{rmk: rmk 1}
\end{itemize}
\end{document}
1
  • 1
    Please don't post code snippets, and not in multiple parts too. It is a lot easier for other people to answer your question is you post a COMPLETE minimum example code that you wish to ask about. As it stands if I copy your "skeleton code" in the first code display, and add you "preamble code" from the second display, the tex file will not compile due to missing \usepackage declarations. Dec 19, 2023 at 1:57

1 Answer 1

1

Since you didn't post compileable MWE, the answer below are not directly tailored to your needs, but only serves to illustrate the principle. The reason that your third displayed code block doesn't work is that you are using the same counter for the three environments. Cleveref uses the counter name to tell apart the various environments.

The solution to this (which is internally used by cleveref for handling theorem environments sharing the same counter created by amsthm which has the same problem), is to create a duplicate slave counter.

For example

\documentclass{article}
\usepackage{cleveref}

\newcounter{mastercounter}
\newcounter{envAcount}
\newcounter{envBcount}
\makeatletter
\let\c@envAcount\c@mastercounter
\let\c@envBcount\c@mastercounter
\makeatother

\newenvironment{envA}{\refstepcounter{envAcount}\textbf{First Environment \theenvAcount}\par}{}
\newenvironment{envB}{\refstepcounter{envBcount}\textbf{Second Enviornment \theenvBcount}\par}{}

\Crefname{envAcount}{First}{First}
\Crefname{envBcount}{Second}{Second}

\begin{document}

\begin{envA}\label{envAtest}
        Test
\end{envA}

\begin{envB}\label{envBtest}
        Another test
\end{envB}

\Cref{envAtest} and \Cref{envBtest}

\end{document}
  1. this code creates three counters: mastercounter, envAcount, and envBcount.
  2. the lines between \makeatletter...\makeatother make envAcount and envBcount internally the same counter as mastercounter, so increasing one increase all three*.
  3. Then we define new environments envA and envB; note that in each environment you \refstepcounter either envAcount or envBcount as appropriate. This is important as the counter name is recorded in the .aux file when you \label the environment.
  4. Note that we define the \Crefname using the names of the counter, not the names of the environment using the counter.

The code above yields the following output enter image description here

You can now incorporate the trick yourself in your full document using mdframed to define your environments.

* An alternative to doing this by hand is to use a package like aliascnt.


Now that you've added a MWE, here's a version adapted to your MWE

\documentclass{book}
\usepackage{amsthm,xcolor,mdframed,hyperref,cleveref}
% Colors
\definecolor{tab-blue}{rgb}{0.341,0.471,0.643}
\definecolor{tab-orange}{rgb}{0.894,0.580,0.267}

%%%
%Counters
\newcounter{thechapter}

% Chapters
\newcommand{\fchapter}[1]%
{%
    \phantomsection\chapter*{Chapter #1}%
    \stepcounter{thechapter}
    \newpage% Chapter Number
} 

% Copy the same style but modify the colour only
\mdfdefinestyle{theoremstyle}{%
    backgroundcolor=tab-blue!10
}

\mdfdefinestyle{remarkstyle}{%
    backgroundcolor=tab-blue!5,
}

\mdfdefinestyle{notestyle}{%
    backgroundcolor=tab-orange!5,
}

\newcounter{boxcounter}[thechapter]
\renewcommand{\theboxcounter}{\arabic{thechapter}.\arabic{boxcounter}}
\newcounter{lemma}
\newcounter{note}
\newcounter{remark}
\makeatletter
\let\c@lemma\c@boxcounter
\let\c@note\c@boxcounter
\let\c@remark\c@boxcounter
\let\thelemma\theboxcounter
\let\thenote\theboxcounter
\let\theremark\theboxcounter
\makeatother


\newenvironment{lemma}[1][]{%
    \refstepcounter{lemma}
    \edef\x{Lemma \thelemma\ #1}
    \begin{mdframed}[%
        frametitle={\x},
        style=theoremstyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{note}[1][]{%
    \refstepcounter{note}
    \edef\x{Note \thenote\ #1}
    \begin{mdframed}[%
        frametitle={\x},
        style=notestyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{remark}[1][]{%
    \refstepcounter{remark}
    \edef\x{Remark \theremark\ #1}
    \begin{mdframed}[%
        frametitle={\x},
        style=remarkstyle
    ]%
}{%
    \end{mdframed}
}

% Here we manage Clever Headers
\crefname{lemma}{lem.}{lems.}
\Crefname{lemma}{Lemma}{Lemmas}

\crefname{note}{note}{notes}
\Crefname{note}{Note}{Notes}

\begin{document}
\fchapter{1: main result}
\begin{lemma}[main lemma]\label{lem:main}
1234556
\end{lemma}
\begin{proof}
    123
\begin{note}[calculations]\label{note:calculations}
\begin{lemma}[calculation 1]\label{lem:calc 1}
calc 1 here
\end{lemma}
\begin{proof}
calc 1 proof
\end{proof}
\begin{lemma}[calculation 2]\label{lem:calc 2}
calc 2 here
\end{lemma}
\begin{proof}
calc 2 proof
\end{proof}
\begin{remark}[rmk 1]\label{rmk: rmk 1}
rmk 
\end{remark}
\end{note}

\end{proof}

Cleveref tests: 
\begin{itemize}
    \item main lemma \cref{lem:main}
    \item note calculations: \cref{note:calculations}
    \item lemma calc 1: \cref{lem:calc 1}
    \item lemma calc 2: \cref{lem:calc 2}
    \item rmk 1 1: \cref{rmk: rmk 1}
\end{itemize}
\end{document}

which shows

enter image description here

A trick here is the line

\edef\x{Note \thenote\ #1}

to work around the issue where if you put \thenote into frametitle={Note \thenote\ #1}, by the time mdframed gets around to expanding it the value of \thenote has already been changed by the interior environments. This trick basically makes sure to store the current value of \thenote into \x, and with the magic of "variable scoping" when the interior environment exits the value of \x is restored to what was the case before entering the environments.

You must log in to answer this question.

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