contadores compartidos de mdthereom, referencia inteligente y teoremas anidados

contadores compartidos de mdthereom, referencia inteligente y teoremas anidados

Tengo problemas al intentar configurar mdframed para que

  1. es compatible con Cleverref
  2. todas las cajas mdframed comparten el mismo contador
  3. 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 \mdtheoremlí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 \crefintenta 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 cleverefpara 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}
  1. este código crea tres contadores: mastercounter, envAcounty envBcount.
  2. las líneas entre \makeatletter...\makeatothermake envAcounte envBcountinternamente son el mismo contador que mastercounter, por lo que al aumentar uno se aumentan los tres * .
  3. Luego definimos nuevos entornos envAy envB; Tenga en cuenta que en cada entorno se utiliza \refstepcountercualquiera de los dos envAcounto envBcountsegún corresponda. Esto es importante ya que el nombre del contador se registra en el .auxarchivo cuando se accede \labelal entorno.
  4. 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 ingrese la descripción de la imagen aquí

Ahora puede incorporar el truco usted mismo en su documento completo mdframedpara 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

ingrese la descripción de la imagen aquí

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 mdframedllegue el momento de expandirlo, \thenotelos ambientes interiores ya habrán cambiado el valor de. Este truco básicamente asegura almacenar el valor actual de \thenoteen \xy con la magia del "alcance variable" cuando el entorno interior sale, el valor de \xse restaura a lo que era antes de ingresar a los entornos.

información relacionada