I am having trouble trying to set up mdframed so that
- it is compatible with cleverref
- all mdframed boxes share the same counter
- 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}


\usepackagedeclarations. – Willie Wong Dec 19 '23 at 01:57