Tengo problemas al intentar configurar mdframed para que
- es compatible con Cleverref
- todas las cajas mdframed comparten el mismo contador
- no se rompe cuando se anidan entornos mdframed
Archivo de demostración. Lo ideal es que veamos
Lema 1.1: lema principal
Nota 1.2: cálculos
Lema 1.3: cálculo 1
Lema 1.4: cálculo 2
Observación 1.5: rmk 1
Aquí está mi archivo de preámbulo original, que cumple con 1) y 3). Pero tenemos el Lema 1.1, la Nota 1.1, el Lema 1.2, el Lema 1.3. Observación 1.1 Debido a que los contadores están separados para cada uno\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}
Inspirándose enaquí, y usando un nuevo contador boxcounter
, he reemplazado las tres \mdtheorem
líneas dentro del documento anterior. Ahora tenemos un contador compartido, pero rompimos el sistema inteligente (que muestra ??? al intentar hacer referencia a los entornos). Esto se debe a que \cref
intenta hacer referencia al contador de cajas en lugar del entorno en sí.
Los entornos anidados también están rotos, con números repetidos (la Nota 1.5 debería ser la Nota 1.2), como se desprende de la compilación del documento.
Lema 1.1, Nota 1.5, Lema 1.3, Lema 1.4, Observación 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}
Respuesta1
Dado que no publicó MWE compilable, la respuesta a continuación no se adapta directamente a sus necesidades, sino que solo sirve para ilustrar el principio. La razón por la que el tercer bloque de código mostrado no funciona es que está utilizando el mismo contador para los tres entornos. Cleveref utiliza el nombre del contador para diferenciar los distintos entornos.
La solución a esto (que es utilizada internamente por cleveref
para manejar entornos de teoremas que comparten el mismo contador creado poramsthm
el cual tiene el mismo problema) es crear un contador esclavo duplicado.
Por ejemplo
\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}
- este código crea tres contadores:
mastercounter
,envAcount
yenvBcount
. - las líneas entre
\makeatletter...\makeatother
makeenvAcount
eenvBcount
internamente son el mismo contador quemastercounter
, por lo que al aumentar uno se aumentan los tres * . - Luego definimos nuevos entornos
envA
yenvB
; Tenga en cuenta que en cada entorno se utiliza\refstepcounter
cualquiera de los dosenvAcount
oenvBcount
según corresponda. Esto es importante ya que el nombre del contador se registra en el.aux
archivo cuando se accede\label
al entorno. - Tenga en cuenta que definimos el
\Crefname
uso denombres del mostrador, no los nombres del entorno que utiliza el contador.
El código anterior produce el siguiente resultado
Ahora puede incorporar el truco usted mismo en su documento completo mdframed
para definir sus entornos.
* Una alternativa a hacer esto a mano es usar un paquete comoaliascnt.
Ahora que has añadido un MWE, aquí tienes una versión adaptada a tu 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}
el cual muestra
Un truco aquí es la línea.
\edef\x{Note \thenote\ #1}
para solucionar el problema por el cual, si coloca \thenote
, frametitle={Note \thenote\ #1}
cuando mdframed
llegue el momento de expandirlo, \thenote
los ambientes interiores ya habrán cambiado el valor de. Este truco básicamente asegura almacenar el valor actual de \thenote
en \x
y con la magia del "alcance variable" cuando el entorno interior sale, el valor de \x
se restaura a lo que era antes de ingresar a los entornos.